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