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