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