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