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