From: Yuval Kogman Date: Wed, 6 Jun 2007 18:02:52 +0000 (+0000) Subject: foo X-Git-Tag: 0_02~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a6ebb4c885376ccba57d836327035a0343f01e72;p=gitmo%2FMooseX-Storage.git foo --- diff --git a/lib/MooseX/Storage/Base/WithChecksum.pm b/lib/MooseX/Storage/Base/WithChecksum.pm index 6db3a56..227c447 100644 --- a/lib/MooseX/Storage/Base/WithChecksum.pm +++ b/lib/MooseX/Storage/Base/WithChecksum.pm @@ -15,7 +15,7 @@ sub pack { my $e = MooseX::Storage::Engine->new( object => $self ); - my $collapsed = $e->collapse_object; + my $collapsed = $e->collapse_object(@args); $collapsed->{$DIGEST_MARKER} = $self->_digest_packed($collapsed, @args); @@ -36,36 +36,44 @@ sub unpack { || confess "Bad Checksum got=($checksum) expected=($old_checksum)"; my $e = MooseX::Storage::Engine->new(class => $class); - $class->new($e->expand_object($data)); + $class->new($e->expand_object($data, @args)); } sub _digest_packed { my ( $self, $collapsed, @args ) = @_; - my $d = shift @args; + my $d = $self->_digest_object(@args); + + + { + local $Storable::canonical = 1; + $d->add( Storable::nfreeze($collapsed) ); + } + + return $d->hexdigest; +} + +sub _digest_object { + my ( $self, %options ) = @_; + my $digest_opts = $options{digest}; + $digest_opts = [ $digest_opts ] if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY'; + my ( $d, @args ) = @$digest_opts; if ( ref $d ) { if ( $d->can("clone") ) { - $d = $d->clone; + return $d->clone; } elsif ( $d->can("reset") ) { $d->reset; + return $d; } else { die "Can't clone or reset digest object: $d"; } } else { - $d = Digest->new($d || "SHA1", @args); + return Digest->new($d || "SHA1", @args); } - - { - local $Storable::canonical = 1; - $d->add( Storable::nfreeze($collapsed) ); - } - - return $d->hexdigest; } - 1; __END__ diff --git a/lib/MooseX/Storage/Basic.pm b/lib/MooseX/Storage/Basic.pm index a0745b2..2a610c9 100644 --- a/lib/MooseX/Storage/Basic.pm +++ b/lib/MooseX/Storage/Basic.pm @@ -7,15 +7,15 @@ use MooseX::Storage::Engine; our $VERSION = '0.01'; sub pack { - my $self = shift; + my ( $self, @args ) = @_; my $e = MooseX::Storage::Engine->new( object => $self ); - $e->collapse_object; + $e->collapse_object(@args); } sub unpack { - my ( $class, $data ) = @_; + my ( $class, $data, @args ) = @_; my $e = MooseX::Storage::Engine->new( class => $class ); - $class->new( $e->expand_object($data) ); + $class->new( $e->expand_object($data, @args) ); } 1; diff --git a/lib/MooseX/Storage/Engine.pm b/lib/MooseX/Storage/Engine.pm index 430634a..1177469 100644 --- a/lib/MooseX/Storage/Engine.pm +++ b/lib/MooseX/Storage/Engine.pm @@ -26,7 +26,7 @@ has 'class' => (is => 'rw', isa => 'Str'); ## this is the API used by other modules ... sub collapse_object { - my $self = shift; + my ( $self, @args ) = @_; # NOTE: # mark the root object as seen ... diff --git a/t/030_with_checksum.t b/t/030_with_checksum.t index 720a633..5d51650 100644 --- a/t/030_with_checksum.t +++ b/t/030_with_checksum.t @@ -124,29 +124,29 @@ SKIP: { ); isa_ok( $foo, 'Foo' ); - my $frozen1 = $foo->freeze( "HMAC_SHA1", "secret" ); + my $frozen1 = $foo->freeze( digest => [ "HMAC_SHA1", "secret" ] ); ok( length($frozen1), "got frozen data" ); my $d2 = Digest::HMAC_SHA1->new("s3cr3t"); - my $frozen2 = $foo->freeze( $d2 ); + my $frozen2 = $foo->freeze( digest => $d2 ); ok( length($frozen2), "got frozen data" ); cmp_ok( $frozen1, "ne", $frozen2, "versions are different" ); - my $foo1 = eval { Foo->thaw( $frozen1, "HMAC_SHA1", "secret" ) }; + my $foo1 = eval { Foo->thaw( $frozen1, digest => [ "HMAC_SHA1", "secret" ] ) }; my $e = $@; ok( $foo1, "thawed" ); ok( !$e, "no error" ) || diag $e; - my $foo2 = eval { Foo->thaw( $frozen2, $d2 ) }; + my $foo2 = eval { Foo->thaw( $frozen2, digest => $d2 ) }; $e = $@; ok( $foo2, "thawed" ); ok( !$e, "no error" ) || diag $e; - $foo1 = eval { Foo->thaw( $frozen1, $d2 ) }; + $foo1 = eval { Foo->thaw( $frozen1, digest => $d2 ) }; $e = $@; ok( !$foo1, "not thawed" ); @@ -155,7 +155,7 @@ SKIP: { $frozen1 =~ s/foo/bar/; - $foo1 = eval { Foo->thaw( $frozen1, "HMAC_SHA1", "secret" ) }; + $foo1 = eval { Foo->thaw( $frozen1, digest => [ "HMAC_SHA1", "secret" ] ) }; $e = $@; ok( !$foo1, "not thawed" );