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