Version 0.32
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Deferred.pm
1 package MooseX::Storage::Deferred;
2 use Moose::Role;
3
4 our $VERSION   = '0.32';
5 our $AUTHORITY = 'cpan:STEVAN';
6
7 with 'MooseX::Storage::Basic';
8
9 sub __get_method {
10     my ( $self, $basename, $value, $method_name ) = @_;
11
12     my $role   = MooseX::Storage->_expand_role($basename => $value)->meta;
13     my $method = $role->get_method($method_name)->body;
14 }
15
16 sub thaw {
17     my ( $class, $packed, $type, @args ) = @_;
18
19     (exists $type->{format})
20         || confess "You must specify a format type to thaw from";
21
22     my $code = $class->__get_method(Format => $type->{format} => 'thaw');
23
24     $class->$code($packed, @args);
25 }
26
27 sub freeze {
28     my ( $self, $type, @args ) = @_;
29
30     (exists $type->{format})
31         || confess "You must specify a format type to freeze into";
32
33     my $code = $self->__get_method(Format => $type->{format} => 'freeze');
34
35     $self->$code(@args);
36 }
37
38 sub load {
39     my ( $class, $filename, $type, @args ) = @_;
40
41     (exists $type->{io})
42         || confess "You must specify an I/O type to load with";
43
44     my $code = $class->__get_method(IO => $type->{io} => 'load');
45
46     $class->$code($filename, $type, @args);
47 }
48
49 sub store {
50     my ( $self, $filename, $type, @args ) = @_;
51
52     (exists $type->{io})
53         || confess "You must specify an I/O type to store with";
54
55     my $code = $self->__get_method(IO => $type->{io} => 'store');
56
57     $self->$code($filename, $type, @args);
58 }
59
60 no Moose::Role;
61
62 1;
63
64 __END__
65
66 =pod
67
68 =head1 NAME
69
70 MooseX::Storage::Deferred - A role for undecisive programmers
71
72 =head1 SYNOPSIS
73
74   package Point;
75   use Moose;
76   use MooseX::Storage;
77
78   our $VERSION = '0.01';
79
80   with 'MooseX::Storage::Deferred';
81
82   has 'x' => (is => 'rw', isa => 'Int');
83   has 'y' => (is => 'rw', isa => 'Int');
84
85   1;
86
87   my $p = Point->new(x => 10, y => 10);
88
89   ## methods to freeze/thaw into
90   ## a specified serialization format
91   ## (in this case JSON)
92
93   # pack the class into a JSON string
94   $p->freeze({ format => 'JSON' }); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
95
96   # pack the class into a JSON string using parameterized JSONpm role
97   $p->freeze({ format => [ JSONpm => { json_opts => { pretty => 1 } } ] });
98
99   # unpack the JSON string into a class
100   my $p2 = Point->thaw(
101       '{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }',
102       { format => 'JSON' }
103   );
104
105 =head1 DESCRIPTION
106
107 This role is designed for those times when you need to
108 serialize into many different formats or I/O options.
109
110 It basically allows you to choose the format and IO
111 options only when you actually use them (see the
112 SYNOPSIS for more info)
113
114 =head1 SUPPORTED FORMATS
115
116 =over 4
117
118 =item I<JSON>
119
120 =item I<JSONpm>
121
122 =item I<YAML>
123
124 =item I<Storable>
125
126 =back
127
128 =head1 SUPPORTED I/O
129
130 =over 4
131
132 =item I<File>
133
134 =item I<AtomicFile>
135
136 =back
137
138 B<NOTE:> The B<StorableFile> I/O option is not supported,
139 this is because it does not mix well with options who also
140 have a C<thaw> and C<freeze> methods like this. It is possible
141 to probably work around this issue, but I don't currently
142 have the need for it. If you need this supported, talk to me
143 and I will see what I can do.
144
145 =head1 METHODS
146
147 =over 4
148
149 =item B<freeze ($type_desc)>
150
151 =item B<thaw ($data, $type_desc)>
152
153 =item B<load ($filename, $type_desc)>
154
155 =item B<store ($filename, $type_desc)>
156
157 =back
158
159 =head2 Introspection
160
161 =over 4
162
163 =item B<meta>
164
165 =back
166
167 =head1 BUGS
168
169 All complex software has bugs lurking in it, and this module is no
170 exception. If you find a bug please either email me, or add the bug
171 to cpan-RT.
172
173 =head1 AUTHOR
174
175 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
176
177 =head1 COPYRIGHT AND LICENSE
178
179 Copyright 2007-2008 by Infinity Interactive, Inc.
180
181 L<http://www.iinteractive.com>
182
183 This library is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself.
185
186 =cut