fix bug
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Base / WithChecksum.pm
CommitLineData
c4a322ec 1
2package MooseX::Storage::Base::WithChecksum;
3use Moose::Role;
4
98ae09f0 5use Digest ();
34dcaa5d 6#use Storable ();
c4a322ec 7use MooseX::Storage::Engine;
8
34dcaa5d 9use Data::Dumper ();
10
c4a322ec 11our $VERSION = '0.01';
12
72a40e08 13our $DIGEST_MARKER = '__DIGEST__';
88651e76 14
c4a322ec 15sub pack {
98ae09f0 16 my ($self, @args ) = @_;
17
c4a322ec 18 my $e = MooseX::Storage::Engine->new( object => $self );
c4a322ec 19
a6ebb4c8 20 my $collapsed = $e->collapse_object(@args);
c4a322ec 21
88651e76 22 $collapsed->{$DIGEST_MARKER} = $self->_digest_packed($collapsed, @args);
c4a322ec 23
24 return $collapsed;
25}
26
27sub unpack {
98ae09f0 28 my ($class, $data, @args) = @_;
c4a322ec 29
30 # check checksum on data
31
34dcaa5d 32 my $old_checksum = delete $data->{$DIGEST_MARKER};
c4a322ec 33
98ae09f0 34 my $checksum = $class->_digest_packed($data, @args);
35
c4a322ec 36 ($checksum eq $old_checksum)
98ae09f0 37 || confess "Bad Checksum got=($checksum) expected=($old_checksum)";
c4a322ec 38
39 my $e = MooseX::Storage::Engine->new(class => $class);
a6ebb4c8 40 $class->new($e->expand_object($data, @args));
c4a322ec 41}
42
98ae09f0 43
44sub _digest_packed {
45 my ( $self, $collapsed, @args ) = @_;
46
a6ebb4c8 47 my $d = $self->_digest_object(@args);
48
49
50 {
51 local $Storable::canonical = 1;
34dcaa5d 52 local $Data::Dumper::Indent = 0;
53 local $Data::Dumper::Sortkeys = 1;
54
55 #Storable::nfreeze($collapsed);
56 $d->add( Data::Dumper::Dumper($collapsed) );
a6ebb4c8 57 }
58
59 return $d->hexdigest;
60}
61
62sub _digest_object {
63 my ( $self, %options ) = @_;
64 my $digest_opts = $options{digest};
65 $digest_opts = [ $digest_opts ] if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY';
66 my ( $d, @args ) = @$digest_opts;
98ae09f0 67
68 if ( ref $d ) {
69 if ( $d->can("clone") ) {
a6ebb4c8 70 return $d->clone;
98ae09f0 71 } elsif ( $d->can("reset") ) {
72 $d->reset;
a6ebb4c8 73 return $d;
98ae09f0 74 } else {
75 die "Can't clone or reset digest object: $d";
76 }
77 } else {
a6ebb4c8 78 return Digest->new($d || "SHA1", @args);
98ae09f0 79 }
98ae09f0 80}
81
c4a322ec 821;
83
84__END__
85
86=pod
87
88=head1 NAME
89
90MooseX::Storage::Base::WithChecksum
91
92=head1 SYNOPSIS
93
94=head1 DESCRIPTION
95
96=head1 METHODS
97
98=over 4
99
100=item B<pack (?$salt)>
101
102=item B<unpack ($data, ?$salt)>
103
104=back
105
106=head2 Introspection
107
108=over 4
109
110=item B<meta>
111
112=back
113
114=head1 BUGS
115
116All complex software has bugs lurking in it, and this module is no
117exception. If you find a bug please either email me, or add the bug
118to cpan-RT.
119
120=head1 AUTHOR
121
122Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
123
124=head1 COPYRIGHT AND LICENSE
125
126Copyright 2007 by Infinity Interactive, Inc.
127
128L<http://www.iinteractive.com>
129
130This library is free software; you can redistribute it and/or modify
131it under the same terms as Perl itself.
132
133=cut