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