Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stringify type constraint objects more consistently. #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 7 additions & 5 deletions lib/MooseX/Storage/Engine.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down
103 changes: 103 additions & 0 deletions t/006_w_custom_type_handlers_specio.t
Original file line number Diff line number Diff line change
@@ -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');
}





99 changes: 99 additions & 0 deletions t/006_w_custom_type_handlers_typetiny.t
Original file line number Diff line number Diff line change
@@ -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');
}