* fix a bug where if a required constructor argument was not serialized, it was
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage.pm
CommitLineData
e59193fb 1
e59193fb 2package MooseX::Storage;
ec9c1923 3use Moose qw(confess);
e59193fb 4
eebcb6dc 5use MooseX::Storage::Meta::Attribute::DoNotSerialize;
6
5ca52230 7our $VERSION = '0.18';
69b45b7d 8our $AUTHORITY = 'cpan:STEVAN';
7b428d1f 9
e59193fb 10sub import {
11 my $pkg = caller();
ec9c1923 12
13 return if $pkg eq 'main';
14
15 ($pkg->can('meta'))
16 || confess "This package can only be used in Moose based classes";
17
f9143059 18 $pkg->meta->add_method('Storage' => __PACKAGE__->meta->find_method_by_name('_injected_storage_role_generator'));
19}
20
21sub _injected_storage_role_generator {
22 my %params = @_;
4d1850a6 23
f9143059 24 if (exists $params{'base'}) {
25 $params{'base'} = ('Base::' . $params{'base'});
26 }
27 else {
28 $params{'base'} = 'Basic';
29 }
ec9c1923 30
f9143059 31 my @roles = (
32 ('MooseX::Storage::' . $params{'base'}),
33 );
4d1850a6 34
f9143059 35 # NOTE:
36 # you don't have to have a format
37 # role, this just means you dont
38 # get anything other than pack/unpack
39 push @roles => 'MooseX::Storage::Format::' . $params{'format'}
40 if exists $params{'format'};
ec9c1923 41
f9143059 42 # NOTE:
43 # many IO roles don't make sense unless
44 # you have also have a format role chosen
45 # too, the exception being StorableFile
46 if (exists $params{'io'}) {
ec9c1923 47 # NOTE:
f9143059 48 # we dont need this code anymore, cause
49 # the role composition will catch it for
50 # us. This allows the StorableFile to work
51 #(exists $params{'format'})
52 # || confess "You must specify a format role in order to use an IO role";
53 push @roles => 'MooseX::Storage::IO::' . $params{'io'};
54 }
76218b46 55
56 # Note:
57 # These traits alter the behaviour of the engine, the user can
58 # specify these per role-usage
59 for my $trait ( @{ $params{'traits'} ||= [] } ) {
60 push @roles, 'MooseX::Storage::Traits::'.$trait;
61 }
4d1850a6 62
f9143059 63 Class::MOP::load_class($_)
64 || die "Could not load role (" . $_ . ")"
65 foreach @roles;
ec9c1923 66
f9143059 67 return @roles;
e59193fb 68}
69
ec9c1923 701;
e59193fb 71
ec9c1923 72__END__
e59193fb 73
ec9c1923 74=pod
e59193fb 75
ec9c1923 76=head1 NAME
e9739624 77
d109f069 78MooseX::Storage - A serialization framework for Moose classes
e59193fb 79
ec9c1923 80=head1 SYNOPSIS
e9739624 81
1390c23d 82 package Point;
83 use Moose;
84 use MooseX::Storage;
85
c1830046 86 our $VERSION = '0.01';
87
1390c23d 88 with Storage('format' => 'JSON', 'io' => 'File');
89
90 has 'x' => (is => 'rw', isa => 'Int');
91 has 'y' => (is => 'rw', isa => 'Int');
92
93 1;
94
95 my $p = Point->new(x => 10, y => 10);
96
97 ## methods to pack/unpack an
98 ## object in perl data structures
99
100 # pack the class into a hash
c1830046 101 $p->pack(); # { __CLASS__ => 'Point-0.01', x => 10, y => 10 }
1390c23d 102
103 # unpack the hash into a class
c1830046 104 my $p2 = Point->unpack({ __CLASS__ => 'Point-0.01', x => 10, y => 10 });
1390c23d 105
106 ## methods to freeze/thaw into
107 ## a specified serialization format
108 ## (in this case JSON)
109
110 # pack the class into a JSON string
c1830046 111 $p->freeze(); # { "__CLASS__" : "Point-0.01", "x" : 10, "y" : 10 }
1390c23d 112
113 # unpack the JSON string into a class
c1830046 114 my $p2 = Point->thaw('{ "__CLASS__" : "Point-0.01", "x" : 10, "y" : 10 }');
1390c23d 115
116 ## methods to load/store a class
117 ## on the file system
118
119 $p->store('my_point.json');
120
121 my $p2 = Point->load('my_point.json');
122
ec9c1923 123=head1 DESCRIPTION
124
1390c23d 125MooseX::Storage is a serialization framework for Moose, it provides
126a very flexible and highly pluggable way to serialize Moose classes
127to a number of different formats and styles.
128
7b428d1f 129=head2 Important Note
130
131This is still an early release of this module, so use with caution.
132It's outward facing serialization API should be considered stable,
133but I still reserve the right to make tweaks if I need too. Anything
134beyond the basic pack/unpack, freeze/thaw and load/store should not
135be relied on.
136
1390c23d 137=head2 Levels of Serialization
138
139There are 3 levels to the serialization, each of which builds upon
140the other and each of which can be customized to the specific needs
141of your class.
142
143=over 4
144
145=item B<base>
146
147The first (base) level is C<pack> and C<unpack>. In this level the
148class is serialized into a Perl HASH reference, it is tagged with the
149class name and each instance attribute is stored. Very simple.
150
151This level is not optional, it is the bare minumum that
152MooseX::Storage provides and all other levels build on top of this.
153
154=item B<format>
155
156The second (format) level is C<freeze> and C<thaw>. In this level the
157output of C<pack> is sent to C<freeze> or the output of C<thaw> is sent
158to C<unpack>. This levels primary role is to convert to and from the
159specific serialization format and Perl land.
160
161This level is optional, if you don't want/need it, you don't have to
162have it. You can just use C<pack>/C<unpack> instead.
163
164=item B<io>
165
166The third (io) level is C<load> and C<store>. In this level we are reading
167and writing data to file/network/database/etc.
168
124c2ba5 169This level is also optional, in most cases it does require a C<format> role
170to also be used, the expection being the C<StorableFile> role.
1390c23d 171
172=back
173
76218b46 174=head2 Behaviour modifiers
175
176The serialization behaviour can be changed by supplying C<traits>.
177This can be done as follows:
178
179 use MooseX::Storage;
180 with Storage( traits => [Trait1, Trait2,...] );
181
182The following traits are currently bundled with C<MooseX::Storage>:
183
184=over 4
185
186=item OnlyWhenBuilt
187
188Only attributes that have been built (ie, where the predicate returns
189'true') will be serialized. This avoids any potentially expensive computations.
190
191See L<MooseX::Storage::Traits::OnlyWhenBuilt> for details.
192
193=back
194
1390c23d 195=head2 How we serialize
196
197There are always limits to any serialization framework, there are just
198some things which are really difficult to serialize properly and some
199things which cannot be serialized at all.
200
201=head2 What can be serialized?
202
203Currently only numbers, string, ARRAY refs, HASH refs and other
204MooseX::Storage enabled objects are supported.
205
206With Array and Hash references the first level down is inspected and
207any objects found are serialized/deserialized for you. We do not do
208this recusively by default, however this feature may become an
209option eventually.
210
211The specific serialize/deserialize routine is determined by the
212Moose type constraint a specific attribute has. In most cases subtypes
213of the supported types are handled correctly, and there is a facility
214for adding handlers for custom types as well. This will get documented
215eventually, but it is currently still in development.
216
217=head2 What can not be serialized?
218
219We do not support CODE references yet, but this support might be added
220in using B::Deparse or some other deep magic.
221
222Scalar refs are not supported, mostly because there is no way to know
223if the value being referenced will be there when the object is inflated.
224I highly doubt will be ever support this in a general sense, but it
225would be possible to add this yourself for a small specific case.
226
227Circular references are specifically disallowed, however if you break
228the cycles yourself then re-assemble them later you can get around this.
229The reason we disallow circular refs is because they are not always supported
230in all formats we use, and they tend to be very tricky to do for all
231possible cases. It is almost always something you want to have tight control
232over anyway.
233
234=head1 CAVEAT
235
236This is B<not> a persistence framework, changes to your object after
237you load or store it will not be reflected in the stored class.
238
239=head1 EXPORTS
240
241=over 4
242
243=item B<Storage (%options)>
244
245This module will export the C<Storage> method will can be used to
246load a specific set of MooseX::Storage roles to implement a specific
247combination of features. It is meant to make things easier, but it
248is by no means the only way. You can still compose your roles by
249hand if you like.
250
251=back
252
ec9c1923 253=head1 METHODS
254
255=over 4
256
257=item B<import>
258
259=back
260
261=head2 Introspection
262
263=over 4
264
265=item B<meta>
266
267=back
268
1390c23d 269=head1 TODO
270
7b428d1f 271This module needs docs and probably a Cookbook of some kind as well.
272This is an early release, so that is my excuse for now :)
1390c23d 273
274For the time being, please read the tests and feel free to email me
275if you have any questions. This module can also be discussed on IRC
276in the #moose channel on irc.perl.org.
277
ec9c1923 278=head1 BUGS
279
280All complex software has bugs lurking in it, and this module is no
281exception. If you find a bug please either email me, or add the bug
282to cpan-RT.
283
284=head1 AUTHOR
285
286Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
287
288Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
289
6c9f2c85 290Yuval Kogman E<lt>yuval.kogman@iinteractive.comE<gt>
291
ec9c1923 292=head1 COPYRIGHT AND LICENSE
293
1f3074ea 294Copyright 2007-2008 by Infinity Interactive, Inc.
ec9c1923 295
296L<http://www.iinteractive.com>
297
298This library is free software; you can redistribute it and/or modify
299it under the same terms as Perl itself.
e9739624 300
301=cut