File Coverage

blib/lib/MooseX/Storage/Base/SerializedClass.pm
Criterion Covered Total %
statement 29 29 100.0
branch 4 4 100.0
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 42 43 97.6


line stmt bran cond sub pod time code
1             package MooseX::Storage::Base::SerializedClass;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Deserialize according to the serialized __CLASS__
4             $MooseX::Storage::Base::SerializedClass::VERSION = '0.2.0';
5              
6 3     3   1795870 use parent 'Exporter';
  3         9  
  3         24  
7              
8 3     3   173 use Moose::Role;
  3         6  
  3         23  
9              
10             with 'MooseX::Storage::Basic';
11              
12 3     3   15683 use Moose::Util qw/ with_traits /;
  3         9  
  3         19  
13 3     3   647 use Class::Load 'load_class';
  3         7  
  3         191  
14 3     3   23 use List::MoreUtils qw/ apply /;
  3         7  
  3         34  
15              
16             our @EXPORT_OK = qw/ moosex_unpack /;
17              
18 3     3   1421 use namespace::autoclean;
  3         5  
  3         23  
19              
20             around unpack => sub {
21             my( $orig, $class, $data, %args ) = @_;
22              
23             $class = _unpack_class( $data );
24              
25             $orig->($class,$data,%args);
26             };
27              
28             sub _unpack_class {
29 13     13   20 my $data = shift;
30              
31 13         49 my $class = Class::Load::load_class( $data->{'__CLASS__'} );
32              
33 13 100       392 if( my $roles = delete $data->{'__ROLES__'} ) {
34             my @roles = apply {
35 3 100   3   8 if( my( $c, $params ) = eval { %$_} ) {
  3         38  
36 1         9 $_ = $c->meta->generate_role( parameters => $params );
37             }
38 3         24 } @$roles;
39              
40 3         4334 $class = with_traits( $class, @roles );
41             }
42              
43 13         6897 return $data->{'__CLASS__'} = $class;
44             }
45              
46             sub moosex_unpack {
47 3     3 0 29356 my $data = shift;
48 3         12 _unpack_class($data)->unpack($data);
49             }
50              
51             1;
52              
53             __END__
54              
55             =pod
56              
57             =encoding UTF-8
58              
59             =head1 NAME
60              
61             MooseX::Storage::Base::SerializedClass - Deserialize according to the serialized __CLASS__
62              
63             =head1 VERSION
64              
65             version 0.2.0
66              
67             =head1 SYNOPSIS
68              
69             package ThirdDimension;
70             use Moose::Role;
71              
72             has 'z' => (is => 'rw', isa => 'Int');
73              
74             package Point;
75             use Moose;
76             use MooseX::Storage;
77              
78             with Storage( base => 'SerializedClass', traits => [ 'WithRoles' ] );
79              
80             has 'x' => (is => 'rw', isa => 'Int');
81             has 'y' => (is => 'rw', isa => 'Int');
82              
83             1;
84              
85             use Moose::Util qw/ with_traits /;
86              
87             my $p = with_traits( 'Point', 'ThirdDimension' )->new(x => 10, y => 10, z => 10);
88              
89             my $packed = $p->pack();
90             # { __CLASS__ => 'Point', '__ROLES__' => [ 'ThirdDimension' ], x => 10, y => 10, z => 10 }
91              
92             # unpack the hash into a class
93             my $p2 = Point->unpack($packed);
94              
95             print $p2->z;
96              
97             =head1 DESCRIPTION
98              
99             Behaves like L<MooseX::Storage::Basic>, with the exception that
100             the unpacking will reinflate the object into the class and roles
101             as provided in the serialized data. It is means to be used in
102             conjuncture with L<MooseX::Storage::Traits::WithRoles>.
103              
104             =head1 EXPORTED FUNCTIONS
105              
106             The function C<moosex_unpack> can be exported. The function unpacks
107             a serialized object based on its C<__CLASS__> and C<__ROLES__> attributes.
108              
109             use MooseX::Storage::Base::SerializedClass qw/ moosex_unpack /;
110              
111             my $object = moosex_unpack( $struct );
112              
113             =head1 AUTHOR
114              
115             Yanick Champoux <yanick@babyl.dyndns.org>
116              
117             =head1 COPYRIGHT AND LICENSE
118              
119             This software is copyright (c) 2015 by Yanick Champoux.
120              
121             This is free software; you can redistribute it and/or modify it under
122             the same terms as the Perl 5 programming language system itself.
123              
124             =cut