diff --git a/Changes b/Changes index 44c9dc3..158d989 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,9 @@ Revision history for {{$dist->name}} {{$NEXT}} + - consistently use stringification (i.e. "" overload) of type constraint + objects in MooseX::Storage::Engine, instead of sometimes calling 'name' + method. 0.43 2013-09-11 01:47:40Z (Karen Etheridge) - removed use of deprecated Class::MOP::load_class diff --git a/lib/MooseX/Storage/Engine.pm b/lib/MooseX/Storage/Engine.pm index 5f4f680..0a0fd36 100644 --- a/lib/MooseX/Storage/Engine.pm +++ b/lib/MooseX/Storage/Engine.pm @@ -317,8 +317,8 @@ sub find_type_handler { # this should handle most type usages # since they they are usually just # the standard set of built-ins - return $TYPES{$type_constraint->name} - if exists $TYPES{$type_constraint->name}; + return $TYPES{$type_constraint} + if exists $TYPES{$type_constraint}; # the next possibility is they are # a subtype of the built-in types, @@ -327,9 +327,11 @@ sub find_type_handler { # 100% ideal though, but until I # come up with a decent test case # it will do for now. - foreach my $type (keys %TYPES) { - return $TYPES{$type} - if $type_constraint->is_subtype_of($type); + if ($type_constraint->can('is_a_type_of')) { + foreach my $type (keys %TYPES) { + return $TYPES{$type} + if $type_constraint->is_a_type_of($type); + } } # NOTE: diff --git a/t/006_w_custom_type_handlers_specio.t b/t/006_w_custom_type_handlers_specio.t new file mode 100644 index 0000000..8681fee --- /dev/null +++ b/t/006_w_custom_type_handlers_specio.t @@ -0,0 +1,103 @@ +use strict; +use warnings; + +use Test::More tests => 9; +use Test::Deep; +use Test::Fatal; + +use Test::Requires { + 'Specio::Declare' => '0.08', +}; +use Test::Requires { + 'Moose' => '2.1000', +}; + +BEGIN { + use_ok('MooseX::Storage'); + use_ok('MooseX::Storage::Engine'); +} + +use Specio::Declare 'object_isa_type'; +my $BAR = object_isa_type('Bar'); + +=pod + +This is just a simple example of defining +a custom type handler to take care of custom +inflate and deflate needs. + +=cut + +{ + package Bar; + use Moose; + + has 'baz' => (is => 'rw', isa => 'Str'); + has 'boo' => (is => 'rw', isa => 'Str'); + + sub encode { + my $self = shift; + $self->baz . '|' . $self->boo; + } + + sub decode { + my ($class, $packed) = @_; + my ($baz, $boo) = split /\|/ => $packed; + $class->new( + baz => $baz, + boo => $boo, + ); + } + + MooseX::Storage::Engine->add_custom_type_handler( + $BAR => ( + expand => sub { Bar->decode(shift) }, + collapse => sub { (shift)->encode }, + ) + ); + + package Foo; + use Moose; + use MooseX::Storage; + + with Storage; + + has 'bar' => ( + is => 'ro', + isa => $BAR, + default => sub { + Bar->new(baz => 'BAZ', boo => 'BOO') + } + ); +} + +my $foo = Foo->new; +isa_ok($foo, 'Foo'); + +isa_ok($foo->bar, 'Bar'); + +cmp_deeply( +$foo->pack, +{ + __CLASS__ => "Foo", + bar => "BAZ|BOO", +}, +'... got correct packed structure'); + +{ + my $foo = Foo->unpack({ + __CLASS__ => "Foo", + bar => "BAZ|BOO", + }); + isa_ok($foo, 'Foo'); + + isa_ok($foo->bar, 'Bar'); + + is($foo->bar->baz, 'BAZ', '... got the right stuff'); + is($foo->bar->boo, 'BOO', '... got the right stuff'); +} + + + + + diff --git a/t/006_w_custom_type_handlers_typetiny.t b/t/006_w_custom_type_handlers_typetiny.t new file mode 100644 index 0000000..03a95a5 --- /dev/null +++ b/t/006_w_custom_type_handlers_typetiny.t @@ -0,0 +1,99 @@ +use strict; +use warnings; + +use Test::More tests => 9; +use Test::Deep; +use Test::Fatal; + +use Test::Requires { + 'Types::Standard' => '0.008', +}; + +BEGIN { + use_ok('MooseX::Storage'); + use_ok('MooseX::Storage::Engine'); +} + +require Types::Standard; + +=pod + +This is just a simple example of defining +a custom type handler to take care of custom +inflate and deflate needs. + +=cut + +{ + package Bar; + use Moose; + + has 'baz' => (is => 'rw', isa => 'Str'); + has 'boo' => (is => 'rw', isa => 'Str'); + + sub encode { + my $self = shift; + $self->baz . '|' . $self->boo; + } + + sub decode { + my ($class, $packed) = @_; + my ($baz, $boo) = split /\|/ => $packed; + $class->new( + baz => $baz, + boo => $boo, + ); + } + + MooseX::Storage::Engine->add_custom_type_handler( + Types::Standard::InstanceOf['Bar'] => ( + expand => sub { Bar->decode(shift) }, + collapse => sub { (shift)->encode }, + ) + ); + + package Foo; + use Moose; + use MooseX::Storage; + + with Storage; + + has 'bar' => ( + is => 'ro', + isa => Types::Standard::InstanceOf['Bar'], + default => sub { + Bar->new(baz => 'BAZ', boo => 'BOO') + } + ); +} + +my $foo = Foo->new; +isa_ok($foo, 'Foo'); + +isa_ok($foo->bar, 'Bar'); + +cmp_deeply( +$foo->pack, +{ + __CLASS__ => "Foo", + bar => "BAZ|BOO", +}, +'... got correct packed structure'); + +{ + my $foo = Foo->unpack({ + __CLASS__ => "Foo", + bar => "BAZ|BOO", + }); + isa_ok($foo, 'Foo'); + + isa_ok($foo->bar, 'Bar'); + + is($foo->bar->baz, 'BAZ', '... got the right stuff'); + is($foo->bar->boo, 'BOO', '... got the right stuff'); +} + + + + +