blib/lib/Starch/Plugin/Sereal.pm

filter: Statementstmt 100% 37 / 37 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 100% 53 / 53 CC 2 SCAR 1.4
CC
2
Cov
100%
is_data_diff
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
↑ Summary
1 package Starch::Plugin::Sereal;
2 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 our $VERSION = '0.04';
5  
6 =head1 NAME
7  
8 Starch::Plugin::Sereal - Use Sereal for cloning and diffing Starch data structures.
9  
10 =head1 SYNOPSIS
11  
12 my $starch = Starch->new(
13 plugins => ['::Sereal'],
14 );
15  
16 =head1 DESCRIPTION
17  
18 By default L<Starch::State/clone_data> and L<Starch::State/is_data_diff>
19 use L<Storable> to do the heavy lifting. This module replaces those two methods
20 with ones that use L<Sereal> which can be leaps and bounds faster than Storable.
21  
22 In this author's testing C<is_data_diff> will be about 3x faster with Sereal and
23 C<clone_data> will be about 1.5x faster with Sereal.
24  
25 =cut
26  
27 1 ▶use Sereal::Encoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Sereal::Decoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 with 'Starch::Plugin::ForManager';
35  
36 =head1 MANAGER ATTRIBUTES
37  
38 These attributes are added to the L<Starch::Manager> class.
39  
40 =head2 sereal_encoder
41  
42 An instance of L<Sereal::Encoder>.
43  
44 =cut
45  
46 1 has sereal_encoder => (
47 is => 'lazy',
48 isa => InstanceOf[ 'Sereal::Encoder' ],
49 );
50 3 ▶sub _build_sereal_encoder {
Subroutine _build_sereal_encoder: called CC=1 SCAR=0.0
51 3 return Sereal::Encoder->new();
52 }
53  
54 =head2 sereal_decoder
55  
56 An instance of L<Sereal::Decoder>.
57  
58 =cut
59  
60 1 has sereal_decoder => (
61 is => 'lazy',
62 isa => InstanceOf[ 'Sereal::Decoder' ],
63 );
64 3 ▶sub _build_sereal_decoder {
Subroutine _build_sereal_decoder: called CC=1 SCAR=0.0
65 3 return Sereal::Decoder->new();
66 }
67  
68 =head2 canonical_sereal_encoder
69  
70 An instance of L<Sereal::Encoder> with the C<canonical> option set.
71  
72 =cut
73  
74 1 has canonical_sereal_encoder => (
75 is => 'lazy',
76 isa => InstanceOf[ 'Sereal::Encoder' ],
77 );
78 3 ▶sub _build_canonical_sereal_encoder {
Subroutine _build_canonical_sereal_encoder: called CC=1 SCAR=0.0
79 3 return Sereal::Encoder->new({ canonical => 1 });
80 }
81  
82 =head1 MODIFIED MANAGER METHODS
83  
84 These methods are added to the L<Starch::Manager> class.
85  
86 =head2 clone_data
87  
88 Modified to use L</sereal_encoder> and L</sereal_decoder> to clone
89 a data structure.
90  
91 =cut
92  
93 48 ▶sub clone_data {
Subroutine clone_data: called CC=1 SCAR=0.0
Pod documented
94 48 my ($self, $data) = @_;
95  
96 48 return $self->sereal_decoder->decode(
97 $self->sereal_encoder->encode( $data ),
98 );
99 }
100  
101 =head2 is_data_diff
102  
103 Modified to use L</canonical_sereal_encoder> to encode the two data
104 structures.
105  
106 =cut
107  
108 31 ▶sub is_data_diff {
Subroutine is_data_diff: called CC=2 SCAR=6.9
Pod documented
109 31 my ($self, $old, $new) = @_;
110  
111 31 my $encoder = $self->canonical_sereal_encoder();
112  
113 31 $old = $encoder->encode( $old );
114 31 $new = $encoder->encode( $new );
115  
116 31 ▶ return 0 if $new eq $old;
Branch: 1 branch
BranchTrueFalseTotal
if $new eq $old 8 23 31
117 23 return 1;
118 }
119  
120 1 1;
121 __END__
↑ Summary