lib/MooseX/Storage/Format/JSONpm.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod n/a totaltotal 91.1% 31 / 34 CC 1 SCAR 1.2
CC
1
Cov
92%
thaw
2.0
__ANON__
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package MooseX::Storage::Format::JSONpm 0.093094;
2 1 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 # ABSTRACT: a format role for MooseX::Storage using JSON.pm
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod package Point;
8 #pod use Moose;
9 #pod use MooseX::Storage;
10 #pod
11 #pod with Storage(format => 'JSONpm');
12 #pod
13 #pod has 'x' => (is => 'rw', isa => 'Int');
14 #pod has 'y' => (is => 'rw', isa => 'Int');
15 #pod
16 #pod 1;
17 #pod
18 #pod my $p = Point->new(x => 10, y => 10);
19 #pod
20 #pod # pack the class into a JSON string
21 #pod my $json = $p->freeze(); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
22 #pod
23 #pod # unpack the JSON string into an object
24 #pod my $p2 = Point->thaw($json);
25 #pod
26 #pod ...in other words, it can be used as a drop-in replacement for
27 #pod MooseX::Storage::Format::JSON. However, it can also be parameterized:
28 #pod
29 #pod package Point;
30 #pod use Moose;
31 #pod use MooseX::Storage;
32 #pod
33 #pod with Storage(format => [ JSONpm => { json_opts => { pretty => 1 } } ]);
34 #pod
35 #pod At present, C<json_opts> is the only parameter, and is used when calling the
36 #pod C<to_json> and C<from_json> routines provided by the L<JSON|JSON> library.
37 #pod Default values are merged into the given hashref (with explict values taking
38 #pod priority). The defaults are as follows:
39 #pod
40 #pod { ascii => 1 }
41 #pod
42 #pod =cut
43  
44 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 parameter json_opts => (
49 isa => 'HashRef',
50 1 ▶ default => sub { return { } },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 1 ▶ initializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 1 my ($self, $value, $set) = @_;
53  
54 1 %$value = (ascii => 1, %$value);
55 1 $set->($value);
56 }
57 1 );
58  
59 1 ▶role {
Subroutine __ANON__: called CC=1 SCAR=0.1
60 1 my $p = shift;
61  
62 1 requires 'pack';
63 1 requires 'unpack';
64  
65 #pod =method freeze
66 #pod
67 #pod my $json = $obj->freeze;
68 #pod
69 #pod =cut
70  
71 3 ▶ method freeze => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 3 my ($self, @args) = @_;
73  
74 3 my $json = to_json($self->pack(@args), $p->json_opts);
75 3 return $json;
76 1 };
77  
78 #pod =method thaw
79 #pod
80 #pod my $obj = Class->thaw($json)
81 #pod
82 #pod =cut
83  
84 0 ▶ method thaw => sub {
Subroutine thaw: not called CC=1 SCAR=6.9
85 0 my ($class, $json, @args) = @_;
86  
87 0 $class->unpack( from_json($json, $p->json_opts), @args);
88 1 };
89  
90 1 };
91  
92 1 1;
93  
94 #pod =head1 THANKS
95 #pod
96 #pod Thanks to Stevan Little, Chris Prather, and Yuval Kogman, from whom I cribbed
97 #pod this code -- from MooseX::Storage::Format::JSON.
98  
99 __END__
↑ Summary