496ca72e48e7258e1351be9ff0f0de440e3c16e1
[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 use Data::Dumper ();
10
11 our $VERSION = '0.01';
12
13 our $DIGEST_MARKER = '__DIGEST__';
14
15 sub pack {
16     my ($self, @args ) = @_;
17
18     my $e = MooseX::Storage::Engine->new( object => $self );
19
20     my $collapsed = $e->collapse_object(@args);
21     
22     $collapsed->{$DIGEST_MARKER} = $self->_digest_packed($collapsed, @args);
23     
24     return $collapsed;
25 }
26
27 sub unpack {
28     my ($class, $data, @args) = @_;
29
30     # check checksum on data
31     
32     my $old_checksum = delete $data->{$DIGEST_MARKER};
33     
34     my $checksum = $class->_digest_packed($data, @args);
35
36     ($checksum eq $old_checksum)
37         || confess "Bad Checksum got=($checksum) expected=($old_checksum)";    
38
39     my $e = MooseX::Storage::Engine->new(class => $class);
40     $class->new($e->expand_object($data, @args));
41 }
42
43
44 sub _digest_packed {
45     my ( $self, $collapsed, @args ) = @_;
46
47     my $d = $self->_digest_object(@args);
48
49
50     {
51         local $Storable::canonical = 1;
52         local $Data::Dumper::Indent = 0;
53         local $Data::Dumper::Sortkeys = 1;
54
55         #Storable::nfreeze($collapsed);
56         $d->add( Data::Dumper::Dumper($collapsed) );
57     }
58
59     return $d->hexdigest;
60 }
61
62 sub _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;
67
68     if ( ref $d ) {
69         if ( $d->can("clone") ) {
70             return $d->clone;
71         } elsif ( $d->can("reset") ) {
72             $d->reset;
73             return $d;
74         } else {
75             die "Can't clone or reset digest object: $d";
76         }
77     } else {
78         return Digest->new($d || "SHA1", @args);
79     }
80 }
81
82 1;
83
84 __END__
85
86 =pod
87
88 =head1 NAME
89
90 MooseX::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
116 All complex software has bugs lurking in it, and this module is no 
117 exception. If you find a bug please either email me, or add the bug
118 to cpan-RT.
119
120 =head1 AUTHOR
121
122 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
123
124 =head1 COPYRIGHT AND LICENSE
125
126 Copyright 2007 by Infinity Interactive, Inc.
127
128 L<http://www.iinteractive.com>
129
130 This library is free software; you can redistribute it and/or modify
131 it under the same terms as Perl itself.
132
133 =cut