1 package MooseX::Storage::Basic;
4 use MooseX::Storage::Engine;
7 our $AUTHORITY = 'cpan:STEVAN';
10 my ( $self, %args ) = @_;
11 my $e = $self->_storage_get_engine_class(%args)->new( object => $self );
12 $e->collapse_object(%args);
16 my ($class, $data, %args) = @_;
17 my $e = $class->_storage_get_engine_class(%args)->new(class => $class);
19 $class->_storage_construct_instance(
20 $e->expand_object($data, %args),
25 sub _storage_get_engine_class {
26 my ($self, %args) = @_;
28 my $default = 'MooseX::Storage::Engine';
32 exists $args{engine_traits}
33 && ref($args{engine_traits}) eq 'ARRAY'
34 && scalar(@{$args{engine_traits}})
37 my @roles = map { sprintf("%s::Trait::%s", $default, $_) }
38 @{$args{engine_traits}};
40 Moose::Meta::Class->create_anon_class(
41 superclasses => [$default],
47 sub _storage_construct_instance {
48 my ($class, $args, $opts) = @_;
49 my %i = defined $opts->{'inject'} ? %{ $opts->{'inject'} } : ();
51 $class->new( %$args, %i );
64 MooseX::Storage::Basic - The simplest level of serialization
72 our $VERSION = '0.01';
76 has 'x' => (is => 'rw', isa => 'Int');
77 has 'y' => (is => 'rw', isa => 'Int');
81 my $p = Point->new(x => 10, y => 10);
83 ## methods to pack/unpack an
84 ## object in perl data structures
86 # pack the class into a hash
87 $p->pack(); # { __CLASS__ => 'Point-0.01', x => 10, y => 10 }
89 # unpack the hash into a class
90 my $p2 = Point->unpack({ __CLASS__ => 'Point-0.01', x => 10, y => 10 });
92 # unpack the hash, with insertion of paramaters
93 my $p3 = Point->unpack( $p->pack, inject => { x => 11 } );
97 This is the most basic form of serialization. This is used by default
98 but the exported C<Storage> function.
104 =item B<pack ([ disable_cycle_check => 1])>
106 Providing the C<disable_cycle_check> argument disables checks for any cyclical
107 references. The current implementation for this check is rather naive, so if
108 you know what you are doing, you can bypass this check.
110 This trait is applied on a perl-case basis. To set this flag for all objects
111 that inherit from this role, see L<MooseX::Storage::Traits::DisableCycleDetection>.
113 =item B<unpack ($data [, insert => { key => val, ... } ] )>
115 Providing the C<insert> argument let's you supply additional arguments to
116 the class' C<new> function, or override ones from the serialized data.
130 All complex software has bugs lurking in it, and this module is no
131 exception. If you find a bug please either email me, or add the bug
136 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
138 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
140 =head1 COPYRIGHT AND LICENSE
142 Copyright 2007-2008 by Infinity Interactive, Inc.
144 L<http://www.iinteractive.com>
146 This library is free software; you can redistribute it and/or modify
147 it under the same terms as Perl itself.