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);
75 MooseX::Storage::Deferred - A role for undecisive programmers
83 our $VERSION = '0.01';
85 with 'MooseX::Storage::Deferred';
87 has 'x' => (is => 'rw', isa => 'Int');
88 has 'y' => (is => 'rw', isa => 'Int');
92 my $p = Point->new(x => 10, y => 10);
94 ## methods to freeze/thaw into
95 ## a specified serialization format
96 ## (in this case JSON)
98 # pack the class into a JSON string
99 $p->freeze({ format => 'JSON' }); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
101 # unpack the JSON string into a class
102 my $p2 = Point->thaw(
103 '{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }',
109 This role is designed for those times when you need to
110 serialize into many different formats or I/O options.
112 It basically allows you to choose the format and IO
113 options only when you actually use them (see the
114 SYNOPSIS for more info)
116 =head1 SUPPORTED FORMATS
138 B<NOTE:> The B<StorableFile> I/O option is not supported,
139 this is because it does not mix well with options who also
140 have a C<thaw> and C<freeze> methods like this. It is possible
141 to probably work around this issue, but I don't currently
142 have the need for it. If you need this supported, talk to me
143 and I will see what I can do.
149 =item B<freeze ($type_desc)>
151 =item B<thaw ($data, $type_desc)>
153 =item B<load ($filename, $type_desc)>
155 =item B<store ($filename, $type_desc)>
169 All complex software has bugs lurking in it, and this module is no
170 exception. If you find a bug please either email me, or add the bug
175 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
177 =head1 COPYRIGHT AND LICENSE
179 Copyright 2007-2008 by Infinity Interactive, Inc.
181 L<http://www.iinteractive.com>
183 This library is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself.