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