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