blib/lib/Venus/Yaml.pm

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