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