1 package MooseX::Storage::Basic;
4 use MooseX::Storage::Engine;
5 use String::RewritePrefix;
8 our $AUTHORITY = 'cpan:STEVAN';
11 my ( $self, %args ) = @_;
12 my $e = $self->_storage_get_engine_class(%args)->new( object => $self );
13 $e->collapse_object(%args);
17 my ($class, $data, %args) = @_;
18 my $e = $class->_storage_get_engine_class(%args)->new(class => $class);
20 $class->_storage_construct_instance(
21 $e->expand_object($data, %args),
26 sub _storage_get_engine_class {
27 my ($self, %args) = @_;
29 return 'MooseX::Storage::Engine'
31 exists $args{engine_traits}
32 && ref($args{engine_traits}) eq 'ARRAY'
33 && scalar(@{$args{engine_traits}})
36 my @roles = String::RewritePrefix->rewrite(
38 '' => 'MooseX::Storage::Engine::Trait::',
41 @{$args{engine_traits}}
44 Moose::Meta::Class->create_anon_class(
45 superclasses => ['MooseX::Storage::Engine'],
51 sub _storage_construct_instance {
52 my ($class, $args, $opts) = @_;
53 my %i = defined $opts->{'inject'} ? %{ $opts->{'inject'} } : ();
55 $class->new( %$args, %i );
68 MooseX::Storage::Basic - The simplest level of serialization
76 our $VERSION = '0.01';
80 has 'x' => (is => 'rw', isa => 'Int');
81 has 'y' => (is => 'rw', isa => 'Int');
85 my $p = Point->new(x => 10, y => 10);
87 ## methods to pack/unpack an
88 ## object in perl data structures
90 # pack the class into a hash
91 $p->pack(); # { __CLASS__ => 'Point-0.01', x => 10, y => 10 }
93 # unpack the hash into a class
94 my $p2 = Point->unpack({ __CLASS__ => 'Point-0.01', x => 10, y => 10 });
96 # unpack the hash, with insertion of paramaters
97 my $p3 = Point->unpack( $p->pack, inject => { x => 11 } );
101 This is the most basic form of serialization. This is used by default
102 but the exported C<Storage> function.
108 =item B<pack ([ disable_cycle_check => 1])>
110 Providing the C<disable_cycle_check> argument disables checks for any cyclical
111 references. The current implementation for this check is rather naive, so if
112 you know what you are doing, you can bypass this check.
114 This trait is applied on a perl-case basis. To set this flag for all objects
115 that inherit from this role, see L<MooseX::Storage::Traits::DisableCycleDetection>.
117 =item B<unpack ($data [, insert => { key => val, ... } ] )>
119 Providing the C<insert> argument let's you supply additional arguments to
120 the class' C<new> function, or override ones from the serialized data.
134 All complex software has bugs lurking in it, and this module is no
135 exception. If you find a bug please either email me, or add the bug
140 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
142 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
144 =head1 COPYRIGHT AND LICENSE
146 Copyright 2007-2008 by Infinity Interactive, Inc.
148 L<http://www.iinteractive.com>
150 This library is free software; you can redistribute it and/or modify
151 it under the same terms as Perl itself.