1 package MooseX::Storage::Basic;
4 use MooseX::Storage::Engine;
5 use String::RewritePrefix;
8 my ( $self, %args ) = @_;
9 my $e = $self->_storage_get_engine_class(%args)->new( object => $self );
10 $e->collapse_object(%args);
14 my ($class, $data, %args) = @_;
15 my $e = $class->_storage_get_engine_class(%args)->new(class => $class);
17 $class->_storage_construct_instance(
18 $e->expand_object($data, %args),
23 sub _storage_get_engine_class {
24 my ($self, %args) = @_;
26 return 'MooseX::Storage::Engine'
28 exists $args{engine_traits}
29 && ref($args{engine_traits}) eq 'ARRAY'
30 && scalar(@{$args{engine_traits}})
33 my @roles = String::RewritePrefix->rewrite(
35 '' => 'MooseX::Storage::Engine::Trait::',
38 @{$args{engine_traits}}
41 Moose::Meta::Class->create_anon_class(
42 superclasses => ['MooseX::Storage::Engine'],
48 sub _storage_construct_instance {
49 my ($class, $args, $opts) = @_;
50 my %i = defined $opts->{'inject'} ? %{ $opts->{'inject'} } : ();
52 $class->new( %$args, %i );
65 MooseX::Storage::Basic - The simplest level of serialization
73 our $VERSION = '0.01';
77 has 'x' => (is => 'rw', isa => 'Int');
78 has 'y' => (is => 'rw', isa => 'Int');
82 my $p = Point->new(x => 10, y => 10);
84 ## methods to pack/unpack an
85 ## object in perl data structures
87 # pack the class into a hash
88 $p->pack(); # { __CLASS__ => 'Point-0.01', x => 10, y => 10 }
90 # unpack the hash into a class
91 my $p2 = Point->unpack({ __CLASS__ => 'Point-0.01', x => 10, y => 10 });
93 # unpack the hash, with insertion of paramaters
94 my $p3 = Point->unpack( $p->pack, inject => { x => 11 } );
98 This is the most basic form of serialization. This is used by default
99 but the exported C<Storage> function.
105 =item B<pack ([ disable_cycle_check => 1])>
107 Providing the C<disable_cycle_check> argument disables checks for any cyclical
108 references. The current implementation for this check is rather naive, so if
109 you know what you are doing, you can bypass this check.
111 This trait is applied on a perl-case basis. To set this flag for all objects
112 that inherit from this role, see L<MooseX::Storage::Traits::DisableCycleDetection>.
114 =item B<unpack ($data [, insert => { key => val, ... } ] )>
116 Providing the C<insert> argument let's you supply additional arguments to
117 the class' C<new> function, or override ones from the serialized data.
131 All complex software has bugs lurking in it, and this module is no
132 exception. If you find a bug please either email me, or add the bug
137 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
139 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
141 =head1 COPYRIGHT AND LICENSE
143 Copyright 2007-2008 by Infinity Interactive, Inc.
145 L<http://www.iinteractive.com>
147 This library is free software; you can redistribute it and/or modify
148 it under the same terms as Perl itself.