blib/lib/Bot/Cobalt/Serializer.pm

filter: Statementstmt 80.9% 102 / 126 Branchbran 46.8% 30 / 64 Conditioncond 38.8% 7 / 18 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 89.6% 26 / 29 Podpod 83.3% 5 / 6 totaltotal 67.5% 171 / 253 CC 39 SCAR 27.8
CC
39
Cov
67%
_write_serialized
46.2
_read_serialized
21.3
writefile
3.6
CRAP 16.1
« blib/lib/Bot/Cobalt/Plugin/Games/RockPaperScissors.pm ↑ Summary blib/lib/Bot/Cobalt/Core/ContextMeta/Ignore.pm »
1 package Bot::Cobalt::Serializer;
2 $Bot::Cobalt::Serializer::VERSION = '0.021003';
3 11 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 ## These two must be present anyway:
8 11 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Time::HiRes 'sleep';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 ▶use Bot::Cobalt::Common ':types';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 has Format => (
21 is => 'rw',
22 isa => Str,
23 33 ▶ builder => sub { 'YAMLXS' },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_Format: called CC=1 SCAR=0.0
24 1 ▶ trigger => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
25 1 my ($self, $format) = @_;
26 1 $format = uc($format);
27 confess "Unknown format $format"
28 1 ▶ unless grep { $_ eq $format } keys %{ $self->_types };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $format;} keys %{$self->_types;} 0 1 1
29 1 ▶ confess "Requested format $format but can't find a module for it"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_if_avail($format) 0 1 1
30 unless $self->_check_if_avail($format)
31 },
32 );
33  
34 has _types => (
35 lazy => 1,
36 is => 'ro',
37 isa => HashRef,
38 1 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 +{
40 1 YAML => 'YAML::Syck',
41 YAMLXS => 'YAML::XS',
42 JSON => 'JSON::MaybeXS',
43 }
44 },
45 );
46  
47 has yamlxs_from_ref => (
48 is => 'rw',
49 lazy => 1,
50 1 ▶ coerce => sub { YAML::XS::Dump($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 );
52  
53 has ref_from_yamlxs => (
54 is => 'rw',
55 lazy => 1,
56 18 ▶ coerce => sub { YAML::XS::Load($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 );
58  
59 has yaml_from_ref => (
60 is => 'rw',
61 lazy => 1,
62 0 ▶ coerce => sub { require YAML::Syck; YAML::Syck::Dump($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 );
64  
65 has ref_from_yaml => (
66 is => 'rw',
67 lazy => 1,
68 0 ▶ coerce => sub { require YAML::Syck; YAML::Syck::Load($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 );
70  
71 has json_from_ref => (
72 is => 'rw',
73 lazy => 1,
74 1 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 1 my $jsify = JSON::MaybeXS->new(
76 utf8 => 1, allow_nonref => 1, convert_blessed => 1
77 );
78 1 $jsify->utf8->encode($_[0]);
79 },
80 );
81  
82 has ref_from_json => (
83 is => 'rw',
84 lazy => 1,
85 1 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
86 1 my $jsify = JSON::MaybeXS->new(
87 utf8 => 1, allow_nonref => 1
88 );
89 1 $jsify->utf8->decode($_[0])
90 },
91 );
92  
93  
94 59 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=6.9
Pod undocumented
95 59 my ($class, @args) = @_;
96 ## my $serializer = Bot::Cobalt::Serializer->new( %opts )
97 ## Serialize to YAML using YAML::XS:
98 ## ->new()
99 ## - or -
100 ## ->new($format)
101 ## ->new('JSON') # f.ex
102 ## - or -
103 ## ->new( Format => 'JSON' ) ## --> to JSON
104 ## - or -
105 ## ->new( Format => 'YAML' ) ## --> to YAML1.0
106 59 ▶ @args == 1 ? { Format => $args[0] } : { @args }
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 1 58 59
107 }
108  
109 2 ▶sub freeze {
Subroutine freeze: called CC=2 SCAR=7.6
Pod documented
110 ## ->freeze($ref)
111 2 my ($self, $ref) = @_;
112 2 ▶ unless (defined $ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ref) 0 2 2
113 0 carp "freeze() received no data";
114 return
115 0 }
116  
117 2 my $method = lc( $self->Format );
118 2 $method = $method . "_from_ref";
119  
120 2 $self->$method($ref)
121 }
122  
123 19 ▶sub thaw {
Subroutine thaw: called CC=2 SCAR=7.6
Pod documented
124 ## ->thaw($data)
125 19 my ($self, $data) = @_;
126 19 ▶ unless (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 19 19
127 0 carp "thaw() received no data";
128 return
129 0 }
130  
131 19 my $method = lc( $self->Format );
132 19 $method = "ref_from_" . $method ;
133  
134 19 $self->$method($data)
135 }
136  
137 2 ▶sub writefile {
Subroutine writefile: called CC=3 SCAR=12.7
Pod documented
138 2 my ($self, $path, $ref, $opts) = @_;
139 ## $serializer->writefile($path, $ref [, { Opts });
140  
141 2 ▶ if (!$path) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $path) { } 0 2 2
elsif (not defined $ref) { } 0 2 2
142 0 confess "writefile called without path argument"
143 } elsif (!defined $ref) {
144 0 confess "writefile called without data to serialize"
145 }
146  
147 2 my $frozen = $self->freeze($ref);
148  
149 2 $self->_write_serialized($path, $frozen, $opts)
150 }
151  
152 16 ▶sub readfile {
Subroutine readfile: called CC=3 SCAR=12.7
Pod documented
153 16 my ($self, $path, $opts) = @_;
154 ## my $ref = $serializer->readfile($path)
155  
156 16 ▶ if (!$path) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $path) { } 0 16 16
elsif (not -e $path) { } 0 16 16
157 0 confess "readfile called without path argument";
158 } elsif (!-e $path ) {
159 0 confess "readfile called on nonexistant file $path";
160 }
161  
162 16 my $data = $self->_read_serialized($path, $opts);
163  
164 16 $self->thaw($data)
165 }
166  
167 0 ▶sub version {
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
168 0 my ($self) = @_;
169  
170 0 my $module = $self->_types->{ $self->Format };
171 0 { local $@; eval "require $module" }
172 0 return($module, $module->VERSION);
173 }
174  
175  
176  
177 1 ▶sub _check_if_avail {
Subroutine _check_if_avail: called CC=3 SCAR=11.1
178 1 my ($self, $type) = @_;
179  
180 1 my $module;
181 1 ▶ return unless $module = $self->_types->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $module = $self->_types->{$type} 0 1 1
182  
183 {
184 1 local $@;
185 1 eval "require $module";
186 1 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
187 }
188  
189 1 return $module
190 }
191  
192  
193 16 ▶sub _read_serialized {
Subroutine _read_serialized: called CC=13 SCAR=30.6
194 16 my ($self, $path, $opts) = @_;
195 16 ▶ return unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 16 16
196  
197 16 my $lock = 1;
198 16 ▶ if (defined $opts && ref $opts && reftype $opts eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts and ref $opts and builtin::reftype($opts) eq "HASH") 0 16 16
Condition33%2/6
defined $opts and ref $opts and builtin::reftype($opts) eq "HASH"
!ll&&!rl&&r
1600
defined $opts and ref $opts
!ll&&!rl&&r
1600
MC/DC0%0/3
defined $opts and ref $opts and builtin::reftype($opts) eq "HASH"
defined $opts ref $opts builtin::reftype($opts) eq "HASH"
Truth table25%1/4
A and B and C
A= defined $optsB= ref $optsC= builtin::reftype($opts) eq "HASH"
ABCresult
0XX0
10X0
1100
1111
199 0 ▶ $lock = $opts->{Locking} if defined $opts->{Locking};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"Locking"} 0 0 0
200 }
201  
202 16 ▶ if (blessed $path && $path->can('slurp_utf8')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($path) and $path->can('slurp_utf8')) { } 14 2 16
Condition66%2/3
builtin::blessed($path) and $path->can("slurp_utf8")
!ll&&!rl&&r
2014
MC/DC50%1/2
builtin::blessed($path) and $path->can("slurp_utf8")
builtin::blessed($path) $path->can("slurp_utf8")
Truth table66%2/3
A and B
A= builtin::blessed($path)B= $path->can("slurp_utf8")
ABresult
0X0
100
111
203 14 return $path->slurp_utf8
204 } else {
205 2 ▶ open(my $in_fh, '<:encoding(UTF-8)', $path)
Branch: 1 branch
BranchTrueFalseTotal
open my $in_fh, "<:encoding(UTF-8)", $path or confess("open failed for ${path}: $!") 2 0 2
206 or confess "open failed for $path: $!";
207  
208 2 ▶ if ($lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock) 2 0 2
209 2 ▶ flock($in_fh, LOCK_SH)
Branch: 1 branch
BranchTrueFalseTotal
flock $in_fh, 1 or confess("LOCK_SH failed for ${path}: $!") 2 0 2
210 or confess "LOCK_SH failed for $path: $!";
211 }
212  
213 2 my $data = join '', <$in_fh>;
214  
215 2 ▶ flock($in_fh, LOCK_UN) if $lock;
Branch: 1 branch
BranchTrueFalseTotal
if $lock 2 0 2
216  
217 2 ▶ close($in_fh)
Branch: 1 branch
BranchTrueFalseTotal
close $in_fh or carp("close failed for ${path}: $!") 2 0 2
218 or carp "close failed for $path: $!";
219  
220 2 return $data
221 }
222 }
223  
224 2 ▶sub _write_serialized {
Subroutine _write_serialized: called CC=16 SCAR=38.3
225 2 my ($self, $path, $data, $opts) = @_;
226 2 ▶ return unless $path and defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $path and defined $data 0 2 2
Condition33%1/3
$path and defined $data
!ll&&!rl&&r
002
MC/DC0%0/2
$path and defined $data
$path defined $data
Truth table33%1/3
A and B
A= $pathB= defined $data
ABresult
0X0
100
111
227  
228 2 my $lock = 1;
229 2 my $timeout = 2;
230  
231 2 ▶ if (defined $opts && ref $opts && reftype $opts eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts and ref $opts and builtin::reftype($opts) eq "HASH") 0 2 2
Condition33%2/6
defined $opts and ref $opts and builtin::reftype($opts) eq "HASH"
!ll&&!rl&&r
200
defined $opts and ref $opts
!ll&&!rl&&r
200
MC/DC0%0/3
defined $opts and ref $opts and builtin::reftype($opts) eq "HASH"
defined $opts ref $opts builtin::reftype($opts) eq "HASH"
Truth table25%1/4
A and B and C
A= defined $optsB= ref $optsC= builtin::reftype($opts) eq "HASH"
ABCresult
0XX0
10X0
1100
1111
232 0 ▶ $lock = $opts->{Locking} if defined $opts->{Locking};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"Locking"} 0 0 0
233 0 ▶ $timeout = $opts->{Timeout} if $opts->{Timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"Timeout"} 0 0 0
234 }
235  
236 2 ▶ open(my $out_fh, '>>:encoding(UTF-8)', $path)
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, ">>:encoding(UTF-8)", $path or confess("open failed for ${path}: $!") 2 0 2
237 or confess "open failed for $path: $!";
238  
239 2 ▶ if ($lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock) 2 0 2
240 2 my $timer = 0;
241  
242 2 ▶ until ( flock $out_fh, LOCK_EX | LOCK_NB ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $out_fh, 6) 0 2 2
243 0 ▶ confess "Failed writefile lock ($path), timed out ($timeout)"
Branch: 1 branch
BranchTrueFalseTotal
if $timer > $timeout 0 0 0
244 if $timer > $timeout;
245  
246 0 sleep 0.01;
247 0 $timer += 0.01;
248 }
249  
250 }
251  
252 2 ▶ seek($out_fh, 0, 0)
Branch: 1 branch
BranchTrueFalseTotal
seek $out_fh, 0, 0 or confess("seek failed for ${path}: $!") 2 0 2
253 or confess "seek failed for $path: $!";
254 2 ▶ truncate($out_fh, 0)
Branch: 1 branch
BranchTrueFalseTotal
truncate $out_fh, 0 or confess("truncate failed for $path") 2 0 2
255 or confess "truncate failed for $path";
256  
257 2 print $out_fh $data;
258  
259 2 ▶ flock($out_fh, LOCK_UN) if $lock;
Branch: 1 branch
BranchTrueFalseTotal
if $lock 2 0 2
260  
261 2 ▶ close($out_fh)
Branch: 1 branch
BranchTrueFalseTotal
close $out_fh or carp("close failed for ${path}: $!") 2 0 2
262 or carp "close failed for $path: $!";
263  
264 2 return 1
265 }
266  
267 1;
268 __END__
« blib/lib/Bot/Cobalt/Plugin/Games/RockPaperScissors.pm ↑ Summary blib/lib/Bot/Cobalt/Core/ContextMeta/Ignore.pm »