2 package MooseX::Storage;
5 use MooseX::Storage::Meta::Attribute::DoNotSerialize;
8 our $AUTHORITY = 'cpan:STEVAN';
13 return if $pkg eq 'main';
16 || confess "This package can only be used in Moose based classes";
18 $pkg->meta->add_method('Storage' => __PACKAGE__->meta->find_method_by_name('_injected_storage_role_generator'));
21 sub _injected_storage_role_generator {
24 if (exists $params{'base'}) {
25 $params{'base'} = ('Base::' . $params{'base'});
28 $params{'base'} = 'Basic';
32 ('MooseX::Storage::' . $params{'base'}),
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'};
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'}) {
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'};
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;
63 for my $role ( @roles ) {
64 Class::MOP::load_class($role) or die "Could not load role ($role)";
78 MooseX::Storage - A serialization framework for Moose classes
86 our $VERSION = '0.01';
88 with Storage('format' => 'JSON', 'io' => 'File');
90 has 'x' => (is => 'rw', isa => 'Int');
91 has 'y' => (is => 'rw', isa => 'Int');
95 my $p = Point->new(x => 10, y => 10);
97 ## methods to pack/unpack an
98 ## object in perl data structures
100 # pack the class into a hash
101 $p->pack(); # { __CLASS__ => 'Point-0.01', x => 10, y => 10 }
103 # unpack the hash into a class
104 my $p2 = Point->unpack({ __CLASS__ => 'Point-0.01', x => 10, y => 10 });
106 ## methods to freeze/thaw into
107 ## a specified serialization format
108 ## (in this case JSON)
110 # pack the class into a JSON string
111 $p->freeze(); # { "__CLASS__" : "Point-0.01", "x" : 10, "y" : 10 }
113 # unpack the JSON string into a class
114 my $p2 = Point->thaw('{ "__CLASS__" : "Point-0.01", "x" : 10, "y" : 10 }');
116 ## methods to load/store a class
117 ## on the file system
119 $p->store('my_point.json');
121 my $p2 = Point->load('my_point.json');
125 MooseX::Storage is a serialization framework for Moose, it provides
126 a very flexible and highly pluggable way to serialize Moose classes
127 to a number of different formats and styles.
129 =head2 Important Note
131 This is still an early release of this module, so use with caution.
132 It's outward facing serialization API should be considered stable,
133 but I still reserve the right to make tweaks if I need too. Anything
134 beyond the basic pack/unpack, freeze/thaw and load/store should not
137 =head2 Levels of Serialization
139 There are 3 levels to the serialization, each of which builds upon
140 the other and each of which can be customized to the specific needs
147 The first (base) level is C<pack> and C<unpack>. In this level the
148 class is serialized into a Perl HASH reference, it is tagged with the
149 class name and each instance attribute is stored. Very simple.
151 This level is not optional, it is the bare minumum that
152 MooseX::Storage provides and all other levels build on top of this.
156 The second (format) level is C<freeze> and C<thaw>. In this level the
157 output of C<pack> is sent to C<freeze> or the output of C<thaw> is sent
158 to C<unpack>. This levels primary role is to convert to and from the
159 specific serialization format and Perl land.
161 This level is optional, if you don't want/need it, you don't have to
162 have it. You can just use C<pack>/C<unpack> instead.
166 The third (io) level is C<load> and C<store>. In this level we are reading
167 and writing data to file/network/database/etc.
169 This level is also optional, in most cases it does require a C<format> role
170 to also be used, the expection being the C<StorableFile> role.
174 =head2 Behaviour modifiers
176 The serialization behaviour can be changed by supplying C<traits>.
177 This can be done as follows:
180 with Storage( traits => [Trait1, Trait2,...] );
182 The following traits are currently bundled with C<MooseX::Storage>:
188 Only attributes that have been built (ie, where the predicate returns
189 'true') will be serialized. This avoids any potentially expensive computations.
191 See L<MooseX::Storage::Traits::OnlyWhenBuilt> for details.
195 =head2 How we serialize
197 There are always limits to any serialization framework, there are just
198 some things which are really difficult to serialize properly and some
199 things which cannot be serialized at all.
201 =head2 What can be serialized?
203 Currently only numbers, string, ARRAY refs, HASH refs and other
204 MooseX::Storage enabled objects are supported.
206 With Array and Hash references the first level down is inspected and
207 any objects found are serialized/deserialized for you. We do not do
208 this recusively by default, however this feature may become an
211 The specific serialize/deserialize routine is determined by the
212 Moose type constraint a specific attribute has. In most cases subtypes
213 of the supported types are handled correctly, and there is a facility
214 for adding handlers for custom types as well. This will get documented
215 eventually, but it is currently still in development.
217 =head2 What can not be serialized?
219 We do not support CODE references yet, but this support might be added
220 in using B::Deparse or some other deep magic.
222 Scalar refs are not supported, mostly because there is no way to know
223 if the value being referenced will be there when the object is inflated.
224 I highly doubt will be ever support this in a general sense, but it
225 would be possible to add this yourself for a small specific case.
227 Circular references are specifically disallowed, however if you break
228 the cycles yourself then re-assemble them later you can get around this.
229 The reason we disallow circular refs is because they are not always supported
230 in all formats we use, and they tend to be very tricky to do for all
231 possible cases. It is almost always something you want to have tight control
236 This is B<not> a persistence framework, changes to your object after
237 you load or store it will not be reflected in the stored class.
243 =item B<Storage (%options)>
245 This module will export the C<Storage> method will can be used to
246 load a specific set of MooseX::Storage roles to implement a specific
247 combination of features. It is meant to make things easier, but it
248 is by no means the only way. You can still compose your roles by
271 This module needs docs and probably a Cookbook of some kind as well.
272 This is an early release, so that is my excuse for now :)
274 For the time being, please read the tests and feel free to email me
275 if you have any questions. This module can also be discussed on IRC
276 in the #moose channel on irc.perl.org.
280 All complex software has bugs lurking in it, and this module is no
281 exception. If you find a bug please either email me, or add the bug
286 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
288 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
290 Yuval Kogman E<lt>yuval.kogman@iinteractive.comE<gt>
292 =head1 COPYRIGHT AND LICENSE
294 Copyright 2007-2008 by Infinity Interactive, Inc.
296 L<http://www.iinteractive.com>
298 This library is free software; you can redistribute it and/or modify
299 it under the same terms as Perl itself.