blib/lib/Venus/Prototype.pm

filter: Statementstmt 74.2% 52 / 70 Branchbran 53.1% 17 / 32 Conditioncond 63.1% 12 / 19 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 82.3% 14 / 17 Podpod 62.5% 5 / 8 totaltotal 67.0% 106 / 158 CC 25 SCAR 21.2
CC
25
Cov
67%
set
30.0
_clone
14.3
get
6.0
CRAP 8.3
« blib/lib/Venus/Role/Comparable.pm ↑ Summary blib/lib/Venus/Run.pm »
1 package Venus::Prototype;
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 # IMPORTS
9  
10 2 ▶use Venus::Class 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # INHERITS
13  
14 2 base 'Venus::Kind::Utility';
15  
16 # INTEGRATES
17  
18 2 with 'Venus::Role::Valuable';
19 2 with 'Venus::Role::Buildable';
20 2 with 'Venus::Role::Proxyable';
21  
22 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # BUILDERS
25  
26 18 ▶sub build_args {
Subroutine build_args: called CC=3 SCAR=11.0
Pod undocumented
27 18 my ($self, $data) = @_;
28  
29 18 ▶ if (keys %$data == 1 && exists $data->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$data == 1 and exists $data->{"value"}) 1 17 18
Condition100%3/3
keys %$data == 1 and exists $data->{"value"}
!ll&&!rl&&r
981
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
30 1 return _clone($data);
31 }
32 return {
33 17 value => _clone($data)
34 };
35 }
36  
37 28 ▶sub build_proxy {
Subroutine build_proxy: called CC=3 SCAR=11.1
Pod undocumented
38 28 my ($self, $package, $method, @args) = @_;
39  
40 # as a method/routine
41 6 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 6 $self->{value}{"\&$method"}->($self, @args)
43 }
44 28 ▶ if defined $self->{value}{"\&$method"};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"value"}{"&$method"} 6 22 28
45  
46 # as a property/attribute
47 22 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
48 22 ▶ @args ? $self->{value}{"\$$method"} = $args[0] : $self->{value}{"\$$method"}
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 4 18 22
49 }
50 22 ▶ if exists $self->{value}{"\$$method"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"value"}{"\$$method"} 22 0 22
51  
52 0 return undef;
53 }
54  
55 # HOOKS
56  
57 74 ▶sub _clone {
Subroutine _clone: called CC=8 SCAR=26.6
58 74 my ($data) = @_;
59  
60 74 ▶ if (!defined($data)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $data) { } 0 74 74
elsif (not builtin::blessed($data) and ref $data eq 'HASH') { } 30 44 74
elsif (not builtin::blessed($data) and ref $data eq 'ARRAY') { } 0 44 44
61 0 return $data;
62 }
63 ▶ elsif (!Scalar::Util::blessed($data) && ref($data) eq 'HASH') {
Condition66%2/3
not builtin::blessed($data) and ref $data eq "HASH"
!ll&&!rl&&r
04430
MC/DC50%1/2
not builtin::blessed($data) and ref $data eq "HASH"
not builtin::blessed($data) ref $data eq "HASH"
Truth table66%2/3
not(A) and B
A= not builtin::blessed($data)B= ref $data eq "HASH"
ABresult
0X0
100
111
64 30 my $copy = {};
65 30 for my $key (keys %$data) {
66 48 $copy->{$key} = _clone($data->{$key});
67 }
68 30 return $copy;
69 }
70 ▶ elsif (!Scalar::Util::blessed($data) && ref($data) eq 'ARRAY') {
Condition33%1/3
not builtin::blessed($data) and ref $data eq "ARRAY"
!ll&&!rl&&r
0440
MC/DC0%0/2
not builtin::blessed($data) and ref $data eq "ARRAY"
not builtin::blessed($data) ref $data eq "ARRAY"
Truth table33%1/3
not(A) and B
A= not builtin::blessed($data)B= ref $data eq "ARRAY"
ABresult
0X0
100
111
71 0 my $copy = [];
72 0 ▶ for (my $i = 0; $i < @$data; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$data) 0 0 0
73 0 $copy->[$i] = _clone($data->[$i]);
74 }
75 0 return $copy;
76 }
77 else {
78 44 return $data;
79 }
80 }
81  
82 16 ▶sub _value {
Subroutine _value: called CC=3 SCAR=11.0
83 16 my ($data) = @_;
84  
85 16 ▶ if (keys %$data == 1 && exists $data->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$data == 1 and exists $data->{'value'}) { } 8 8 16
Condition100%3/3
keys %$data == 1 and exists $data->{"value"}
!ll&&!rl&&r
358
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
86 8 return $data->{value};
87 }
88 else {
89 8 return $data;
90 }
91 }
92  
93 # METHODS
94  
95 4 ▶sub apply {
Subroutine apply: called CC=2 SCAR=6.9
Pod documented
96 4 my ($self, $data) = @_;
97  
98 4 ▶ $data ||= {};
Condition100%2/2
$data ||= {}
l!l
31
MC/DC100%1/1
$data ||= {}
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
99  
100 4 return $self->do('value', _clone({%{_value($self)}, %{_value($data)}}));
101 }
102  
103 2 ▶sub call {
Subroutine call: called CC=4 SCAR=14.3
Pod documented
104 2 my ($self, $method, @args) = @_;
105  
106 # as a method/routine
107 return $self->{value}{"\&$method"}->($self, @args)
108 2 ▶ if defined $self->{value}{"\&$method"};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"value"}{"&$method"} 0 2 2
109  
110 # as a property/attribute
111 return @args ? $self->{value}{"\$$method"} = $args[0] : $self->{value}{"\$$method"}
112 2 ▶ if exists $self->{value}{"\$$method"};
Branch: 2 branches
BranchTrueFalseTotal
if exists $self->{"value"}{"\$$method"} 2 0 2
@args ? : 1 1 2
113 }
114  
115 0 ▶sub default {
Subroutine default: not called CC=1 SCAR=6.9
Pod undocumented
116 0 my ($self) = @_;
117  
118 0 return {};
119 }
120  
121 4 ▶sub extend {
Subroutine extend: called CC=2 SCAR=7.0
Pod documented
122 4 my ($self, $data) = @_;
123  
124 4 ▶ $data ||= {};
Condition50%1/2
$data ||= {}
l!l
40
MC/DC0%0/1
$data ||= {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
125  
126 4 return $self->class->new(_clone({%{_value($self)}, %{_value($data)}}));
127 }
128  
129 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
130 0 my ($self, @args) = @_;
131  
132 0 ▶ return $self->value if !@args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 0 0
133  
134 0 my ($index) = @args;
135  
136 0 return $self->value->{$index};
137 }
138  
139 0 ▶sub set {
Subroutine set: not called CC=5 SCAR=34.0
Pod documented
140 0 my ($self, @args) = @_;
141  
142 0 ▶ return $self->value if !@args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 0 0
143  
144 0 ▶ return $self->value(@args) if @args == 1 && ref $args[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 and ref $args[0] eq "HASH" 0 0 0
Condition0%0/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table0%0/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
145  
146 0 my ($index, $value) = @args;
147  
148 0 ▶ return if not defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 0 0
149  
150 0 return $self->value->{$index} = $value;
151 }
152  
153 2 1;
154  
155  
156  
157 =head1 NAME
158  
159 Venus::Prototype - Prototype Class
160  
161 =cut
162  
163 =head1 ABSTRACT
164  
165 Prototype Class for Perl 5
166  
167 =cut
168  
169 =head1 SYNOPSIS
170  
171 package main;
172  
173 use Venus::Prototype;
174  
175 my $prototype = Venus::Prototype->new(
176 '$counter' => 0,
177 '&decrement' => sub { $_[0]->counter($_[0]->counter - 1) },
178 '&increment' => sub { $_[0]->counter($_[0]->counter + 1) },
179 );
180  
181 # bless({value => {...}}, 'Venus::Prototype')
182  
183 # $prototype->counter # 0
184 # $prototype->increment # 1
185 # $prototype->counter # 1
186 # $prototype->decrement # 0
187 # $prototype->counter # 0
188  
189 =cut
190  
191 =head1 DESCRIPTION
192  
193 This package provides a simple construct for enabling prototype-base
194 programming. Properties can be called as methods when prefixed with a dollar or
195 ampersand symbol. See L</call> for more details.
196  
197 =cut
198  
199 =head1 INHERITS
200  
201 This package inherits behaviors from:
202  
203 L<Venus::Kind::Utility>
204  
205 =cut
206  
207 =head1 INTEGRATES
208  
209 This package integrates behaviors from:
210  
211 L<Venus::Role::Buildable>
212  
213 L<Venus::Role::Proxyable>
214  
215 L<Venus::Role::Valuable>
216  
217 =cut
218  
219 =head1 METHODS
220  
221 This package provides the following methods:
222  
223 =cut
224  
225 =head2 apply
226  
227 apply(hashref $data) (Venus::Prototype)
228  
229 The apply method extends the underlying data structure by merging the data
230 provided, and then returns the invocant.
231  
232 I<Since C<1.50>>
233  
234 =over 4
235  
236 =item apply example 1
237  
238 package main;
239  
240 my $person = Venus::Prototype->new({
241 '$name' => '',
242 });
243  
244 $person->apply;
245  
246 # bless({value => {'$name' => ''}}, 'Venus::Prototype')
247  
248 =back
249  
250 =over 4
251  
252 =item apply example 2
253  
254 package main;
255  
256 my $person = Venus::Prototype->new({
257 '$name' => '',
258 });
259  
260 $person->apply({
261 '$name' => 'Elliot Alderson',
262 });
263  
264 # bless({value => {'$name' => 'Elliot Alderson'}}, 'Venus::Prototype')
265  
266 =back
267  
268 =over 4
269  
270 =item apply example 3
271  
272 package main;
273  
274 my $person = Venus::Prototype->new({
275 '$name' => '',
276 '&greet' => sub {'hello'},
277 });
278  
279 $person->apply({
280 '$name' => 'Elliot Alderson',
281 });
282  
283 # bless({value => {...}}, 'Venus::Prototype')
284  
285 =back
286  
287 =cut
288  
289 =head2 call
290  
291 call(string $method, any @args) (any)
292  
293 The call method dispatches method calls based on the method name provided and
294 the state of the object, and returns the results. If the method name provided
295 matches an object property of the same name with an ampersand prefix, denoting
296 a method, then the dispatched method call acts as a method call providing the
297 invocant as the first argument. If the method name provided matches an object
298 property of the same name with a dollar sign prefix, denoting an attribute,
299 then the dispatched method call acts as an attribute accessor call. This method
300 is also useful for calling virtual methods when those virtual methods conflict
301 with the L<Venus::Prototype> methods.
302  
303 I<Since C<1.50>>
304  
305 =over 4
306  
307 =item call example 1
308  
309 package main;
310  
311 my $person = Venus::Prototype->new({
312 '$name' => 'anonymous',
313 });
314  
315 my $name = $person->call('name');
316  
317 # "anonymous"
318  
319 =back
320  
321 =over 4
322  
323 =item call example 2
324  
325 package main;
326  
327 my $person = Venus::Prototype->new({
328 '$name' => 'anonymous',
329 });
330  
331 my $name = $person->call('name', 'unidentified');
332  
333 # "unidentified"
334  
335 =back
336  
337 =cut
338  
339 =head2 extend
340  
341 extend(hashref $data) (Venus::Prototype)
342  
343 The extend method copies the underlying data structure, merging the data
344 provided if any, and then returns a new prototype object.
345  
346 I<Since C<1.50>>
347  
348 =over 4
349  
350 =item extend example 1
351  
352 package main;
353  
354 my $mrrobot = Venus::Prototype->new({
355 '$name' => 'Edward Alderson',
356 '$group' => 'fsociety',
357 });
358  
359 my $elliot = $mrrobot->extend({
360 '$name' => 'Elliot Alderson',
361 });
362  
363 # bless({value => {...}}, 'Venus::Prototype')
364  
365 =back
366  
367 =over 4
368  
369 =item extend example 2
370  
371 package main;
372  
373 my $mrrobot = Venus::Prototype->new({
374 '$name' => 'Edward Alderson',
375 '$group' => 'fsociety',
376 '$login' => { username => 'admin', password => 'secret', },
377 });
378  
379 my $elliot = $mrrobot->extend({
380 '$name' => 'Elliot Alderson',
381 '$login' => { password => '$ecr3+', },
382 });
383  
384 # bless({value => {...}}, 'Venus::Prototype')
385  
386 =back
387  
388 =over 4
389  
390 =item extend example 3
391  
392 package main;
393  
394 my $ability = {
395 '&access' => sub {time},
396 };
397  
398 my $person = Venus::Prototype->new;
399  
400 my $mrrobot = $person->extend($ability);
401  
402 my $elliot = $mrrobot->extend($ability);
403  
404 # bless({value => {...}}, 'Venus::Prototype')
405  
406 =back
407  
408 =cut
409  
410 =head2 new
411  
412 new(any @args) (Venus::Prototype)
413  
414 The new method constructs an instance of the package.
415  
416 I<Since C<4.15>>
417  
418 =over 4
419  
420 =item new example 1
421  
422 package main;
423  
424 use Venus::Prototype;
425  
426 my $new = Venus::Prototype->new;
427  
428 # bless(..., "Venus::Prototype")
429  
430 =back
431  
432 =over 4
433  
434 =item new example 2
435  
436 package main;
437  
438 use Venus::Prototype;
439  
440 my $new = Venus::Prototype->new(
441 '$counter' => 0,
442 '&decrement' => sub { $_[0]->counter($_[0]->counter - 1) },
443 '&increment' => sub { $_[0]->counter($_[0]->counter + 1) },
444 );
445  
446 # bless(..., "Venus::Prototype")
447  
448 =back
449  
450 =over 4
451  
452 =item new example 3
453  
454 package main;
455  
456 use Venus::Prototype;
457  
458 my $new = Venus::Prototype->new(value => {
459 '$counter' => 0,
460 '&decrement' => sub { $_[0]->counter($_[0]->counter - 1) },
461 '&increment' => sub { $_[0]->counter($_[0]->counter + 1) },
462 });
463  
464 # bless(..., "Venus::Prototype")
465  
466 =back
467  
468 =cut
469  
470 =head1 AUTHORS
471  
472 Awncorp, C<awncorp@cpan.org>
473  
474 =cut
475  
476 =head1 LICENSE
477  
478 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
479  
480 This program is free software, you can redistribute it and/or modify it under
481 the terms of the Apache license version 2.0.
482  
483 =cut
« blib/lib/Venus/Role/Comparable.pm ↑ Summary blib/lib/Venus/Run.pm »