blib/lib/Venus/Prototype.pm

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