X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FStorage%2FEngine.pm;h=1b6a70bdf3aac07983ea99287c0203154f343159;hb=004bf3ea04d7818116a94bb4b387d8ae1dd10f9a;hp=753d4d856197be1ebfb8b1008650caf93d273b3a;hpb=14e5132ad4def507c424a56dfb30b935b1ab3b44;p=gitmo%2FMooseX-Storage.git diff --git a/lib/MooseX/Storage/Engine.pm b/lib/MooseX/Storage/Engine.pm index 753d4d8..1b6a70b 100644 --- a/lib/MooseX/Storage/Engine.pm +++ b/lib/MooseX/Storage/Engine.pm @@ -3,7 +3,7 @@ package MooseX::Storage::Engine; use Moose; use Scalar::Util qw(refaddr); -our $VERSION = '0.21'; +our $VERSION = '0.28'; our $AUTHORITY = 'cpan:STEVAN'; # the class marker when @@ -22,7 +22,7 @@ has 'seen' => ( default => sub {{}} ); -has 'object' => (is => 'rw', isa => 'Object'); +has 'object' => (is => 'rw', isa => 'Object', predicate => '_has_object'); has 'class' => (is => 'rw', isa => 'Str'); ## this is the API used by other modules ... @@ -82,7 +82,7 @@ sub collapse_attribute_value { if ref $value; if (defined $value && $attr->has_type_constraint) { - my $type_converter = $self->find_type_handler($attr->type_constraint); + my $type_converter = $self->find_type_handler($attr->type_constraint, $value); (defined $type_converter) || confess "Cannot convert " . $attr->type_constraint->name; $value = $type_converter->{collapse}->($value, $options); @@ -103,7 +103,7 @@ sub expand_attribute_value { } if (defined $value && $attr->has_type_constraint) { - my $type_converter = $self->find_type_handler($attr->type_constraint); + my $type_converter = $self->find_type_handler($attr->type_constraint, $value); $value = $type_converter->{expand}->($value, $options); } return $value; @@ -143,7 +143,7 @@ sub map_attributes { } grep { # Skip our special skip attribute :) !$_->does('MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize') - } ($self->object || $self->class)->meta->get_all_attributes; + } ($self->_has_object ? $self->object : $self->class)->meta->get_all_attributes; } ## ------------------------------------------------------------------ @@ -300,15 +300,23 @@ sub remove_custom_type_handler { } sub find_type_handler { - my ($self, $type_constraint) = @_; - + my ($self, $type_constraint, $value) = @_; + # check if the type is a Maybe and # if its parent is not parameterized. # If both is true recurse this method # using ->type_parameter. - return $self->find_type_handler($type_constraint->type_parameter) - if $type_constraint->parent eq 'Maybe' - and not $type_constraint->parent->can('type_parameter'); + return $self->find_type_handler($type_constraint->type_parameter, $value) + if ($type_constraint->parent && $type_constraint->parent eq 'Maybe' + and not $type_constraint->parent->can('type_parameter')); + + # find_type_for is a method of a union type. If we can call that method + # then we are dealign with a union and we need to ascertain which of + # the union's types we need to use for the value we are serializing. + if($type_constraint->can('find_type_for')) { + my $tc = $type_constraint->find_type_for($value); + return $self->find_type_handler($tc, $value) if defined($tc); + } # this should handle most type usages # since they they are usually just