01e66dd9ecbde24ebfc6566f291440045fbeb616
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Basic.pm
1 package MooseX::Storage::Basic;
2 use Moose::Role;
3
4 use MooseX::Storage::Engine;
5
6 our $VERSION   = '0.18';
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 sub pack {
10     my ( $self, @args ) = @_;
11     my $e = $self->_storage_get_engine( object => $self );
12     $e->collapse_object(@args);
13 }
14
15 sub unpack {
16     my ($class, $data, %args) = @_;
17     my $e = $class->_storage_get_engine(class => $class);
18     
19     $class->_storage_construct_instance( 
20         [ $e->expand_object($data, %args) ], 
21         \%args 
22     );
23 }
24
25 sub _storage_get_engine {
26     my $self = shift;
27     MooseX::Storage::Engine->new( @_ );
28 }
29
30 sub _storage_construct_instance {
31     my ($class, $args, $opts) = @_;
32     
33     my @i = defined $opts->{'inject'} ? @{ $opts->{'inject'} } : ();
34  
35     $class->new( @$args, @i );
36 }
37
38
39
40
41 1;
42
43 __END__
44
45 =pod
46
47 =head1 NAME
48
49 MooseX::Storage::Basic - The simplest level of serialization
50
51 =head1 SYNOPSIS
52
53   package Point;
54   use Moose;
55   use MooseX::Storage;
56   
57   our $VERSION = '0.01';
58   
59   with Storage;
60   
61   has 'x' => (is => 'rw', isa => 'Int');
62   has 'y' => (is => 'rw', isa => 'Int');
63   
64   1;
65   
66   my $p = Point->new(x => 10, y => 10);
67   
68   ## methods to pack/unpack an 
69   ## object in perl data structures
70   
71   # pack the class into a hash
72   $p->pack(); # { __CLASS__ => 'Point-0.01', x => 10, y => 10 }
73   
74   # unpack the hash into a class
75   my $p2 = Point->unpack({ __CLASS__ => 'Point-0.01', x => 10, y => 10 });
76
77 =head1 DESCRIPTION
78
79 This is the most basic form of serialization. This is used by default 
80 but the exported C<Storage> function.
81
82 =head1 METHODS
83
84 =over 4
85
86 =item B<pack>
87
88 =item B<unpack ($data)>
89
90 =back
91
92 =head2 Introspection
93
94 =over 4
95
96 =item B<meta>
97
98 =back
99
100 =head1 BUGS
101
102 All complex software has bugs lurking in it, and this module is no 
103 exception. If you find a bug please either email me, or add the bug
104 to cpan-RT.
105
106 =head1 AUTHOR
107
108 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
109
110 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
111
112 =head1 COPYRIGHT AND LICENSE
113
114 Copyright 2007-2008 by Infinity Interactive, Inc.
115
116 L<http://www.iinteractive.com>
117
118 This library is free software; you can redistribute it and/or modify
119 it under the same terms as Perl itself.
120
121 =cut