blib/lib/Venus/Yaml.pm

filter: Statementstmt 78.9% 86 / 109 Branchbran 81.8% 18 / 22 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 73.9% 17 / 23 Podpod 16.6% 2 / 12 totaltotal 73.3% 129 / 176 CC 19 SCAR 14.2
CC
19
Cov
79%
config
11.7
FROM_BOOL
5.0
TO_BOOL
5.0
CRAP 4.1
« blib/lib/Venus/Array.pm ↑ Summary blib/lib/Venus/Try.pm »
1 package Venus::Yaml;
2  
3 5 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORTS
9  
10 5 ▶use Venus::Class 'attr', 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # INHERITS
13  
14 5 base 'Venus::Kind::Utility';
15  
16 # INTEGRATES
17  
18 5 with 'Venus::Role::Valuable';
19 5 with 'Venus::Role::Buildable';
20 5 with 'Venus::Role::Accessible';
21 5 with 'Venus::Role::Explainable';
22  
23 # OVERLOADS
24  
25 5 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 '""' => 'explain',
27 '~~' => 'explain',
28 fallback => 1,
29 5 );
30  
31 # ATTRIBUTES
32  
33 5 attr 'decoder';
34 5 attr 'encoder';
35  
36 # BUILDERS
37  
38 4 ▶sub build_arg {
Subroutine build_arg: called CC=1 SCAR=0.0
Pod undocumented
39 4 my ($self, $data) = @_;
40  
41 return {
42 4 value => $data
43 };
44 }
45  
46 28 ▶sub build_args {
Subroutine build_args: called CC=3 SCAR=11.0
Pod undocumented
47 28 my ($self, $data) = @_;
48  
49 28 ▶ if (keys %$data == 1 && exists $data->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$data == 1 and exists $data->{"value"}) 9 19 28
Condition100%3/3
keys %$data == 1 and exists $data->{"value"}
!ll&&!rl&&r
1549
MC/DC100%2/2
keys %$data == 1 and exists $data->{"value"}
keys %$data == 1 exists $data->{"value"}
Truth table100%3/3
A and B
A= keys %$data == 1B= exists $data->{"value"}
ABresult
0X0
100
111
50 9 return $data;
51 }
52 return {
53 19 value => $data
54 };
55 }
56  
57 0 ▶sub build_nil {
Subroutine build_nil: not called CC=1 SCAR=6.9
Pod undocumented
58 0 my ($self, $data) = @_;
59  
60 return {
61 0 value => $data
62 };
63 }
64  
65 28 ▶sub build_self {
Subroutine build_self: called CC=1 SCAR=0.0
Pod undocumented
66 28 my ($self, $data) = @_;
67  
68 28 return $self->config;
69 }
70  
71 # METHODS
72  
73 28 ▶sub config {
Subroutine config: called CC=6 SCAR=24.6
Pod undocumented
74 28 my ($self, $package) = @_;
75  
76 28 ▶ $package ||= $self->package
Branch: 1 branch
BranchTrueFalseTotal
$package ||= $self->package or $self->error_on_config->input($self, $package)->throw 28 0 28
Condition33%1/3
$package ||= $self->package
l!l&&r!l&&!r
0280
MC/DC0%0/2
$package ||= $self->package
$package $self->package
Truth table33%1/3
A ||= B
A= $packageB= $self->package
ABresult
1X1
011
000
77 or $self->error_on_config->input($self, $package)->throw;
78  
79 # YAML::XS
80 28 ▶ if ($package eq 'YAML::XS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "YAML::XS") 0 28 28
81 0 ▶ $self->decoder(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 my ($text) = @_;
83 0 local $YAML::XS::Boolean = 'JSON::PP';
84 0 YAML::XS::Load($text);
85 0 });
86 0 ▶ $self->encoder(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 my ($data) = @_;
88 0 local $YAML::XS::Boolean = 'JSON::PP';
89 0 YAML::XS::Dump($data);
90 0 });
91 }
92  
93 # YAML::PP::LibYAML
94 28 ▶ if ($package eq 'YAML::PP::LibYAML') {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "YAML::PP::LibYAML") 0 28 28
95 0 ▶ $self->decoder(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 my ($text) = @_;
97 0 YAML::PP->new(boolean => 'JSON::PP')->load_string($text);
98 0 });
99 0 ▶ $self->encoder(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 my ($data) = @_;
101 0 YAML::PP->new(boolean => 'JSON::PP')->dump_string($data);
102 0 });
103 }
104  
105 # YAML::PP
106 28 ▶ if ($package eq 'YAML::PP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "YAML::PP") 28 0 28
107 10 ▶ $self->decoder(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
108 10 my ($text) = @_;
109 10 YAML::PP->new(boolean => 'JSON::PP')->load_string($text);
110 28 });
111 19 ▶ $self->encoder(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
112 19 my ($data) = @_;
113 19 YAML::PP->new(boolean => 'JSON::PP')->dump_string($data);
114 28 });
115 }
116  
117 28 return $self;
118 }
119  
120 10 ▶sub decode {
Subroutine decode: called CC=1 SCAR=0.0
Pod documented
121 10 my ($self, $data) = @_;
122  
123 # double-traversing the data structure due to lack of serialization hooks
124 10 return $self->set(FROM_BOOL($self->decoder->($data)));
125 }
126  
127 19 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
128 19 my ($self) = @_;
129  
130 # double-traversing the data structure due to lack of serialization hooks
131 19 return $self->encoder->(TO_BOOL($self->get));
132 }
133  
134 6 ▶sub explain {
Subroutine explain: called CC=1 SCAR=0.0
Pod undocumented
135 6 my ($self) = @_;
136  
137 6 return $self->encode;
138 }
139  
140 49 ▶sub package {
Subroutine package: called CC=4 SCAR=13.9
Pod undocumented
141 49 my ($self) = @_;
142  
143 49 state $engine;
144  
145 49 ▶ return $engine if defined $engine;
Branch: 1 branch
BranchTrueFalseTotal
if defined $engine 44 5 49
146  
147 5 my %packages = (
148 'YAML::XS' => '0.67',
149 'YAML::PP::LibYAML' => '0.004',
150 'YAML::PP' => '0.023',
151 );
152 5 for my $package (
153 grep defined,
154 $ENV{VENUS_YAML_PACKAGE},
155 qw(YAML::XS YAML::PP::LibYAML YAML::PP)
156 )
157 {
158 15 my $criteria = "require $package; $package->VERSION($packages{$package})";
159 15 ▶ if (do {local $@; eval "$criteria"; $@}) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { local $@; eval "$criteria"; $@ }) { } 10 5 15
160 10 next;
161 }
162 else {
163 5 $engine = $package;
164 5 last;
165 }
166 }
167  
168 5 return $engine;
169 }
170  
171 235 ▶sub FROM_BOOL {
Subroutine FROM_BOOL: called CC=5 SCAR=16.1
Pod undocumented
172 235 my ($value) = @_;
173  
174 235 require Venus::Boolean;
175  
176 235 ▶ if (ref($value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 85 150 235
177 85 for my $key (keys %$value) {
178 197 $value->{$key} = FROM_BOOL($value->{$key});
179 }
180 85 return $value;
181 }
182  
183 150 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 14 136 150
184 14 for my $key (keys @$value) {
185 28 $value->[$key] = FROM_BOOL($value->[$key]);
186 }
187 14 return $value;
188 }
189  
190 136 return Venus::Boolean::TO_BOOL(Venus::Boolean::FROM_BOOL($value));
191 }
192  
193 126 ▶sub TO_BOOL {
Subroutine TO_BOOL: called CC=5 SCAR=16.1
Pod undocumented
194 126 my ($value) = @_;
195  
196 126 require Venus::Boolean;
197  
198 126 ▶ if (ref($value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 35 91 126
199 35 $value = {
200 %$value
201 };
202 35 for my $key (keys %$value) {
203 87 $value->{$key} = TO_BOOL($value->{$key});
204 }
205 35 return $value;
206 }
207  
208 91 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 10 81 91
209 10 $value = [
210 @$value
211 ];
212 10 for my $key (keys @$value) {
213 20 $value->[$key] = TO_BOOL($value->[$key]);
214 }
215 10 return $value;
216 }
217  
218 81 return Venus::Boolean::TO_BOOL_JPO($value);
219 }
220  
221 # ERRORS
222  
223 0 ▶sub error_on_config {
Subroutine error_on_config: not called CC=1 SCAR=6.9
Pod undocumented
224 0 my ($self) = @_;
225  
226 0 my $error = $self->error->sysinfo;
227  
228 0 $error->name('on.config');
229 0 $error->message('No suitable YAML package');
230 0 $error->offset(1);
231 0 $error->reset;
232  
233 0 return $error;
234 }
235  
236 5 1;
237  
238  
239  
240 =head1 NAME
241  
242 Venus::Yaml - Yaml Class
243  
244 =cut
245  
246 =head1 ABSTRACT
247  
248 Yaml Class for Perl 5
249  
250 =cut
251  
252 =head1 SYNOPSIS
253  
254 package main;
255  
256 use Venus::Yaml;
257  
258 my $yaml = Venus::Yaml->new(
259 value => { name => ['Ready', 'Robot'], version => 0.12, stable => !!1, }
260 );
261  
262 # $yaml->encode;
263  
264 =cut
265  
266 =head1 DESCRIPTION
267  
268 This package provides methods for reading and writing L<YAML|https://yaml.org>
269 data. B<Note:> This package requires that a suitable YAML library is installed,
270 currently either C<YAML::XS> C<0.67+>, C<YAML::PP::LibYAML> C<0.004+>, or
271 C<YAML::PP> C<0.23+>. You can use the C<VENUS_YAML_PACKAGE> environment
272 variable to include or prioritize your preferred YAML library.
273  
274 =cut
275  
276 =head1 ATTRIBUTES
277  
278 This package has the following attributes:
279  
280 =cut
281  
282 =head2 decoder
283  
284 decoder(CodeRef)
285  
286 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
287  
288 =cut
289  
290 =head2 encoder
291  
292 encoder(CodeRef)
293  
294 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
295  
296 =cut
297  
298 =head1 INHERITS
299  
300 This package inherits behaviors from:
301  
302 L<Venus::Kind::Utility>
303  
304 =cut
305  
306 =head1 INTEGRATES
307  
308 This package integrates behaviors from:
309  
310 L<Venus::Role::Accessible>
311  
312 L<Venus::Role::Buildable>
313  
314 L<Venus::Role::Explainable>
315  
316 L<Venus::Role::Valuable>
317  
318 =cut
319  
320 =head1 METHODS
321  
322 This package provides the following methods:
323  
324 =cut
325  
326 =head2 decode
327  
328 decode(string $yaml) (any)
329  
330 The decode method decodes the YAML string, sets the object value, and returns
331 the decoded value.
332  
333 I<Since C<0.01>>
334  
335 =over 4
336  
337 =item decode example 1
338  
339 # given: synopsis;
340  
341 my $decode = $yaml->decode("codename: ['Ready','Robot']\nstable: true");
342  
343 # { codename => ["Ready", "Robot"], stable => 1 }
344  
345 =back
346  
347 =cut
348  
349 =head2 encode
350  
351 encode() (string)
352  
353 The encode method encodes the objects value as a YAML string and returns the
354 encoded string.
355  
356 I<Since C<0.01>>
357  
358 =over 4
359  
360 =item encode example 1
361  
362 # given: synopsis;
363  
364 my $encode = $yaml->encode;
365  
366 # "---\nname:\n- Ready\n- Robot\nstable: true\nversion: 0.12\n"
367  
368 =back
369  
370 =cut
371  
372 =head2 new
373  
374 new(any @args) (Venus::Yaml)
375  
376 The new method constructs an instance of the package.
377  
378 I<Since C<4.15>>
379  
380 =over 4
381  
382 =item new example 1
383  
384 package main;
385  
386 use Venus::Yaml;
387  
388 my $new = Venus::Yaml->new;
389  
390 # bless(..., "Venus::Yaml")
391  
392 =back
393  
394 =over 4
395  
396 =item new example 2
397  
398 package main;
399  
400 use Venus::Yaml;
401  
402 my $new = Venus::Yaml->new(value => {password => 'secret'});
403  
404 # bless(..., "Venus::Yaml")
405  
406 =back
407  
408 =over 4
409  
410 =item B<may raise> L<Venus::Yaml::Error> C<on.config>
411  
412 package main;
413  
414 use Venus::Yaml;
415  
416 local $ENV{VENUS_YAML_PACKAGE} = 'Fake::Yaml';
417  
418 my $new = Venus::Yaml->new;
419  
420 # Error! (on.config)
421  
422 =back
423  
424 =cut
425  
426 =head1 AUTHORS
427  
428 Awncorp, C<awncorp@cpan.org>
429  
430 =cut
431  
432 =head1 LICENSE
433  
434 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
435  
436 This program is free software, you can redistribute it and/or modify it under
437 the terms of the Apache license version 2.0.
438  
439 =cut
« blib/lib/Venus/Array.pm ↑ Summary blib/lib/Venus/Try.pm »