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