Commit | Line | Data |
69ac22ee |
1 | package DBIx::Class::Serialize::Storable; |
ed28f830 |
2 | use strict; |
bf5ecff9 |
3 | use warnings; |
69ac22ee |
4 | use Storable; |
ed28f830 |
5 | |
6 | sub STORABLE_freeze { |
e60dc79f |
7 | my ($self, $cloning) = @_; |
ed28f830 |
8 | my $to_serialize = { %$self }; |
e60dc79f |
9 | |
7cfda9a6 |
10 | # The source is either derived from _source_handle or is |
11 | # reattached in the thaw handler below |
ed28f830 |
12 | delete $to_serialize->{result_source}; |
7cfda9a6 |
13 | |
7cfda9a6 |
14 | # Dynamic values, easy to recalculate |
15 | delete $to_serialize->{$_} for qw/related_resultsets _inflated_column/; |
e60dc79f |
16 | |
7244b45f |
17 | return (Storable::freeze($to_serialize)); |
ed28f830 |
18 | } |
19 | |
20 | sub STORABLE_thaw { |
7244b45f |
21 | my ($self, $cloning, $serialized) = @_; |
e60dc79f |
22 | |
7244b45f |
23 | %$self = %{ Storable::thaw($serialized) }; |
7cfda9a6 |
24 | |
25 | # if the handle went missing somehow, reattach |
24d67825 |
26 | $self->result_source($self->result_source_instance) |
7cfda9a6 |
27 | if !$self->_source_handle && $self->can('result_source_instance'); |
ed28f830 |
28 | } |
29 | |
19345968 |
30 | 1; |
31 | |
32 | __END__ |
33 | |
75d07914 |
34 | =head1 NAME |
19345968 |
35 | |
24d67825 |
36 | DBIx::Class::Serialize::Storable - hooks for Storable freeze/thaw |
19345968 |
37 | |
38 | =head1 SYNOPSIS |
39 | |
40 | # in a table class definition |
69ac22ee |
41 | __PACKAGE__->load_components(qw/Serialize::Storable/); |
9b83fccd |
42 | |
19345968 |
43 | # meanwhile, in a nearby piece of code |
24d67825 |
44 | my $cd = $schema->resultset('CD')->find(12); |
2053ab2a |
45 | # if the cache uses Storable, this will work automatically |
46 | $cache->set($cd->ID, $cd); |
19345968 |
47 | |
48 | =head1 DESCRIPTION |
49 | |
24d67825 |
50 | This component adds hooks for Storable so that row objects can be |
51 | serialized. It assumes that your row object class (C<result_class>) is |
2053ab2a |
52 | the same as your table class, which is the normal situation. |
19345968 |
53 | |
9b83fccd |
54 | =head1 HOOKS |
55 | |
56 | The following hooks are defined for L<Storable> - see the |
57 | documentation for L<Storable/Hooks> for detailed information on these |
58 | hooks. |
59 | |
60 | =head2 STORABLE_freeze |
61 | |
62 | The serializing hook, called on the object during serialization. It |
63 | can be inherited, or defined in the class itself, like any other |
64 | method. |
65 | |
66 | =head2 STORABLE_thaw |
67 | |
68 | The deserializing hook called on the object during deserialization. |
69 | |
19345968 |
70 | =head1 AUTHORS |
71 | |
72 | David Kamholz <dkamholz@cpan.org> |
73 | |
74 | =head1 LICENSE |
75 | |
76 | You may distribute this code under the same terms as Perl itself. |
77 | |
78 | =cut |