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