blib/lib/Venus/Role/Serializable.pm

filter: Statementstmt 91.8% 68 / 74 Branchbran 86.6% 26 / 30 Conditioncond 44.4% 8 / 18 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 88.8% 8 / 9 Podpod 66.6% 2 / 3 totaltotal 79.5% 113 / 142 CC 24 SCAR 24.5
CC
24
Cov
84%
deconstruct
17.1
serialize
11.1
__ANON__
2.0
CRAP 11.5
« blib/lib/Venus/Role/Buildable.pm ↑ Summary blib/lib/Venus/Schema.pm »
1 package Venus::Role::Serializable;
2  
3 119 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 119 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 119 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORTS
9  
10 119 ▶use Venus::Role 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # METHODS
13  
14 11 ▶sub serialize {
Subroutine serialize: called CC=11 SCAR=24.1
Pod documented
15 11 my ($self) = @_;
16  
17 11 ▶ if ( Scalar::Util::blessed($self)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self) and $self->isa("Venus::Core") and $self->can("DOES") and $self->DOES("Venus::Role::Valuable")) 3 8 11
Condition44%4/9
builtin::blessed($self) and $self->isa("Venus::Core") and $self->can("DOES") and $self->DOES("Venus::Role::Valuable")
!ll&&!rl&&r
083
builtin::blessed($self) and $self->isa("Venus::Core") and $self->can("DOES")
!ll&&!rl&&r
0011
builtin::blessed($self) and $self->isa("Venus::Core")
!ll&&!rl&&r
0011
MC/DC25%1/4
builtin::blessed($self) and $self->isa("Venus::Core") and $self->can("DOES") and $self->DOES("Venus::Role::Valuable")
builtin::blessed($self) $self->isa("Venus::Core") $self->can("DOES") $self->DOES("Venus::Role::Valuable")
Truth table40%2/5
A and B and C and D
A= builtin::blessed($self)B= $self->isa("Venus::Core")C= $self->can("DOES")D= $self->DOES("Venus::Role::Valuable")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
18 && $self->isa('Venus::Core')
19 && $self->can('DOES')
20 && $self->DOES('Venus::Role::Valuable'))
21 {
22 3 return deconstruct($self, $self->value);
23 }
24  
25 8 ▶ if (UNIVERSAL::isa($self, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($self, "ARRAY")) 1 7 8
26 1 return deconstruct($self, [@{$self}]);
27 }
28  
29 7 ▶ if (UNIVERSAL::isa($self, 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($self, "CODE")) 1 6 7
30 1 ▶ return sub{goto \&$self};
Subroutine __ANON__: called CC=1 SCAR=0.0
31 }
32  
33 6 ▶ if (UNIVERSAL::isa($self, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($self, "HASH")) 2 4 6
34 2 return deconstruct($self, {%{$self}});
35 }
36  
37 4 ▶ if (UNIVERSAL::isa($self, 'REF')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($self, "REF")) 1 3 4
38 1 return deconstruct($self, ${$self});
39 }
40  
41 3 ▶ if (UNIVERSAL::isa($self, 'REGEXP')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($self, "REGEXP")) 1 2 3
42 1 return qr/$self/;
43 }
44  
45 2 ▶ if (UNIVERSAL::isa($self, 'SCALAR')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($self, "SCALAR")) 1 1 2
46 1 return deconstruct($self, ${$self});
47 }
48  
49 1 require Venus::Throw;
50 1 my $throw = Venus::Throw->new(join('::', map ucfirst, ref($self), 'error'));
51 1 $throw->name('on.serialize');
52 1 $throw->message("Can't serialize the object: $self");
53 1 $throw->stash(self => $self);
54 1 $throw->die;
55 }
56  
57 19 ▶sub deconstruct {
Subroutine deconstruct: called CC=14 SCAR=28.4
Pod documented
58 19 my ($self, $value) = @_;
59  
60 19 require Scalar::Util;
61  
62 19 ▶ if ( Scalar::Util::blessed($value)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Venus::Core") and $value->can("DOES") and $value->DOES("Venus::Role::Serializable")) 0 19 19
Condition44%4/9
builtin::blessed($value) and $value->isa("Venus::Core") and $value->can("DOES") and $value->DOES("Venus::Role::Serializable")
!ll&&!rl&&r
1900
builtin::blessed($value) and $value->isa("Venus::Core") and $value->can("DOES")
!ll&&!rl&&r
1900
builtin::blessed($value) and $value->isa("Venus::Core")
!ll&&!rl&&r
1810
MC/DC0%0/4
builtin::blessed($value) and $value->isa("Venus::Core") and $value->can("DOES") and $value->DOES("Venus::Role::Serializable")
builtin::blessed($value) $value->isa("Venus::Core") $value->can("DOES") $value->DOES("Venus::Role::Serializable")
Truth table40%2/5
A and B and C and D
A= builtin::blessed($value)B= $value->isa("Venus::Core")C= $value->can("DOES")D= $value->DOES("Venus::Role::Serializable")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
63 && $value->isa('Venus::Core')
64 && $value->can('DOES')
65 && $value->DOES('Venus::Role::Serializable'))
66 {
67 0 return $value->serialize;
68 }
69  
70 19 ▶ if (UNIVERSAL::isa($value, 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "CODE")) 0 19 19
71 0 ▶ return sub{goto \&$value};
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 }
73  
74 19 ▶ if (UNIVERSAL::isa($value, 'REF')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "REF")) 0 19 19
75 0 return deconstruct($self, ${$value});
76 }
77  
78 19 ▶ if (UNIVERSAL::isa($value, 'REGEXP')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "REGEXP")) 0 19 19
79 0 return qr/$value/;
80 }
81  
82 19 ▶ if (UNIVERSAL::isa($value, 'SCALAR')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "SCALAR")) 1 18 19
83 1 return deconstruct($self, ${$value});
84 }
85  
86 18 ▶ if (UNIVERSAL::isa($value, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "HASH")) 2 16 18
87 2 my $result = {};
88 2 for my $key (keys %{$value}) {
89 2 $result->{$key} = deconstruct($self, $value->{$key});
90 }
91 1 return $result;
92 }
93  
94 16 ▶ if (UNIVERSAL::isa($value, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "ARRAY")) 2 14 16
95 2 my $result = [];
96 2 for my $key (keys @{$value}) {
97 8 $result->[$key] = deconstruct($self, $value->[$key]);
98 }
99 2 return $result;
100 }
101  
102 14 ▶ if (Scalar::Util::blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 1 13 14
103 1 require Venus::Throw;
104 1 my $throw = Venus::Throw->new(join('::', map ucfirst, ref($self), 'error'));
105 1 $throw->name('on.serialize.deconstruct');
106 1 $throw->message("Can't serialize properties in the object: $self");
107 1 $throw->stash(self => $self);
108 1 $throw->die;
109 }
110  
111 13 return $value;
112 }
113  
114 # EXPORTS
115  
116 128 ▶sub EXPORT {
Subroutine EXPORT: called CC=1 SCAR=0.0
Pod undocumented
117 128 ['serialize']
118 }
119  
120 119 1;
121  
122  
123  
124 =head1 NAME
125  
126 Venus::Role::Serializable - Serializable Role
127  
128 =cut
129  
130 =head1 ABSTRACT
131  
132 Serializable Role for Perl 5
133  
134 =cut
135  
136 =head1 SYNOPSIS
137  
138 package Example;
139  
140 use Venus::Class;
141  
142 with 'Venus::Role::Serializable';
143  
144 attr 'test';
145  
146 package main;
147  
148 my $example = Example->new(test => 123);
149  
150 # $example->serialize;
151  
152 # {test => 123}
153  
154 =cut
155  
156 =head1 DESCRIPTION
157  
158 This package provides a mechanism for serializing objects or the return value
159 of a dispatched method call.
160  
161 =cut
162  
163 =head1 METHODS
164  
165 This package provides the following methods:
166  
167 =cut
168  
169 =head2 serialize
170  
171 serialize(string | coderef $code, any @args) (any)
172  
173 The serialize method serializes the invocant or the return value of a
174 dispatched method call, and returns the result.
175  
176 I<Since C<1.75>>
177  
178 =over 4
179  
180 =item serialize example 1
181  
182 package Example1;
183  
184 use Venus::Class 'with';
185  
186 with 'Venus::Role::Serializable';
187  
188 sub ARGS {
189 (@_[1..$#_])
190 }
191  
192 sub DATA {
193 [@_[1..$#_]]
194 }
195  
196 package main;
197  
198 my $example1 = Example1->new(1..4);
199  
200 # bless([1..4], 'Example1')
201  
202 # my $result = $example1->serialize;
203  
204 # [1..4]
205  
206 =back
207  
208 =over 4
209  
210 =item serialize example 2
211  
212 package Example2;
213  
214 use Venus::Class 'with';
215  
216 with 'Venus::Role::Serializable';
217  
218 sub ARGS {
219 (@_[1..$#_])
220 }
221  
222 sub DATA {
223 sub{[@_[1..$#_]]}
224 }
225  
226 package main;
227  
228 my $example2 = Example2->new(1..4);
229  
230 # bless(sub{[1..4]}, 'Example2')
231  
232 # my $result = $example2->serialize;
233  
234 # sub{...}
235  
236 =back
237  
238 =over 4
239  
240 =item serialize example 3
241  
242 package Example3;
243  
244 use Venus::Class 'with';
245  
246 with 'Venus::Role::Serializable';
247  
248 sub ARGS {
249 (@_[1..$#_])
250 }
251  
252 sub DATA {
253 qr{@{[join '', @_[1..$#_]]}};
254 }
255  
256 package main;
257  
258 my $example3 = Example3->new(1..4);
259  
260 # bless(qr/1234/, 'Example3')
261  
262 # my $result = $example3->serialize;
263  
264 # qr/1234/u
265  
266 =back
267  
268 =over 4
269  
270 =item serialize example 4
271  
272 package Example4;
273  
274 use Venus::Class 'with';
275  
276 with 'Venus::Role::Serializable';
277  
278 sub ARGS {
279 (@_[1..$#_])
280 }
281  
282 sub DATA {
283 \join '', @_[1..$#_]
284 }
285  
286 package main;
287  
288 my $example4 = Example4->new(1..4);
289  
290 # bless(\'1234', 'Example4')
291  
292 # my $result = $example4->serialize;
293  
294 # "1234"
295  
296 =back
297  
298 =over 4
299  
300 =item serialize example 5
301  
302 package Example5;
303  
304 use Venus::Class 'with';
305  
306 with 'Venus::Role::Serializable';
307  
308 sub ARGS {
309 (@_[1..$#_])
310 }
311  
312 sub DATA {
313 \(my $ref = \join '', @_[1..$#_])
314 }
315  
316 package main;
317  
318 my $example5 = Example5->new(1..4);
319  
320 # bless(do{\(my $ref = \'1234')}, 'Example5')
321  
322 # my $result = $example5->serialize;
323  
324 # "1234"
325  
326 =back
327  
328 =over 4
329  
330 =item serialize example 6
331  
332 package Example6;
333  
334 use Venus::Class 'base';
335  
336 base 'Venus::Array';
337  
338 package main;
339  
340 my $example6 = Example6->new([1..4]);
341  
342 # bless(..., 'Example6')
343  
344 # my $result = $example6->serialize;
345  
346 # [1..4]
347  
348 =back
349  
350 =over 4
351  
352 =item serialize example 7
353  
354 package Example7;
355  
356 use Venus::Class 'base';
357  
358 base 'Venus::Path';
359  
360 package main;
361  
362 my $example7 = Example7->new('/path/to/somewhere');
363  
364 # bless(..., 'Example7')
365  
366 # my $result = $example7->serialize;
367  
368 # "/path/to/somewhere"
369  
370 =back
371  
372 =over 4
373  
374 =item serialize example 8
375  
376 package Example8;
377  
378 use Venus::Class 'with';
379  
380 with 'Venus::Role::Serializable';
381 with 'Venus::Role::Valuable';
382  
383 package main;
384  
385 my $example8 = Example8->new(value => 123);
386  
387 # bless(..., 'Example8')
388  
389 # my $result = $example8->serialize;
390  
391 # 123
392  
393 =back
394  
395 =over 4
396  
397 =item serialize example 9
398  
399 package Example9;
400  
401 use Venus::Class 'base', 'with';
402  
403 base 'IO::Handle';
404  
405 with 'Venus::Role::Serializable';
406  
407 package main;
408  
409 my $example9 = Example9->new;
410  
411 # bless(..., 'Example9')
412  
413 # my $result = $example9->serialize;
414  
415 # Exception! (isa Venus::Error) is "on.serialize"
416  
417 =back
418  
419 =over 4
420  
421 =item serialize example 10
422  
423 package Example10;
424  
425 use Venus::Class 'attr', 'with';
426  
427 with 'Venus::Role::Serializable';
428  
429 attr 'test';
430  
431 package main;
432  
433 use IO::Handle;
434  
435 my $example10 = Example10->new(test => IO::Handle->new);
436  
437 # bless(..., 'Example10')
438  
439 # my $result = $example10->serialize;
440  
441 # Exception! (isa Venus::Error) is "on.serialize.deconstruct"
442  
443 =back
444  
445 =cut
446  
447 =head1 AUTHORS
448  
449 Awncorp, C<awncorp@cpan.org>
450  
451 =cut
452  
453 =head1 LICENSE
454  
455 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
456  
457 This program is free software, you can redistribute it and/or modify it under
458 the terms of the Apache license version 2.0.
459  
460 =cut
« blib/lib/Venus/Role/Buildable.pm ↑ Summary blib/lib/Venus/Schema.pm »