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.50'; |
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
4197
|
use Moose::Role; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
29
|
|
7
|
|
|
|
|
|
|
with 'MooseX::Storage::Basic'; |
8
|
3
|
|
|
3
|
|
15543
|
use Carp 'confess'; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
171
|
|
9
|
3
|
|
|
3
|
|
16
|
use namespace::autoclean; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
30
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub __get_method { |
12
|
17
|
|
|
17
|
|
42
|
my ( $self, $basename, $value, $method_name ) = @_; |
13
|
|
|
|
|
|
|
|
14
|
17
|
|
|
|
|
129
|
my $role = MooseX::Storage->_expand_role($basename => $value)->meta; |
15
|
17
|
|
|
|
|
285
|
my $method = $role->get_method($method_name)->body; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub thaw { |
19
|
5
|
|
|
5
|
1
|
21387
|
my ( $class, $packed, $type, @args ) = @_; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
(exists $type->{format}) |
22
|
5
|
50
|
|
|
|
20
|
|| 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
|
|
|
|
|
137
|
$class->$code($packed, @args); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub freeze { |
30
|
8
|
|
|
8
|
1
|
72615
|
my ( $self, $type, @args ) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
(exists $type->{format}) |
33
|
8
|
50
|
|
|
|
45
|
|| confess "You must specify a format type to freeze into"; |
34
|
|
|
|
|
|
|
|
35
|
8
|
|
|
|
|
37
|
my $code = $self->__get_method(Format => $type->{format} => 'freeze'); |
36
|
|
|
|
|
|
|
|
37
|
8
|
|
|
|
|
267
|
$self->$code(@args); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub load { |
41
|
2
|
|
|
2
|
1
|
757
|
my ( $class, $filename, $type, @args ) = @_; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
(exists $type->{io}) |
44
|
2
|
50
|
|
|
|
9
|
|| confess "You must specify an I/O type to load with"; |
45
|
|
|
|
|
|
|
|
46
|
2
|
|
|
|
|
9
|
my $code = $class->__get_method(IO => $type->{io} => 'load'); |
47
|
|
|
|
|
|
|
|
48
|
2
|
|
|
|
|
57
|
$class->$code($filename, $type, @args); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub store { |
52
|
2
|
|
|
2
|
1
|
48833
|
my ( $self, $filename, $type, @args ) = @_; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
(exists $type->{io}) |
55
|
2
|
50
|
|
|
|
14
|
|| confess "You must specify an I/O type to store with"; |
56
|
|
|
|
|
|
|
|
57
|
2
|
|
|
|
|
12
|
my $code = $self->__get_method(IO => $type->{io} => 'store'); |
58
|
|
|
|
|
|
|
|
59
|
2
|
|
|
|
|
60
|
$self->$code($filename, $type, @args); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
3
|
|
|
3
|
|
1124
|
no Moose::Role; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
15
|
|
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.50 |
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
|
|
|
|
|
|
|
=head2 Introspection |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=over 4 |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=item B<meta> |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=back |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 BUGS |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
All complex software has bugs lurking in it, and this module is no |
178
|
|
|
|
|
|
|
exception. If you find a bug please or add the bug to cpan-RT |
179
|
|
|
|
|
|
|
at L<https://rt.cpan.org/Dist/Display.html?Queue=MooseX-Storage>. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 AUTHORS |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=over 4 |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=item * |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Chris Prather <chris.prather@iinteractive.com> |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=item * |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Stevan Little <stevan.little@iinteractive.com> |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=item * |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
×××× ×§××'×× (Yuval Kogman) <nothingmuch@woobling.org> |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=back |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
This software is copyright (c) 2007 by Infinity Interactive, Inc.. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
204
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=cut |