File Coverage

blib/lib/MooseX/Storage/Deferred.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 8 50.0
condition n/a
subroutine 9 9 100.0
pod 4 4 100.0
total 48 52 92.3


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