File Coverage

blib/lib/MooseX/Storage/Basic.pm
Criterion Covered Total %
statement 28 28 100.0
branch 3 4 75.0
condition 4 5 80.0
subroutine 8 8 100.0
pod 2 2 100.0
total 45 47 95.7


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