File Coverage

blib/lib/MooseX/Storage/Deferred.pm
Criterion Covered Total %
statement 28 28 100.0
branch 4 8 50.0
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 44 48 91.6


line stmt bran cond sub pod time code
1             package MooseX::Storage::Deferred;
2             # ABSTRACT: A role for indecisive programmers
3              
4             our $VERSION = '0.53';
5              
6 2     2   7573 use Moose::Role;
  2         4  
  2         28  
7             with 'MooseX::Storage::Basic';
8 2     2   9854 use Carp 'confess';
  2         4  
  2         107  
9 2     2   10 use namespace::autoclean;
  2         3  
  2         17  
10              
11             sub __get_method {
12 14     14   37 my ( $self, $basename, $value, $method_name ) = @_;
13              
14 14         102 my $role = MooseX::Storage->_expand_role($basename => $value)->meta;
15 14         206 my $method = $role->get_method($method_name)->body;
16             }
17              
18             sub thaw {
19 5     5 1 19573 my ( $class, $packed, $type, @args ) = @_;
20              
21             (exists $type->{format})
22 5 50       24 || confess "You must specify a format type to thaw from";
23              
24 5         24 my $code = $class->__get_method(Format => $type->{format} => 'thaw');
25              
26 5         176 $class->$code($packed, @args);
27             }
28              
29             sub freeze {
30 5     5 1 51273 my ( $self, $type, @args ) = @_;
31              
32             (exists $type->{format})
33 5 50       22 || confess "You must specify a format type to freeze into";
34              
35 5         21 my $code = $self->__get_method(Format => $type->{format} => 'freeze');
36              
37 5         143 $self->$code(@args);
38             }
39              
40             sub load {
41 2     2 1 680 my ( $class, $filename, $type, @args ) = @_;
42              
43             (exists $type->{io})
44 2 50       8 || confess "You must specify an I/O type to load with";
45              
46 2         17 my $code = $class->__get_method(IO => $type->{io} => 'load');
47              
48 2         70 $class->$code($filename, $type, @args);
49             }
50              
51             sub store {
52 2     2 1 49386 my ( $self, $filename, $type, @args ) = @_;
53              
54             (exists $type->{io})
55 2 50       9 || confess "You must specify an I/O type to store with";
56              
57 2         10 my $code = $self->__get_method(IO => $type->{io} => 'store');
58              
59 2         59 $self->$code($filename, $type, @args);
60             }
61              
62             1;
63              
64             __END__
65              
66             =pod
67              
68             =encoding UTF-8
69              
70             =head1 NAME
71              
72             MooseX::Storage::Deferred - A role for indecisive programmers
73              
74             =head1 VERSION
75              
76             version 0.53
77              
78             =head1 SYNOPSIS
79              
80             package Point;
81             use Moose;
82             use MooseX::Storage;
83              
84             with 'MooseX::Storage::Deferred';
85              
86             has 'x' => (is => 'rw', isa => 'Int');
87             has 'y' => (is => 'rw', isa => 'Int');
88              
89             1;
90              
91             my $p = Point->new(x => 10, y => 10);
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
98             $p->freeze({ format => 'JSON' }); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
99              
100             # pack the class into a JSON string using parameterized JSONpm role
101             $p->freeze({ format => [ JSONpm => { json_opts => { pretty => 1 } } ] });
102              
103             # unpack the JSON string into a class
104             my $p2 = Point->thaw(
105             '{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }',
106             { format => 'JSON' }
107             );
108              
109             =head1 DESCRIPTION
110              
111             This role is designed for those times when you need to
112             serialize into many different formats or I/O options.
113              
114             It basically allows you to choose the format and IO
115             options only when you actually use them (see the
116             SYNOPSIS for more info)
117              
118             =head1 SUPPORTED FORMATS
119              
120             =over 4
121              
122             =item I<JSON>
123              
124             =for stopwords JSONpm
125              
126             =item I<JSONpm>
127              
128             =item I<YAML>
129              
130             =item I<Storable>
131              
132             =back
133              
134             =head1 SUPPORTED I/O
135              
136             =over 4
137              
138             =item I<File>
139              
140             =item I<AtomicFile>
141              
142             =back
143              
144             B<NOTE:> The B<StorableFile> I/O option is not supported,
145             this is because it does not mix well with options who also
146             have a C<thaw> and C<freeze> methods like this. It is possible
147             to probably work around this issue, but I don't currently
148             have the need for it. If you need this supported, talk to me
149             and I will see what I can do.
150              
151             =head1 METHODS
152              
153             =over 4
154              
155             =item B<freeze ($type_desc)>
156              
157             =item B<thaw ($data, $type_desc)>
158              
159             =item B<load ($filename, $type_desc)>
160              
161             =item B<store ($filename, $type_desc)>
162              
163             =back
164              
165             =head1 SUPPORT
166              
167             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Storage>
168             (or L<bug-MooseX-Storage@rt.cpan.org|mailto:bug-MooseX-Storage@rt.cpan.org>).
169              
170             There is also a mailing list available for users of this distribution, at
171             L<http://lists.perl.org/list/moose.html>.
172              
173             There is also an irc channel available for users of this distribution, at
174             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
175              
176             =head1 AUTHORS
177              
178             =over 4
179              
180             =item *
181              
182             Chris Prather <chris.prather@iinteractive.com>
183              
184             =item *
185              
186             Stevan Little <stevan.little@iinteractive.com>
187              
188             =item *
189              
190             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
191              
192             =back
193              
194             =head1 COPYRIGHT AND LICENSE
195              
196             This software is copyright (c) 2007 by Infinity Interactive, Inc.
197              
198             This is free software; you can redistribute it and/or modify it under
199             the same terms as the Perl 5 programming language system itself.
200              
201             =cut