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