Venus/Yaml.pm

filter: Statementstmt 84.7% 78 / 92 Branchbran 80.0% 16 / 20 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 78.9% 15 / 19 Podpod 25.0% 2 / 8 totaltotal 77.7% 112 / 144 CC 17 SCAR 14.9
CC
17
Cov
83%
config
11.7
FROM_BOOL
5.0
TO_BOOL
5.0
CRAP 4.4
« Venus/Try.pm ↑ Summary Venus/Hash.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;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 base 'Venus::Kind::Utility';
17  
18 2 with 'Venus::Role::Accessible';
19 2 with 'Venus::Role::Explainable';
20  
21 # ATTRIBUTES
22  
23 2 attr 'decoder';
24 2 attr 'encoder';
25  
26 # BUILDERS
27  
28 sub build_self {
29 4 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=1 SCAR=0.0
Pod undocumented
30  
31 4 return $self->config;
32 }
33  
34 # METHODS
35  
36 sub config {
37 4 ▶ my ($self, $package) = @_;
Subroutine config: called CC=6 SCAR=24.6
Pod undocumented
38  
39 4 ▶ $package ||= $self->package or $self->throw->error({
Branch: 1 branch
BranchTrueFalseTotal
$package ||= $self->package or $self->throw->error({"message", "No suitable YAML package"}) 0 4 4
Condition33%1/3
$package ||= $self->package
l!l&&r!l&&!r
040
MC/DC0%0/2
$package ||= $self->package
$package $self->package
Truth table33%1/3
A ||= B
A= $packageB= $self->package
ABresult
1X1
011
000
40 message => 'No suitable YAML package',
41 });
42  
43 # YAML::XS
44 4 ▶ if ($package eq 'YAML::XS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "YAML::XS") 0 4 4
45 $self->decoder(sub {
46 0 ▶ my ($text) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 local $YAML::XS::Boolean = 'JSON::PP';
48 0 YAML::XS::Load($text);
49 0 });
50 $self->encoder(sub {
51 0 ▶ my ($data) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 local $YAML::XS::Boolean = 'JSON::PP';
53 0 YAML::XS::Dump($data);
54 0 });
55 }
56  
57 # YAML::PP::LibYAML
58 4 ▶ if ($package eq 'YAML::PP::LibYAML') {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "YAML::PP::LibYAML") 0 4 4
59 $self->decoder(sub {
60 0 ▶ my ($text) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 YAML::PP->new(boolean => 'JSON::PP')->load_string($text);
62 0 });
63 $self->encoder(sub {
64 0 ▶ my ($data) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 YAML::PP->new(boolean => 'JSON::PP')->dump_string($data);
66 0 });
67 }
68  
69 # YAML::PP
70 4 ▶ if ($package eq 'YAML::PP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "YAML::PP") 4 0 4
71 $self->decoder(sub {
72 1 ▶ my ($text) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
73 1 YAML::PP->new(boolean => 'JSON::PP')->load_string($text);
74 4 });
75 $self->encoder(sub {
76 5 ▶ my ($data) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
77 5 YAML::PP->new(boolean => 'JSON::PP')->dump_string($data);
78 4 });
79 }
80  
81 4 return $self;
82 }
83  
84 sub decode {
85 1 ▶ my ($self, $data) = @_;
Subroutine decode: called CC=1 SCAR=0.0
Pod documented
86  
87 # double-traversing the data structure due to lack of serialization hooks
88 1 return $self->set(FROM_BOOL($self->decoder->($data)));
89 }
90  
91 sub encode {
92 5 ▶ my ($self) = @_;
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
93  
94 # double-traversing the data structure due to lack of serialization hooks
95 5 return $self->encoder->(TO_BOOL($self->get));
96 }
97  
98 sub explain {
99 4 ▶ my ($self) = @_;
Subroutine explain: called CC=1 SCAR=0.0
Pod undocumented
100  
101 4 return $self->encode;
102 }
103  
104 sub package {
105 6 ▶ my ($self) = @_;
Subroutine package: called CC=4 SCAR=13.9
Pod undocumented
106  
107 6 state $engine;
108  
109 6 ▶ return $engine if defined $engine;
Branch: 1 branch
BranchTrueFalseTotal
if defined $engine 4 2 6
110  
111 2 my %packages = (
112 'YAML::XS' => '0.67',
113 'YAML::PP::LibYAML' => '0.004',
114 'YAML::PP' => '0.023',
115 );
116 2 for my $package (qw(YAML::XS YAML::PP::LibYAML YAML::PP)) {
117 6 my $criteria = "require $package; $package->VERSION($packages{$package})";
118 6 ▶ if (do {local $@; eval "$criteria"; $@}) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { local $@; eval "$criteria"; $@ }) { } 4 2 6
119 4 next;
120 }
121 else {
122 2 $engine = $package;
123 2 last;
124 }
125 }
126  
127 2 return $engine;
128 }
129  
130 sub FROM_BOOL {
131 5 ▶ my ($value) = @_;
Subroutine FROM_BOOL: called CC=5 SCAR=16.1
Pod undocumented
132  
133 5 require Venus::Boolean;
134  
135 5 ▶ if (ref($value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 1 4 5
136 1 for my $key (keys %$value) {
137 2 $value->{$key} = FROM_BOOL($value->{$key});
138 }
139 1 return $value;
140 }
141  
142 4 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 1 3 4
143 1 for my $key (keys @$value) {
144 2 $value->[$key] = FROM_BOOL($value->[$key]);
145 }
146 1 return $value;
147 }
148  
149 3 return Venus::Boolean::TO_BOOL(Venus::Boolean::FROM_BOOL($value));
150 }
151  
152 sub TO_BOOL {
153 20 ▶ my ($value) = @_;
Subroutine TO_BOOL: called CC=5 SCAR=16.1
Pod undocumented
154  
155 20 require Venus::Boolean;
156  
157 20 ▶ if (ref($value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 3 17 20
158 3 $value = {
159 %$value
160 };
161 3 for my $key (keys %$value) {
162 9 $value->{$key} = TO_BOOL($value->{$key});
163 }
164 3 return $value;
165 }
166  
167 17 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 3 14 17
168 3 $value = [
169 @$value
170 ];
171 3 for my $key (keys @$value) {
172 6 $value->[$key] = TO_BOOL($value->[$key]);
173 }
174 3 return $value;
175 }
176  
177 14 return Venus::Boolean::TO_BOOL_OBJ($value);
178 }
179  
180 2 1;
181  
182  
183  
184 =head1 NAME
185  
186 Venus::Yaml - Yaml Class
187  
188 =cut
189  
190 =head1 ABSTRACT
191  
192 Yaml Class for Perl 5
193  
194 =cut
195  
196 =head1 SYNOPSIS
197  
198 package main;
199  
200 use Venus::Yaml;
201  
202 my $yaml = Venus::Yaml->new(
203 value => { name => ['Ready', 'Robot'], version => 0.12, stable => !!1, }
204 );
205  
206 # $yaml->encode;
207  
208 =cut
209  
210 =head1 DESCRIPTION
211  
212 This package provides methods for reading and writing L<YAML|https://yaml.org>
213 data. B<Note:> This package requires that a suitable YAML is installed,
214 currently either C<YAML::XS> C<0.67+>, C<YAML::PP::LibYAML> C<0.004+>, or
215 C<YAML::PP> C<0.23+>.
216  
217 =cut
218  
219 =head1 ATTRIBUTES
220  
221 This package has the following attributes:
222  
223 =cut
224  
225 =head2 decoder
226  
227 decoder(CodeRef)
228  
229 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
230  
231 =cut
232  
233 =head2 encoder
234  
235 encoder(CodeRef)
236  
237 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
238  
239 =cut
240  
241 =head1 INHERITS
242  
243 This package inherits behaviors from:
244  
245 L<Venus::Kind::Utility>
246  
247 =cut
248  
249 =head1 INTEGRATES
250  
251 This package integrates behaviors from:
252  
253 L<Venus::Role::Accessible>
254  
255 L<Venus::Role::Explainable>
256  
257 =cut
258  
259 =head1 METHODS
260  
261 This package provides the following methods:
262  
263 =cut
264  
265 =head2 decode
266  
267 decode(Str $yaml) (Any)
268  
269 The decode method decodes the YAML string, sets the object value, and returns
270 the decoded value.
271  
272 I<Since C<0.01>>
273  
274 =over 4
275  
276 =item decode example 1
277  
278 # given: synopsis;
279  
280 my $decode = $yaml->decode("codename: ['Ready','Robot']\nstable: true");
281  
282 # { codename => ["Ready", "Robot"], stable => 1 }
283  
284 =back
285  
286 =cut
287  
288 =head2 encode
289  
290 encode() (Str)
291  
292 The encode method encodes the objects value as a YAML string and returns the
293 encoded string.
294  
295 I<Since C<0.01>>
296  
297 =over 4
298  
299 =item encode example 1
300  
301 # given: synopsis;
302  
303 my $encode = $yaml->encode;
304  
305 # "---\nname:\n- Ready\n- Robot\nstable: true\nversion: 0.12\n"
306  
307 =back
308  
309 =cut
« Venus/Try.pm ↑ Summary Venus/Hash.pm »