From: Jos Boumans Date: Wed, 24 Jun 2009 12:13:10 +0000 (+0200) Subject: * move Base::WithChecksum to build upon Basic.pm, avoid code duplication X-Git-Tag: 0.20~15 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=298cda9863d1e75bd897cef144c86046aa16c430;p=gitmo%2FMooseX-Storage.git * move Base::WithChecksum to build upon Basic.pm, avoid code duplication --- diff --git a/lib/MooseX/Storage/Base/WithChecksum.pm b/lib/MooseX/Storage/Base/WithChecksum.pm index d7a888d..285e3e0 100644 --- a/lib/MooseX/Storage/Base/WithChecksum.pm +++ b/lib/MooseX/Storage/Base/WithChecksum.pm @@ -1,34 +1,32 @@ - package MooseX::Storage::Base::WithChecksum; use Moose::Role; +with 'MooseX::Storage::Basic'; + use Digest (); use Data::Dumper (); -use MooseX::Storage::Engine; - our $VERSION = '0.18'; our $AUTHORITY = 'cpan:STEVAN'; our $DIGEST_MARKER = '__DIGEST__'; -sub pack { - my ($self, @args ) = @_; +around pack => sub { + my $orig = shift; + my $self = shift; + my @args = @_; - my $e = MooseX::Storage::Engine->new( object => $self ); + my $collapsed = $self->$orig( @args ); - my $collapsed = $e->collapse_object(@args); - $collapsed->{$DIGEST_MARKER} = $self->_digest_packed($collapsed, @args); return $collapsed; -} +}; -sub unpack { - my ($class, $data, @args) = @_; +around unpack => sub { + my ($orig, $class, $data, @args) = @_; # check checksum on data - my $old_checksum = delete $data->{$DIGEST_MARKER}; my $checksum = $class->_digest_packed($data, @args); @@ -36,9 +34,8 @@ sub unpack { ($checksum eq $old_checksum) || confess "Bad Checksum got=($checksum) expected=($old_checksum)"; - my $e = MooseX::Storage::Engine->new(class => $class); - $class->new($e->expand_object($data, @args)); -} + $class->$orig( $data, @args ); +}; sub _digest_packed { diff --git a/lib/MooseX/Storage/Basic.pm b/lib/MooseX/Storage/Basic.pm index 8651c49..01e66dd 100644 --- a/lib/MooseX/Storage/Basic.pm +++ b/lib/MooseX/Storage/Basic.pm @@ -1,4 +1,3 @@ - package MooseX::Storage::Basic; use Moose::Role; @@ -9,16 +8,36 @@ 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; __END__