X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FStorage%2FEngine.pm;h=ccf2fde3b76db8261256f49e0b4561d25e28e3ee;hb=e44b5f5498b782752d2c91b6796698c86143a2f0;hp=cee4178ef3d2aadaf96880cf8f164c008bfbd87a;hpb=1f5c6dd8f93eeac19200d0b3ad36d33e5065f53b;p=gitmo%2FMooseX-Storage.git diff --git a/lib/MooseX/Storage/Engine.pm b/lib/MooseX/Storage/Engine.pm index cee4178..ccf2fde 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.25'; +our $VERSION = '0.32'; 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 ... @@ -35,22 +35,22 @@ sub collapse_object { $self->seen->{refaddr $self->object} = undef; $self->map_attributes('collapse_attribute', \%options); - $self->storage->{$CLASS_MARKER} = $self->object->meta->identifier; + $self->storage->{$CLASS_MARKER} = $self->object->meta->identifier; return $self->storage; } sub expand_object { my ($self, $data, %options) = @_; - + $options{check_version} = 1 unless exists $options{check_version}; - $options{check_authority} = 1 unless exists $options{check_authority}; + $options{check_authority} = 1 unless exists $options{check_authority}; # NOTE: # mark the root object as seen ... - $self->seen->{refaddr $data} = undef; - + $self->seen->{refaddr $data} = undef; + $self->map_attributes('expand_attribute', $data, \%options); - return $self->storage; + return $self->storage; } ## this is the internal API ... @@ -98,10 +98,10 @@ sub expand_attribute_value { if( ref $value and not( $options->{disable_cycle_check} or $self->class->does('MooseX::Storage::Traits::DisableCycleDetection') - )) { + )) { $self->check_for_cycle_in_collapse($attr, $value) } - + if (defined $value && $attr->has_type_constraint) { my $type_converter = $self->find_type_handler($attr->type_constraint, $value); $value = $type_converter->{expand}->($value, $options); @@ -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; } ## ------------------------------------------------------------------ @@ -158,27 +158,27 @@ sub map_attributes { # below, so I need easy access my %OBJECT_HANDLERS = ( expand => sub { - my ($data, $options) = @_; + my ($data, $options) = @_; (exists $data->{$CLASS_MARKER}) || confess "Serialized item has no class marker"; # check the class more thoroughly here ... my ($class, $version, $authority) = (split '-' => $data->{$CLASS_MARKER}); my $meta = eval { $class->meta }; - confess "Class ($class) is not loaded, cannot unpack" if $@; - + confess "Class ($class) is not loaded, cannot unpack" if $@; + if ($options->{check_version}) { my $meta_version = $meta->version; - if (defined $meta_version && $version) { + if (defined $meta_version && $version) { if ($options->{check_version} eq 'allow_less_than') { ($meta_version <= $version) || confess "Class ($class) versions is not less than currently available." - . " got=($version) available=($meta_version)"; + . " got=($version) available=($meta_version)"; } elsif ($options->{check_version} eq 'allow_greater_than') { ($meta->version >= $version) || confess "Class ($class) versions is not greater than currently available." - . " got=($version) available=($meta_version)"; - } + . " got=($version) available=($meta_version)"; + } else { ($meta->version == $version) || confess "Class ($class) versions don't match." @@ -186,15 +186,15 @@ my %OBJECT_HANDLERS = ( } } } - + if ($options->{check_authority}) { my $meta_authority = $meta->authority; ($meta->authority eq $authority) || confess "Class ($class) authorities don't match." . " got=($authority) available=($meta_authority)" - if defined $meta_authority && defined $authority; + if defined $meta_authority && defined $authority; } - + # all is well ... $class->unpack($data, %$options); }, @@ -216,7 +216,7 @@ my %TYPES = ( # the JSON engines are stupid/annoying/frustrating 'Int' => { expand => sub { $_[0] + 0 }, collapse => sub { $_[0] + 0 } }, 'Num' => { expand => sub { $_[0] + 0 }, collapse => sub { $_[0] + 0 } }, - # These are boring ones, so they use the identity function ... + # These are boring ones, so they use the identity function ... 'Str' => { expand => sub { shift }, collapse => sub { shift } }, 'Bool' => { expand => sub { shift }, collapse => sub { shift } }, # These are the trickier ones, (see notes) @@ -225,11 +225,11 @@ my %TYPES = ( # your ArrayRef and/or HashRef one level # down and inflate any objects we find. # But this is where it ends, it is too - # expensive to try and do this any more + # expensive to try and do this any more # recursively, when it is probably not # nessecary in most of the use cases. # However, if you need more then this, subtype - # and add a custom handler. + # and add a custom handler. 'ArrayRef' => { expand => sub { my ( $array, @args ) = @_; @@ -242,7 +242,7 @@ my %TYPES = ( }, collapse => sub { my ( $array, @args ) = @_; - # NOTE: + # NOTE: # we need to make a copy cause # otherwise it will affect the # other real version. @@ -261,11 +261,11 @@ my %TYPES = ( && exists $hash->{$k}->{$CLASS_MARKER}; $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}, @args); } - $hash; + $hash; }, collapse => sub { my ( $hash, @args ) = @_; - # NOTE: + # NOTE: # we need to make a copy cause # otherwise it will affect the # other real version. @@ -273,7 +273,7 @@ my %TYPES = ( blessed($hash->{$_}) ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args)) : ($_ => $hash->{$_}) - } keys %$hash } + } keys %$hash } } }, 'Object' => \%OBJECT_HANDLERS, @@ -283,7 +283,7 @@ my %TYPES = ( # - SL #'CodeRef' => { # expand => sub {}, # use eval ... - # collapse => sub {}, # use B::Deparse ... + # collapse => sub {}, # use B::Deparse ... #} ); @@ -323,7 +323,7 @@ sub find_type_handler { # the standard set of built-ins return $TYPES{$type_constraint->name} if exists $TYPES{$type_constraint->name}; - + # the next possibility is they are # a subtype of the built-in types, # in which case this will DWIM in @@ -335,7 +335,7 @@ sub find_type_handler { return $TYPES{$type} if $type_constraint->is_subtype_of($type); } - + # NOTE: # the reason the above will work has to # do with the fact that custom subtypes @@ -343,16 +343,16 @@ sub find_type_handler { # the guts of a type, and not for some # weird structural thing which would # need to be accomidated by the serializer. - # Of course, mst or phaylon will probably + # Of course, mst or phaylon will probably # do something to throw this assumption # totally out the door ;) # - SL - + # NOTE: # if this method hasnt returned by now # then we have no been able to find a # type constraint handler to match - confess "Cannot handle type constraint (" . $type_constraint->name . ")"; + confess "Cannot handle type constraint (" . $type_constraint->name . ")"; } sub find_type_handler_for { @@ -374,7 +374,17 @@ MooseX::Storage::Engine - The meta-engine to handle collapsing and expanding obj =head1 DESCRIPTION -No user serviceable parts inside. If you really want to know, read the source :) +There really aren't any major user serviceable parts here. However the typical +use case is adding new non-Moose classes to the type registry for +serialization. Here is an example of this for DateTime objects. This +assumes a C type has been registered. + + MooseX::Storage::Engine->add_custom_type_handler( + 'DateTime' => ( + expand => sub { DateTime->new(shift) }, + collapse => sub { (shift)->iso8601 }, + ) + ); =head1 METHODS