Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions Object-Tiny-XS/lib/Object/Tiny/XS.pm
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
package Object::Tiny::XS;
use strict 'vars', 'subs';

BEGIN {
require 5.004;
$Object::Tiny::XS::VERSION = '1.01';
}
use 5.006;
use strict;

our $VERSION = '1.01';

sub import {
return unless shift eq 'Object::Tiny::XS';
return unless shift eq __PACKAGE__;
my $pkg = caller;
my $child = !! @{"${pkg}::ISA"};
eval join "\n",
"package $pkg;",
($child ? () : "\@${pkg}::ISA = 'Object::Tiny::XS';"),
"use Class::XSAccessor getters => {",
(map {
defined and ! ref and /^[^\W\d]\w*\z/s
or die "Invalid accessor name '$_'";
"'$_' => '$_',"
} @_),
"};";
die "Failed to generate $pkg" if $@;
return 1;
Class::XSAccessor->import( class => $pkg, getters => [ @_ ] );
no strict 'refs';
*{ "${pkg}::ISA" } = [ __PACKAGE__ ] unless @{ "${pkg}::ISA" };
}

use Class::XSAccessor
constructor => 'new';
use Class::XSAccessor constructor => 'new';

1;

Expand Down
7 changes: 4 additions & 3 deletions Object-Tiny-XS/t/02_main.t
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ SCOPE: {
my $object = Foo->new( foo => 1, bar => 2, baz => 3 );
isa_ok( $object, 'Foo' );
isa_ok( $object, 'Object::Tiny::XS' );
is( scalar( keys %$object ), 3, 'Object contains expect elements' );
is( scalar( keys %$object ), 3, 'Object contains expected elements' );
is( $object->foo, 1, '->foo ok' );
is( $object->bar, 2, '->bar ok' );
eval {
$object->baz;
};
ok( $@, '->bar returns an error' );
ok( $@, '->baz returns an error' );
is( $object->{baz}, 3, '->{baz} does contain value' );
}

# Trigger the constructor exception
SCOPE: {
SKIP: {
skip 'Object::Tiny::XS inherently is less strict about the names of accessors', 1;
eval "package Bar; use Object::Tiny::XS 'bad thing';";
ok( $@ =~ /Invalid accessor name/, 'Got expected error' );
}
4 changes: 2 additions & 2 deletions Object-Tiny-XS/t/03_subclass.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ SCOPE: {
my $object = Foo->new( foo => 1, bar => 2, baz => 3 );
isa_ok( $object, 'Foo' );
isa_ok( $object, 'Bar' );
is( scalar( keys %$object ), 3, 'Object contains expect elements' );
is( scalar( keys %$object ), 3, 'Object contains expected elements' );
is( $object->foo, 1, '->foo ok' );
is( $object->bar, 2, '->bar ok' );
eval {
$object->baz;
};
ok( $@, '->bar returns an error' );
ok( $@, '->baz returns an error' );
is( $object->{baz}, 3, '->{baz} does contain value' );
}