File Coverage

blib/lib/MooseX/Storage/Basic.pm
Criterion Covered Total %
statement 31 31 100.0
branch 3 4 75.0
condition 4 5 80.0
subroutine 9 9 100.0
pod 2 2 100.0
total 49 51 96.0


line stmt bran cond sub pod time code
1             package MooseX::Storage::Basic;
2             # ABSTRACT: The simplest level of serialization
3              
4             our $VERSION = '0.52';
5              
6 28     28   12502 use Moose::Role;
  28         43  
  28         338  
7 28     28   124438 use MooseX::Storage::Engine;
  28         74  
  28         1055  
8 28     28   201 use String::RewritePrefix;
  28         37  
  28         268  
9 28     28   5313 use namespace::autoclean;
  28         44  
  28         180  
10              
11             sub pack {
12 88     88 1 168630 my ( $self, %args ) = @_;
13 88         351 my $e = $self->_storage_get_engine_class(%args)->new( object => $self );
14 88         50918 $e->collapse_object(%args);
15             }
16              
17             sub unpack {
18 78     78 1 205184 my ($class, $data, %args) = @_;
19 78         317 my $e = $class->_storage_get_engine_class(%args)->new(class => $class);
20              
21 78         45246 $class->_storage_construct_instance(
22             $e->expand_object($data, %args),
23             \%args
24             );
25             }
26              
27             sub _storage_get_engine_class {
28 166     166   312 my ($self, %args) = @_;
29              
30             return 'MooseX::Storage::Engine'
31             unless (
32             exists $args{engine_traits}
33             && ref($args{engine_traits}) eq 'ARRAY'
34 166 50 66     1587 && scalar(@{$args{engine_traits}})
  4   100     16  
35             );
36              
37             my @roles = String::RewritePrefix->rewrite(
38             {
39             '' => 'MooseX::Storage::Engine::Trait::',
40             '+' => '',
41             },
42 4         69 @{$args{engine_traits}}
  4         40  
43             );
44              
45 4         291 Moose::Meta::Class->create_anon_class(
46             superclasses => ['MooseX::Storage::Engine'],
47             roles => [ @roles ],
48             cache => 1,
49             )->name;
50             }
51              
52             sub _storage_construct_instance {
53 75     75   150 my ($class, $args, $opts) = @_;
54 75 100       257 my %i = defined $opts->{'inject'} ? %{ $opts->{'inject'} } : ();
  1         4  
55              
56 75         385 $class->new( %$args, %i );
57             }
58              
59 28     28   9241 no Moose::Role;
  28         48  
  28         176  
60              
61             1;
62              
63             __END__
64              
65             =pod
66              
67             =encoding UTF-8
68              
69             =head1 NAME
70              
71             MooseX::Storage::Basic - The simplest level of serialization
72              
73             =head1 VERSION
74              
75             version 0.52
76              
77             =head1 SYNOPSIS
78              
79             package Point;
80             use Moose;
81             use MooseX::Storage;
82              
83             with Storage;
84              
85             has 'x' => (is => 'rw', isa => 'Int');
86             has 'y' => (is => 'rw', isa => 'Int');
87              
88             1;
89              
90             my $p = Point->new(x => 10, y => 10);
91              
92             ## methods to pack/unpack an
93             ## object in perl data structures
94              
95             # pack the class into a hash
96             $p->pack(); # { __CLASS__ => 'Point-0.01', x => 10, y => 10 }
97              
98             # unpack the hash into a class
99             my $p2 = Point->unpack({ __CLASS__ => 'Point-0.01', x => 10, y => 10 });
100              
101             # unpack the hash, with injection of additional paramaters
102             my $p3 = Point->unpack( $p->pack, inject => { x => 11 } );
103              
104             =head1 DESCRIPTION
105              
106             This is the most basic form of serialization. This is used by default
107             but the exported C<Storage> function.
108              
109             =head1 METHODS
110              
111             =over 4
112              
113             =item B<pack ([ disable_cycle_check => 1])>
114              
115             Providing the C<disable_cycle_check> argument disables checks for any cyclical
116             references. The current implementation for this check is rather naive, so if
117             you know what you are doing, you can bypass this check.
118              
119             This trait is applied on a perl-case basis. To set this flag for all objects
120             that inherit from this role, see L<MooseX::Storage::Traits::DisableCycleDetection>.
121              
122             =item B<unpack ($data [, inject => { key => val, ... } ] )>
123              
124             Providing the C<inject> argument lets you supply additional arguments to
125             the class' C<new> function, or override ones from the serialized data.
126              
127             =back
128              
129             =head1 SUPPORT
130              
131             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Storage>
132             (or L<bug-MooseX-Storage@rt.cpan.org|mailto:bug-MooseX-Storage@rt.cpan.org>).
133              
134             There is also a mailing list available for users of this distribution, at
135             L<http://lists.perl.org/list/moose.html>.
136              
137             There is also an irc channel available for users of this distribution, at
138             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
139              
140             =head1 AUTHORS
141              
142             =over 4
143              
144             =item *
145              
146             Chris Prather <chris.prather@iinteractive.com>
147              
148             =item *
149              
150             Stevan Little <stevan.little@iinteractive.com>
151              
152             =item *
153              
154             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
155              
156             =back
157              
158             =head1 COPYRIGHT AND LICENSE
159              
160             This software is copyright (c) 2007 by Infinity Interactive, Inc.
161              
162             This is free software; you can redistribute it and/or modify it under
163             the same terms as the Perl 5 programming language system itself.
164              
165             =cut