adding support for Deferred
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Engine.pm
index 49e5664..a49cd07 100644 (file)
@@ -2,7 +2,8 @@
 package MooseX::Storage::Engine;
 use Moose;
 
-our $VERSION = '0.02';
+our $VERSION   = '0.03';
+our $AUTHORITY = 'cpan:STEVAN';
 
 # the class marker when 
 # serializing an object. 
@@ -54,17 +55,20 @@ sub expand_object {
 ## this is the internal API ...
 
 sub collapse_attribute {
-    my ($self, $attr, @args)  = @_;
-    $self->storage->{$attr->name} = $self->collapse_attribute_value($attr, @args) || 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, @args)  = @_;
+    my ($self, $attr, $options)  = @_;
        my $value = $attr->get_value($self->object);
        
        # NOTE:
@@ -78,7 +82,7 @@ sub collapse_attribute_value {
         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, @args);
+        $value = $type_converter->{collapse}->($value, $options);
     }
        return $value;
 }
@@ -191,6 +195,8 @@ my %OBJECT_HANDLERS = (
         my ( $obj, $options ) = @_;
 #        ($obj->can('does') && $obj->does('MooseX::Storage::Basic'))
 #            || confess "Bad object ($obj) does not do MooseX::Storage::Basic role";
+        ($obj->can('pack'))
+            || confess "Object does not have a &pack method, cannot collapse";
         $obj->pack(%$options);
     },
 );
@@ -201,6 +207,7 @@ my %TYPES = (
     'Int'      => { expand => sub { shift }, collapse => sub { shift } },
     'Num'      => { expand => sub { shift }, collapse => sub { shift } },
     '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 
@@ -253,7 +260,7 @@ my %TYPES = (
             # other real version.
             +{ map {
                 blessed($hash->{$_})
-                    ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}), @args)
+                    ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args))
                     : ($_ => $hash->{$_})
             } keys %$hash }            
         } 
@@ -415,7 +422,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>