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