1 package MooseX::Storage;
4 use MooseX::Storage::Meta::Attribute::DoNotSerialize;
5 use String::RewritePrefix ();
10 return if $pkg eq 'main';
13 || confess "This package can only be used in Moose based classes";
15 $pkg->meta->add_method('Storage' => __PACKAGE__->meta->find_method_by_name('_injected_storage_role_generator'));
18 my %HORRIBLE_GC_AVOIDANCE_HACK;
20 sub _rewrite_role_name {
21 my ($self, $base, $string) = @_;
23 my $role_name = scalar String::RewritePrefix->rewrite(
25 '' => "MooseX::Storage::$base\::",
33 my ($self, $base, $value) = @_;
35 return unless defined $value;
38 confess "too many args in arrayref role declaration" if @$value > 2;
39 my ($class, $param) = @$value;
41 $class = $self->_rewrite_role_name($base => $class);
42 Class::MOP::load_class($class);
44 my $role = $class->meta->generate_role(parameters => $param);
46 $HORRIBLE_GC_AVOIDANCE_HACK{ $role->name } = $role;
49 my $class = $self->_rewrite_role_name($base, $value);
50 Class::MOP::load_class($class);
54 if ($class->meta->isa(
55 'MooseX::Role::Parameterized::Meta::Role::Parameterizable'
57 $role = $class->meta->generate_role(parameters => undef);
58 $HORRIBLE_GC_AVOIDANCE_HACK{ $role->name } = $role;
66 sub _injected_storage_role_generator {
69 $params{base} = '=MooseX::Storage::Basic' unless defined $params{base};
71 my @roles = __PACKAGE__->_expand_role(Base => $params{base});
74 # you don't have to have a format
75 # role, this just means you dont
76 # get anything other than pack/unpack
77 push @roles, __PACKAGE__->_expand_role(Format => $params{format});
80 # many IO roles don't make sense unless
81 # you have also have a format role chosen
82 # too, the exception being StorableFile
85 # we dont need this code anymore, cause
86 # the role composition will catch it for
87 # us. This allows the StorableFile to work
88 #(exists $params{'format'})
89 # || confess "You must specify a format role in order to use an IO role";
90 push @roles, __PACKAGE__->_expand_role(IO => $params{io});
93 # These traits alter the behaviour of the engine, the user can
94 # specify these per role-usage
95 for my $trait ( @{ $params{'traits'} ||= [] } ) {
96 push @roles, __PACKAGE__->_expand_role(Traits => $trait);
110 MooseX::Storage - A serialization framework for Moose classes
118 with Storage('format' => 'JSON', 'io' => 'File');
120 has 'x' => (is => 'rw', isa => 'Int');
121 has 'y' => (is => 'rw', isa => 'Int');
125 my $p = Point->new(x => 10, y => 10);
127 ## methods to pack/unpack an
128 ## object in perl data structures
130 # pack the class into a hash
131 $p->pack(); # { __CLASS__ => 'Point-0.01', x => 10, y => 10 }
133 # unpack the hash into a class
134 my $p2 = Point->unpack({ __CLASS__ => 'Point-0.01', x => 10, y => 10 });
136 ## methods to freeze/thaw into
137 ## a specified serialization format
138 ## (in this case JSON)
140 # pack the class into a JSON string
141 $p->freeze(); # { "__CLASS__" : "Point-0.01", "x" : 10, "y" : 10 }
143 # unpack the JSON string into a class
144 my $p2 = Point->thaw('{ "__CLASS__" : "Point-0.01", "x" : 10, "y" : 10 }');
146 ## methods to load/store a class
147 ## on the file system
149 $p->store('my_point.json');
151 my $p2 = Point->load('my_point.json');
155 MooseX::Storage is a serialization framework for Moose, it provides
156 a very flexible and highly pluggable way to serialize Moose classes
157 to a number of different formats and styles.
159 =head2 Important Note
161 This is still an early release of this module, so use with caution.
162 It's outward facing serialization API should be considered stable,
163 but I still reserve the right to make tweaks if I need too. Anything
164 beyond the basic pack/unpack, freeze/thaw and load/store should not
167 =head2 Levels of Serialization
169 There are 3 levels to the serialization, each of which builds upon
170 the other and each of which can be customized to the specific needs
177 The first (base) level is C<pack> and C<unpack>. In this level the
178 class is serialized into a Perl HASH reference, it is tagged with the
179 class name and each instance attribute is stored. Very simple.
181 This level is not optional, it is the bare minumum that
182 MooseX::Storage provides and all other levels build on top of this.
184 See L<MooseX::Storage::Basic> for the fundamental implementation and
185 options to C<pack> and C<unpack>
189 The second (format) level is C<freeze> and C<thaw>. In this level the
190 output of C<pack> is sent to C<freeze> or the output of C<thaw> is sent
191 to C<unpack>. This levels primary role is to convert to and from the
192 specific serialization format and Perl land.
194 This level is optional, if you don't want/need it, you don't have to
195 have it. You can just use C<pack>/C<unpack> instead.
199 The third (io) level is C<load> and C<store>. In this level we are reading
200 and writing data to file/network/database/etc.
202 This level is also optional, in most cases it does require a C<format> role
203 to also be used, the exception being the C<StorableFile> role.
207 =head2 Behaviour modifiers
209 The serialization behaviour can be changed by supplying C<traits>.
210 This can be done as follows:
213 with Storage( traits => [Trait1, Trait2,...] );
215 The following traits are currently bundled with C<MooseX::Storage>:
221 Only attributes that have been built (ie, where the predicate returns
222 'true') will be serialized. This avoids any potentially expensive computations.
224 See L<MooseX::Storage::Traits::OnlyWhenBuilt> for details.
228 =head2 How we serialize
230 There are always limits to any serialization framework, there are just
231 some things which are really difficult to serialize properly and some
232 things which cannot be serialized at all.
234 =head2 What can be serialized?
236 Currently only numbers, string, ARRAY refs, HASH refs and other
237 MooseX::Storage enabled objects are supported.
239 With Array and Hash references the first level down is inspected and
240 any objects found are serialized/deserialized for you. We do not do
241 this recusively by default, however this feature may become an
244 The specific serialize/deserialize routine is determined by the
245 Moose type constraint a specific attribute has. In most cases subtypes
246 of the supported types are handled correctly, and there is a facility
247 for adding handlers for custom types as well. This will get documented
248 eventually, but it is currently still in development.
250 =head2 What can not be serialized?
252 We do not support CODE references yet, but this support might be added
253 in using B::Deparse or some other deep magic.
255 Scalar refs are not supported, mostly because there is no way to know
256 if the value being referenced will be there when the object is inflated.
257 I highly doubt will be ever support this in a general sense, but it
258 would be possible to add this yourself for a small specific case.
260 Circular references are specifically disallowed, however if you break
261 the cycles yourself then re-assemble them later you can get around this.
262 The reason we disallow circular refs is because they are not always supported
263 in all formats we use, and they tend to be very tricky to do for all
264 possible cases. It is almost always something you want to have tight control
269 This is B<not> a persistence framework; changes to your object after
270 you load or store it will not be reflected in the stored class.
276 =item B<Storage (%options)>
278 This module will export the C<Storage> method and can be used to
279 load a specific set of MooseX::Storage roles to implement a specific
280 combination of features. It is meant to make things easier, but it
281 is by no means the only way. You can still compose your roles by
284 By default, options are assumed to be short forms. For example, this:
286 Storage(format => 'JSON');
288 ...will result in looking for MooseX::Storage::Format::JSON. To use a role
289 that is not under the default namespace prefix, start with an equal sign:
291 Storage(format => '=My::Private::JSONFormat');
293 To use a parameterized role (for which, see L<MooseX::Role::Parameterized>) you
294 can pass an arrayref of the role name (in short or long form, as above) and its
297 Storage(format => [ JSONpm => { json_opts => { pretty => 1 } } ]);
319 This module needs docs and probably a Cookbook of some kind as well.
320 This is an early release, so that is my excuse for now :)
322 For the time being, please read the tests and feel free to email me
323 if you have any questions. This module can also be discussed on IRC
324 in the #moose channel on irc.perl.org.
328 All complex software has bugs lurking in it, and this module is no
329 exception. If you find a bug please either email me, or add the bug
334 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
336 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
338 Yuval Kogman E<lt>yuval.kogman@iinteractive.comE<gt>
340 =head1 COPYRIGHT AND LICENSE
342 Copyright 2007-2008 by Infinity Interactive, Inc.
344 L<http://www.iinteractive.com>
346 This library is free software; you can redistribute it and/or modify
347 it under the same terms as Perl itself.