1 package MooseX::Storage::Deferred;
5 our $AUTHORITY = 'cpan:STEVAN';
7 with 'MooseX::Storage::Basic';
10 my ( $class, $packed, $type, @args ) = @_;
12 (exists $type->{format})
13 || confess "You must specify a format type to thaw from";
15 my $class_to_load = 'MooseX::Storage::Format::' . $type->{format};
16 Class::MOP::load_class($class_to_load);
18 my $method_to_call = $class_to_load . '::thaw';
20 $class->$method_to_call($packed, @args);
24 my ( $self, $type, @args ) = @_;
26 (exists $type->{format})
27 || confess "You must specify a format type to freeze into";
29 my $class_to_load = 'MooseX::Storage::Format::' . $type->{format};
30 Class::MOP::load_class($class_to_load);
32 my $method_to_call = $class_to_load . '::freeze';
34 $self->$method_to_call(@args);
38 my ( $class, $filename, $type, @args ) = @_;
41 || confess "You must specify an I/O type to load with";
43 my $class_to_load = 'MooseX::Storage::IO::' . $type->{io};
44 Class::MOP::load_class($class_to_load);
46 my $method_to_call = $class_to_load . '::load';
48 $class->$method_to_call($filename, $type, @args);
52 my ( $self, $filename, $type, @args ) = @_;
55 || confess "You must specify an I/O type to store with";
57 my $class_to_load = 'MooseX::Storage::IO::' . $type->{io};
58 Class::MOP::load_class($class_to_load);
60 my $method_to_call = $class_to_load . '::store';
62 $self->$method_to_call($filename, $type, @args);
73 MooseX::Storage::Deferred - A role for undecisive programmers
81 our $VERSION = '0.01';
83 with 'MooseX::Storage::Deferred';
85 has 'x' => (is => 'rw', isa => 'Int');
86 has 'y' => (is => 'rw', isa => 'Int');
90 my $p = Point->new(x => 10, y => 10);
92 ## methods to freeze/thaw into
93 ## a specified serialization format
94 ## (in this case JSON)
96 # pack the class into a JSON string
97 $p->freeze({ format => 'JSON' }); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
99 # unpack the JSON string into a class
100 my $p2 = Point->thaw(
101 '{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }',
107 This role is designed for those times when you need to
108 serialize into many different formats or I/O options.
110 It basically allows you to choose the format and IO
111 options only when you actually use them (see the
112 SYNOPSIS for more info)
114 =head1 SUPPORTED FORMATS
136 B<NOTE:> The B<StorableFile> I/O option is not supported,
137 this is because it does not mix well with options who also
138 have a C<thaw> and C<freeze> methods like this. It is possible
139 to probably work around this issue, but I don't currently
140 have the need for it. If you need this supported, talk to me
141 and I will see what I can do.
147 =item B<freeze ($type_desc)>
149 =item B<thaw ($data, $type_desc)>
151 =item B<load ($filename, $type_desc)>
153 =item B<store ($filename, $type_desc)>
167 All complex software has bugs lurking in it, and this module is no
168 exception. If you find a bug please either email me, or add the bug
173 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
175 =head1 COPYRIGHT AND LICENSE
177 Copyright 2007-2008 by Infinity Interactive, Inc.
179 L<http://www.iinteractive.com>
181 This library is free software; you can redistribute it and/or modify
182 it under the same terms as Perl itself.