Commit | Line | Data |
ec9c1923 |
1 | package MooseX::Storage::Basic; |
2 | use Moose::Role; |
3 | |
4 | use MooseX::Storage::Engine; |
5 | |
8919e01e |
6 | our $VERSION = '0.26'; |
69b45b7d |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
7b428d1f |
8 | |
ec9c1923 |
9 | sub pack { |
a473d69d |
10 | my ( $self, %args ) = @_; |
11 | my $e = $self->_storage_get_engine_class(%args)->new( object => $self ); |
12 | $e->collapse_object(%args); |
ec9c1923 |
13 | } |
14 | |
15 | sub unpack { |
298cda98 |
16 | my ($class, $data, %args) = @_; |
a473d69d |
17 | my $e = $class->_storage_get_engine_class(%args)->new(class => $class); |
ec725183 |
18 | |
19 | $class->_storage_construct_instance( |
20 | $e->expand_object($data, %args), |
21 | \%args |
298cda98 |
22 | ); |
23 | } |
24 | |
21257e61 |
25 | sub _storage_get_engine_class { |
a473d69d |
26 | my ($self, %args) = @_; |
27 | |
28 | my $default = 'MooseX::Storage::Engine'; |
29 | |
30 | return $default |
31 | unless ( |
ec725183 |
32 | exists $args{engine_traits} |
a473d69d |
33 | && ref($args{engine_traits}) eq 'ARRAY' |
34 | && scalar(@{$args{engine_traits}}) |
35 | ); |
ec725183 |
36 | |
a473d69d |
37 | my @roles = map { sprintf("%s::Trait::%s", $default, $_) } |
38 | @{$args{engine_traits}}; |
ec725183 |
39 | |
a473d69d |
40 | Moose::Meta::Class->create_anon_class( |
41 | superclasses => [$default], |
42 | roles => [ @roles ], |
43 | cache => 1, |
44 | )->name; |
298cda98 |
45 | } |
46 | |
47 | sub _storage_construct_instance { |
48 | my ($class, $args, $opts) = @_; |
c21a034f |
49 | my %i = defined $opts->{'inject'} ? %{ $opts->{'inject'} } : (); |
ec725183 |
50 | |
c21a034f |
51 | $class->new( %$args, %i ); |
ec9c1923 |
52 | } |
53 | |
f82612bc |
54 | no Moose::Role; |
55 | |
ec9c1923 |
56 | 1; |
57 | |
58 | __END__ |
59 | |
60 | =pod |
61 | |
62 | =head1 NAME |
63 | |
1390c23d |
64 | MooseX::Storage::Basic - The simplest level of serialization |
ec9c1923 |
65 | |
66 | =head1 SYNOPSIS |
67 | |
1390c23d |
68 | package Point; |
69 | use Moose; |
70 | use MooseX::Storage; |
ec725183 |
71 | |
c1830046 |
72 | our $VERSION = '0.01'; |
ec725183 |
73 | |
1390c23d |
74 | with Storage; |
ec725183 |
75 | |
1390c23d |
76 | has 'x' => (is => 'rw', isa => 'Int'); |
77 | has 'y' => (is => 'rw', isa => 'Int'); |
ec725183 |
78 | |
1390c23d |
79 | 1; |
ec725183 |
80 | |
1390c23d |
81 | my $p = Point->new(x => 10, y => 10); |
ec725183 |
82 | |
83 | ## methods to pack/unpack an |
1390c23d |
84 | ## object in perl data structures |
ec725183 |
85 | |
1390c23d |
86 | # pack the class into a hash |
c1830046 |
87 | $p->pack(); # { __CLASS__ => 'Point-0.01', x => 10, y => 10 } |
ec725183 |
88 | |
1390c23d |
89 | # unpack the hash into a class |
c1830046 |
90 | my $p2 = Point->unpack({ __CLASS__ => 'Point-0.01', x => 10, y => 10 }); |
ec725183 |
91 | |
c21a034f |
92 | # unpack the hash, with insertion of paramaters |
93 | my $p3 = Point->unpack( $p->pack, inject => { x => 11 } ); |
1390c23d |
94 | |
ec9c1923 |
95 | =head1 DESCRIPTION |
96 | |
ec725183 |
97 | This is the most basic form of serialization. This is used by default |
1390c23d |
98 | but the exported C<Storage> function. |
99 | |
ec9c1923 |
100 | =head1 METHODS |
101 | |
102 | =over 4 |
103 | |
5b7ea1fd |
104 | =item B<pack ([ disable_cycle_check => 1])> |
105 | |
106 | Providing the C<disable_cycle_check> argument disables checks for any cyclical |
107 | references. The current implementation for this check is rather naive, so if |
108 | you know what you are doing, you can bypass this check. |
109 | |
110 | This trait is applied on a perl-case basis. To set this flag for all objects |
111 | that inherit from this role, see L<MooseX::Storage::Traits::DisableCycleDetection>. |
ec9c1923 |
112 | |
c21a034f |
113 | =item B<unpack ($data [, insert => { key => val, ... } ] )> |
114 | |
115 | Providing the C<insert> argument let's you supply additional arguments to |
116 | the class' C<new> function, or override ones from the serialized data. |
ec9c1923 |
117 | |
118 | =back |
119 | |
120 | =head2 Introspection |
121 | |
122 | =over 4 |
123 | |
124 | =item B<meta> |
125 | |
126 | =back |
127 | |
128 | =head1 BUGS |
129 | |
ec725183 |
130 | All complex software has bugs lurking in it, and this module is no |
ec9c1923 |
131 | exception. If you find a bug please either email me, or add the bug |
132 | to cpan-RT. |
133 | |
134 | =head1 AUTHOR |
135 | |
136 | Chris Prather E<lt>chris.prather@iinteractive.comE<gt> |
137 | |
138 | Stevan Little E<lt>stevan.little@iinteractive.comE<gt> |
139 | |
140 | =head1 COPYRIGHT AND LICENSE |
141 | |
1f3074ea |
142 | Copyright 2007-2008 by Infinity Interactive, Inc. |
ec9c1923 |
143 | |
144 | L<http://www.iinteractive.com> |
145 | |
146 | This library is free software; you can redistribute it and/or modify |
147 | it under the same terms as Perl itself. |
148 | |
149 | =cut |