1 package MooseX::Storage::Deferred;
4 with 'MooseX::Storage::Basic';
7 my ( $self, $basename, $value, $method_name ) = @_;
9 my $role = MooseX::Storage->_expand_role($basename => $value)->meta;
10 my $method = $role->get_method($method_name)->body;
14 my ( $class, $packed, $type, @args ) = @_;
16 (exists $type->{format})
17 || confess "You must specify a format type to thaw from";
19 my $code = $class->__get_method(Format => $type->{format} => 'thaw');
21 $class->$code($packed, @args);
25 my ( $self, $type, @args ) = @_;
27 (exists $type->{format})
28 || confess "You must specify a format type to freeze into";
30 my $code = $self->__get_method(Format => $type->{format} => 'freeze');
36 my ( $class, $filename, $type, @args ) = @_;
39 || confess "You must specify an I/O type to load with";
41 my $code = $class->__get_method(IO => $type->{io} => 'load');
43 $class->$code($filename, $type, @args);
47 my ( $self, $filename, $type, @args ) = @_;
50 || confess "You must specify an I/O type to store with";
52 my $code = $self->__get_method(IO => $type->{io} => 'store');
54 $self->$code($filename, $type, @args);
67 MooseX::Storage::Deferred - A role for undecisive programmers
75 with 'MooseX::Storage::Deferred';
77 has 'x' => (is => 'rw', isa => 'Int');
78 has 'y' => (is => 'rw', isa => 'Int');
82 my $p = Point->new(x => 10, y => 10);
84 ## methods to freeze/thaw into
85 ## a specified serialization format
86 ## (in this case JSON)
88 # pack the class into a JSON string
89 $p->freeze({ format => 'JSON' }); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
91 # pack the class into a JSON string using parameterized JSONpm role
92 $p->freeze({ format => [ JSONpm => { json_opts => { pretty => 1 } } ] });
94 # unpack the JSON string into a class
96 '{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }',
102 This role is designed for those times when you need to
103 serialize into many different formats or I/O options.
105 It basically allows you to choose the format and IO
106 options only when you actually use them (see the
107 SYNOPSIS for more info)
109 =head1 SUPPORTED FORMATS
133 B<NOTE:> The B<StorableFile> I/O option is not supported,
134 this is because it does not mix well with options who also
135 have a C<thaw> and C<freeze> methods like this. It is possible
136 to probably work around this issue, but I don't currently
137 have the need for it. If you need this supported, talk to me
138 and I will see what I can do.
144 =item B<freeze ($type_desc)>
146 =item B<thaw ($data, $type_desc)>
148 =item B<load ($filename, $type_desc)>
150 =item B<store ($filename, $type_desc)>
164 All complex software has bugs lurking in it, and this module is no
165 exception. If you find a bug please either email me, or add the bug
170 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
172 =head1 COPYRIGHT AND LICENSE
174 Copyright 2007-2008 by Infinity Interactive, Inc.
176 L<http://www.iinteractive.com>
178 This library is free software; you can redistribute it and/or modify
179 it under the same terms as Perl itself.