Commit | Line | Data |
ec9c1923 |
1 | package MooseX::Storage::Basic; |
2 | use Moose::Role; |
3 | |
4 | use MooseX::Storage::Engine; |
5 | |
5ca52230 |
6 | our $VERSION = '0.18'; |
69b45b7d |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
7b428d1f |
8 | |
ec9c1923 |
9 | sub pack { |
a6ebb4c8 |
10 | my ( $self, @args ) = @_; |
298cda98 |
11 | my $e = $self->_storage_get_engine( object => $self ); |
a6ebb4c8 |
12 | $e->collapse_object(@args); |
ec9c1923 |
13 | } |
14 | |
15 | sub unpack { |
298cda98 |
16 | my ($class, $data, %args) = @_; |
17 | my $e = $class->_storage_get_engine(class => $class); |
18 | |
19 | $class->_storage_construct_instance( |
20 | [ $e->expand_object($data, %args) ], |
21 | \%args |
22 | ); |
23 | } |
24 | |
25 | sub _storage_get_engine { |
26 | my $self = shift; |
27 | MooseX::Storage::Engine->new( @_ ); |
28 | } |
29 | |
30 | sub _storage_construct_instance { |
31 | my ($class, $args, $opts) = @_; |
32 | |
33 | my @i = defined $opts->{'inject'} ? @{ $opts->{'inject'} } : (); |
34 | |
35 | $class->new( @$args, @i ); |
ec9c1923 |
36 | } |
37 | |
298cda98 |
38 | |
39 | |
40 | |
ec9c1923 |
41 | 1; |
42 | |
43 | __END__ |
44 | |
45 | =pod |
46 | |
47 | =head1 NAME |
48 | |
1390c23d |
49 | MooseX::Storage::Basic - The simplest level of serialization |
ec9c1923 |
50 | |
51 | =head1 SYNOPSIS |
52 | |
1390c23d |
53 | package Point; |
54 | use Moose; |
55 | use MooseX::Storage; |
56 | |
c1830046 |
57 | our $VERSION = '0.01'; |
58 | |
1390c23d |
59 | with Storage; |
60 | |
61 | has 'x' => (is => 'rw', isa => 'Int'); |
62 | has 'y' => (is => 'rw', isa => 'Int'); |
63 | |
64 | 1; |
65 | |
66 | my $p = Point->new(x => 10, y => 10); |
67 | |
68 | ## methods to pack/unpack an |
69 | ## object in perl data structures |
70 | |
71 | # pack the class into a hash |
c1830046 |
72 | $p->pack(); # { __CLASS__ => 'Point-0.01', x => 10, y => 10 } |
1390c23d |
73 | |
74 | # unpack the hash into a class |
c1830046 |
75 | my $p2 = Point->unpack({ __CLASS__ => 'Point-0.01', x => 10, y => 10 }); |
1390c23d |
76 | |
ec9c1923 |
77 | =head1 DESCRIPTION |
78 | |
1390c23d |
79 | This is the most basic form of serialization. This is used by default |
80 | but the exported C<Storage> function. |
81 | |
ec9c1923 |
82 | =head1 METHODS |
83 | |
84 | =over 4 |
85 | |
86 | =item B<pack> |
87 | |
88 | =item B<unpack ($data)> |
89 | |
90 | =back |
91 | |
92 | =head2 Introspection |
93 | |
94 | =over 4 |
95 | |
96 | =item B<meta> |
97 | |
98 | =back |
99 | |
100 | =head1 BUGS |
101 | |
102 | All complex software has bugs lurking in it, and this module is no |
103 | exception. If you find a bug please either email me, or add the bug |
104 | to cpan-RT. |
105 | |
106 | =head1 AUTHOR |
107 | |
108 | Chris Prather E<lt>chris.prather@iinteractive.comE<gt> |
109 | |
110 | Stevan Little E<lt>stevan.little@iinteractive.comE<gt> |
111 | |
112 | =head1 COPYRIGHT AND LICENSE |
113 | |
1f3074ea |
114 | Copyright 2007-2008 by Infinity Interactive, Inc. |
ec9c1923 |
115 | |
116 | L<http://www.iinteractive.com> |
117 | |
118 | This library is free software; you can redistribute it and/or modify |
119 | it under the same terms as Perl itself. |
120 | |
121 | =cut |