bump version
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Engine.pm
index 430634a..604d689 100644 (file)
@@ -1,8 +1,10 @@
 
 package MooseX::Storage::Engine;
 use Moose;
+use Scalar::Util qw(refaddr);
 
-our $VERSION = '0.02';
+our $VERSION   = '0.19';
+our $AUTHORITY = 'cpan:STEVAN';
 
 # the class marker when 
 # serializing an object. 
@@ -16,7 +18,7 @@ has 'storage' => (
 
 has 'seen' => (
     is      => 'ro',
-    isa     => 'HashRef',
+    isa     => 'HashRef[Int]', # int is the refaddr
     default => sub {{}}
 );
 
@@ -26,13 +28,13 @@ has 'class'  => (is => 'rw', isa => 'Str');
 ## this is the API used by other modules ...
 
 sub collapse_object {
-       my $self = shift;
+    my ( $self, %options ) = @_;
 
        # NOTE:
        # mark the root object as seen ...
-       $self->seen->{$self->object} = undef;
+       $self->seen->{refaddr $self->object} = undef;
        
-    $self->map_attributes('collapse_attribute');
+    $self->map_attributes('collapse_attribute', \%options);
     $self->storage->{$CLASS_MARKER} = $self->object->meta->identifier;    
        return $self->storage;
 }
@@ -45,7 +47,7 @@ sub expand_object {
     
        # NOTE:
        # mark the root object as seen ...
-       $self->seen->{$data} = undef;    
+       $self->seen->{refaddr $data} = undef;    
     
     $self->map_attributes('expand_attribute', $data, \%options);
        return $self->storage;    
@@ -54,31 +56,36 @@ sub expand_object {
 ## this is the internal API ...
 
 sub collapse_attribute {
-    my ($self, $attr)  = @_;
-    $self->storage->{$attr->name} = $self->collapse_attribute_value($attr) || return;
+    my ($self, $attr, $options)  = @_;
+    my $value = $self->collapse_attribute_value($attr, $options);
+    return if !defined($value);
+    $self->storage->{$attr->name} = $value;
 }
 
 sub expand_attribute {
     my ($self, $attr, $data, $options)  = @_;
-    $self->storage->{$attr->name} = $self->expand_attribute_value($attr, $data->{$attr->name}, $options) || return;
+    my $value = $self->expand_attribute_value($attr, $data->{$attr->name}, $options);
+    $self->storage->{$attr->name} = defined $value ? $value : return;
 }
 
 sub collapse_attribute_value {
-    my ($self, $attr)  = @_;
+    my ($self, $attr, $options)  = @_;
+    # Faster, but breaks attributes without readers, do we care?
+       #my $value = $attr->get_read_method_ref->($self->object);
        my $value = $attr->get_value($self->object);
-       
+
        # NOTE:
-       # this might not be enough, we might 
-       # need to make it possible for the 
+       # this might not be enough, we might
+       # need to make it possible for the
        # cycle checker to return the value
-    $self->check_for_cycle_in_collapse($attr, $value) 
+    $self->check_for_cycle_in_collapse($attr, $value)
         if ref $value;
-       
+
     if (defined $value && $attr->has_type_constraint) {
         my $type_converter = $self->find_type_handler($attr->type_constraint);
         (defined $type_converter)
             || confess "Cannot convert " . $attr->type_constraint->name;
-        $value = $type_converter->{collapse}->($value);
+        $value = $type_converter->{collapse}->($value, $options);
     }
        return $value;
 }
@@ -107,20 +114,20 @@ sub expand_attribute_value {
 
 sub check_for_cycle_in_collapse {
     my ($self, $attr, $value) = @_;
-    (!exists $self->seen->{$value})
+    (!exists $self->seen->{refaddr $value})
         || confess "Basic Engine does not support cycles in class(" 
                  . ($attr->associated_class->name) . ").attr("
                  . ($attr->name) . ") with $value";
-    $self->seen->{$value} = undef;
+    $self->seen->{refaddr $value} = undef;
 }
 
 sub check_for_cycle_in_expansion {
     my ($self, $attr, $value) = @_;
-    (!exists $self->seen->{$value})
+    (!exists $self->seen->{refaddr $value})
     || confess "Basic Engine does not support cycles in class(" 
              . ($attr->associated_class->name) . ").attr("
              . ($attr->name) . ") with $value";
-    $self->seen->{$value} = undef;
+    $self->seen->{refaddr $value} = undef;
 }
 
 # util methods ...
@@ -131,8 +138,8 @@ sub map_attributes {
         $self->$method_name($_, @args) 
     } grep {
         # Skip our special skip attribute :)
-        !$_->isa('MooseX::Storage::Meta::Attribute::DoNotSerialize')
-    } ($self->object || $self->class)->meta->compute_all_applicable_attributes;
+        !$_->does('MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize') 
+    } ($self->object || $self->class)->meta->get_all_attributes;
 }
 
 ## ------------------------------------------------------------------
@@ -185,22 +192,29 @@ my %OBJECT_HANDLERS = (
         }
             
         # all is well ...
-        $class->unpack($data);
+        $class->unpack($data, %$options);
     },
     collapse => sub {
-        my $obj = shift;
+        my ( $obj, $options ) = @_;
 #        ($obj->can('does') && $obj->does('MooseX::Storage::Basic'))
 #            || confess "Bad object ($obj) does not do MooseX::Storage::Basic role";
-        $obj->pack();
+        ($obj->can('pack'))
+            || confess "Object ($obj) does not have a &pack method, cannot collapse";
+        $obj->pack(%$options);
     },
 );
 
 
 my %TYPES = (
-    # These are boring ones, so they use the identity function ...
-    'Int'      => { expand => sub { shift }, collapse => sub { shift } },
-    'Num'      => { expand => sub { shift }, collapse => sub { shift } },
+    # NOTE:
+    # we need to make sure that we properly numify the numbers 
+    # before and after them being futzed with, because some of 
+    # 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 ...    
     'Str'      => { expand => sub { shift }, collapse => sub { shift } },
+    'Bool'     => { expand => sub { shift }, collapse => sub { shift } },
     # These are the trickier ones, (see notes)
     # NOTE:
     # Because we are nice guys, we will check 
@@ -214,46 +228,46 @@ my %TYPES = (
     # and add a custom handler.    
     'ArrayRef' => { 
         expand => sub {
-            my $array = shift;
+            my ( $array, @args ) = @_;
             foreach my $i (0 .. $#{$array}) {
                 next unless ref($array->[$i]) eq 'HASH' 
                          && exists $array->[$i]->{$CLASS_MARKER};
-                $array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i])
+                $array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i], @args);
             }
             $array;
         }, 
-        collapse => sub { 
-            my $array = shift;   
+        collapse => sub {
+            my ( $array, @args ) = @_;
             # NOTE:         
             # we need to make a copy cause
             # otherwise it will affect the 
             # other real version.
             [ map {
                 blessed($_)
-                    ? $OBJECT_HANDLERS{collapse}->($_)
+                    ? $OBJECT_HANDLERS{collapse}->($_, @args)
                     : $_
             } @$array ] 
         } 
     },
     'HashRef'  => { 
         expand   => sub {
-            my $hash = shift;
+            my ( $hash, @args ) = @_;
             foreach my $k (keys %$hash) {
                 next unless ref($hash->{$k}) eq 'HASH' 
                          && exists $hash->{$k}->{$CLASS_MARKER};
-                $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k})
+                $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}, @args);
             }
             $hash;            
         }, 
         collapse => sub {
-            my $hash = shift;   
+            my ( $hash, @args ) = @_;
             # NOTE:         
             # we need to make a copy cause
             # otherwise it will affect the 
             # other real version.
             +{ map {
                 blessed($hash->{$_})
-                    ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}))
+                    ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args))
                     : ($_ => $hash->{$_})
             } keys %$hash }            
         } 
@@ -284,6 +298,14 @@ sub remove_custom_type_handler {
 sub find_type_handler {
     my ($self, $type_constraint) = @_;
     
+    # 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');
+
     # this should handle most type usages
     # since they they are usually just 
     # the standard set of built-ins
@@ -321,6 +343,11 @@ sub find_type_handler {
     confess "Cannot handle type constraint (" . $type_constraint->name . ")";    
 }
 
+sub find_type_handler_for {
+    my ($self, $type_handler_name) = @_;
+    $TYPES{$type_handler_name}
+}
+
 1;
 
 __END__
@@ -385,11 +412,13 @@ No user serviceable parts inside. If you really want to know, read the source :)
 
 =over 4
 
-=item B<find_type_handler>
+=item B<find_type_handler ($type)>
+
+=item B<find_type_handler_for ($name)>
 
-=item B<add_custom_type_handler>
+=item B<add_custom_type_handler ($name, %handlers)>
 
-=item B<remove_custom_type_handler>
+=item B<remove_custom_type_handler ($name)>
 
 =back
 
@@ -415,7 +444,7 @@ Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2007 by Infinity Interactive, Inc.
+Copyright 2007-2008 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>