Commit | Line | Data |
1f3074ea |
1 | package MooseX::Storage::Deferred; |
2 | use Moose::Role; |
3 | |
4 | our $VERSION = '0.03'; |
5 | our $AUTHORITY = 'cpan:STEVAN'; |
6 | |
7 | with 'MooseX::Storage::Basic'; |
8 | |
9 | sub thaw { |
10 | my ( $class, $packed, $type, @args ) = @_; |
11 | |
12 | (exists $type->{format}) |
13 | || confess "You must specify a format type to thaw from"; |
14 | |
15 | my $class_to_load = 'MooseX::Storage::Format::' . $type->{format}; |
16 | Class::MOP::load_class($class_to_load); |
17 | |
18 | my $method_to_call = $class_to_load . '::thaw'; |
19 | |
20 | $class->$method_to_call($packed, @args); |
21 | } |
22 | |
23 | sub freeze { |
24 | my ( $self, $type, @args ) = @_; |
25 | |
26 | (exists $type->{format}) |
27 | || confess "You must specify a format type to freeze into"; |
28 | |
29 | my $class_to_load = 'MooseX::Storage::Format::' . $type->{format}; |
30 | Class::MOP::load_class($class_to_load); |
31 | |
32 | my $method_to_call = $class_to_load . '::freeze'; |
33 | |
34 | $self->$method_to_call(@args); |
35 | } |
36 | |
37 | 1; |
38 | |
39 | __END__ |
40 | |
41 | =pod |
42 | |
43 | =head1 NAME |
44 | |
45 | MooseX::Storage::Deferred - A role for undecisive programmers |
46 | |
47 | =head1 SYNOPSIS |
48 | |
49 | package Point; |
50 | use Moose; |
51 | use MooseX::Storage; |
52 | |
53 | our $VERSION = '0.01'; |
54 | |
55 | with 'MooseX::Storage::Deferred'; |
56 | |
57 | has 'x' => (is => 'rw', isa => 'Int'); |
58 | has 'y' => (is => 'rw', isa => 'Int'); |
59 | |
60 | 1; |
61 | |
62 | my $p = Point->new(x => 10, y => 10); |
63 | |
64 | ## methods to freeze/thaw into |
65 | ## a specified serialization format |
66 | ## (in this case JSON) |
67 | |
68 | # pack the class into a JSON string |
69 | $p->freeze({ format => 'JSON' }); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 } |
70 | |
71 | # unpack the JSON string into a class |
72 | my $p2 = Point->thaw( |
73 | '{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }', |
74 | { format => 'JSON' } |
75 | ); |
76 | |
77 | =head1 DESCRIPTION |
78 | |
79 | This role is designed for those times when you need to |
80 | serialize into many different formats or I/O options. |
81 | It basically allows you to choose the format and IO |
82 | options only when you actually use them (see the |
83 | SYNOPSIS for more info) |
84 | |
85 | =head1 METHODS |
86 | |
87 | =over 4 |
88 | |
89 | =item B<freeze ($type_desc)> |
90 | |
91 | =item B<thaw ($data, $type_desc)> |
92 | |
93 | =back |
94 | |
95 | =head2 Introspection |
96 | |
97 | =over 4 |
98 | |
99 | =item B<meta> |
100 | |
101 | =back |
102 | |
103 | =head1 BUGS |
104 | |
105 | All complex software has bugs lurking in it, and this module is no |
106 | exception. If you find a bug please either email me, or add the bug |
107 | to cpan-RT. |
108 | |
109 | =head1 AUTHOR |
110 | |
111 | Stevan Little E<lt>stevan.little@iinteractive.comE<gt> |
112 | |
113 | =head1 COPYRIGHT AND LICENSE |
114 | |
115 | Copyright 2007-2008 by Infinity Interactive, Inc. |
116 | |
117 | L<http://www.iinteractive.com> |
118 | |
119 | This library is free software; you can redistribute it and/or modify |
120 | it under the same terms as Perl itself. |
121 | |
122 | =cut |