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