* implement the unpack( $data, inject => {...} ) feature.
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Basic.pm
index a572668..686772b 100644 (file)
@@ -1,22 +1,37 @@
-
 package MooseX::Storage::Basic;
 use Moose::Role;
 
 use MooseX::Storage::Engine;
 
-our $VERSION   = '0.01';
+our $VERSION   = '0.18';
 our $AUTHORITY = 'cpan:STEVAN';
 
 sub pack {
     my ( $self, @args ) = @_;
-    my $e = MooseX::Storage::Engine->new( object => $self );
+    my $e = $self->_storage_get_engine( object => $self );
     $e->collapse_object(@args);
 }
 
 sub unpack {
-    my ( $class, $data, @args ) = @_;
-    my $e = MooseX::Storage::Engine->new( class => $class );
-    $class->new( $e->expand_object($data, @args) );
+    my ($class, $data, %args) = @_;
+    my $e = $class->_storage_get_engine(class => $class);
+    
+    $class->_storage_construct_instance( 
+        $e->expand_object($data, %args), 
+        \%args 
+    );
+}
+
+sub _storage_get_engine {
+    my $self = shift;
+    MooseX::Storage::Engine->new( @_ );
+}
+
+sub _storage_construct_instance {
+    my ($class, $args, $opts) = @_;
+    my %i = defined $opts->{'inject'} ? %{ $opts->{'inject'} } : ();
+    $class->new( %$args, %i );
 }
 
 1;
@@ -54,6 +69,9 @@ MooseX::Storage::Basic - The simplest level of serialization
   
   # unpack the hash into a class
   my $p2 = Point->unpack({ __CLASS__ => 'Point-0.01', x => 10, y => 10 });
+  
+  # unpack the hash, with insertion of paramaters
+  my $p3 = Point->unpack( $p->pack, inject => { x => 11 } );
 
 =head1 DESCRIPTION
 
@@ -66,7 +84,10 @@ but the exported C<Storage> function.
 
 =item B<pack>
 
-=item B<unpack ($data)>
+=item B<unpack ($data [, insert => { key => val, ... } ] )>
+
+Providing the C<insert> argument let's you supply additional arguments to
+the class' C<new> function, or override ones from the serialized data.
 
 =back
 
@@ -92,7 +113,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>