Venus/Class.pm

filter: Statementstmt 100% 90 / 90 Branchbran 100% 18 / 18 Conditioncond 100% 18 / 18 MC/DCmcdc 100% 12 / 12 Subroutinesub 100% 20 / 20 Podpod 100% 5 / 5 totaltotal 100% 163 / 163 CC 16 SCAR 20.6
CC
16
Cov
100%
import
16.0
BEGIN
1.0
BEGIN
1.0
CRAP 7.9
« Venus/Role/Buildable.pm ↑ Summary Venus/Space.pm »
1 package Venus::Class;
2  
3 61 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 61 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORT
9  
10 sub import {
11 772 ▶ my ($self, @args) = @_;
Subroutine import: called CC=16 SCAR=27.7
12  
13 772 my $from = caller;
14  
15 772 require Venus::Core::Class;
16  
17 61 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 61 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 61 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 772 ▶ my %exports = map +($_,$_), @args ? @args : qw(
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 323 449 772
22 attr
23 base
24 false
25 from
26 role
27 test
28 true
29 with
30 );
31  
32 772 @{"${from}::ISA"} = 'Venus::Core::Class';
33  
34 772 ▶ if ($exports{"attr"} && !*{"${from}::attr"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{"attr"} and not *{"${from}::attr";}{"CODE"}) 442 330 772
Condition100%3/3
$exports{"attr"} and not *{"${from}::attr";}{"CODE"}
!ll&&!rl&&r
26070442
MC/DC100%2/2
$exports{"attr"} and not *{"${from}::attr";}{"CODE"}
$exports{"attr"} not *{"${from}::attr";}{"CODE"}
Truth table100%3/3
A and not(B)
A= $exports{"attr"}B= not *{"${from}::attr";}{"CODE"}
ABresult
0X0
100
111
35 442 ▶ *{"${from}::attr"} = sub {@_ = ($from, @_); goto \&attr};
Subroutine __ANON__: called CC=1 SCAR=0.0
36 }
37 772 ▶ if ($exports{"base"} && !*{"${from}::base"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{"base"} and not *{"${from}::base";}{"CODE"}) 638 134 772
Condition100%3/3
$exports{"base"} and not *{"${from}::base";}{"CODE"}
!ll&&!rl&&r
6470638
MC/DC100%2/2
$exports{"base"} and not *{"${from}::base";}{"CODE"}
$exports{"base"} not *{"${from}::base";}{"CODE"}
Truth table100%3/3
A and not(B)
A= $exports{"base"}B= not *{"${from}::base";}{"CODE"}
ABresult
0X0
100
111
38 638 ▶ *{"${from}::base"} = sub {@_ = ($from, @_); goto \&base};
Subroutine __ANON__: called CC=1 SCAR=0.0
39 }
40 772 ▶ if (!*{"${from}::false"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{"${from}::false";}{"CODE"}) 702 70 772
41 702 ▶ *{"${from}::false"} = sub {require Venus; Venus::false()};
Subroutine __ANON__: called CC=1 SCAR=0.0
42 }
43 772 ▶ if ($exports{"from"} && !*{"${from}::from"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{"from"} and not *{"${from}::from";}{"CODE"}) 379 393 772
Condition100%3/3
$exports{"from"} and not *{"${from}::from";}{"CODE"}
!ll&&!rl&&r
32370379
MC/DC100%2/2
$exports{"from"} and not *{"${from}::from";}{"CODE"}
$exports{"from"} not *{"${from}::from";}{"CODE"}
Truth table100%3/3
A and not(B)
A= $exports{"from"}B= not *{"${from}::from";}{"CODE"}
ABresult
0X0
100
111
44 379 ▶ *{"${from}::from"} = sub {@_ = ($from, @_); goto \&from};
Subroutine __ANON__: called CC=1 SCAR=0.0
45 }
46 772 ▶ if ($exports{"role"} && !*{"${from}::role"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{"role"} and not *{"${from}::role";}{"CODE"}) 379 393 772
Condition100%3/3
$exports{"role"} and not *{"${from}::role";}{"CODE"}
!ll&&!rl&&r
32370379
MC/DC100%2/2
$exports{"role"} and not *{"${from}::role";}{"CODE"}
$exports{"role"} not *{"${from}::role";}{"CODE"}
Truth table100%3/3
A and not(B)
A= $exports{"role"}B= not *{"${from}::role";}{"CODE"}
ABresult
0X0
100
111
47 379 ▶ *{"${from}::role"} = sub {@_ = ($from, @_); goto \&role};
Subroutine __ANON__: called CC=1 SCAR=0.0
48 }
49 772 ▶ if ($exports{"test"} && !*{"${from}::test"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{"test"} and not *{"${from}::test";}{"CODE"}) 379 393 772
Condition100%3/3
$exports{"test"} and not *{"${from}::test";}{"CODE"}
!ll&&!rl&&r
32370379
MC/DC100%2/2
$exports{"test"} and not *{"${from}::test";}{"CODE"}
$exports{"test"} not *{"${from}::test";}{"CODE"}
Truth table100%3/3
A and not(B)
A= $exports{"test"}B= not *{"${from}::test";}{"CODE"}
ABresult
0X0
100
111
50 379 ▶ *{"${from}::test"} = sub {@_ = ($from, @_); goto \&test};
Subroutine __ANON__: called CC=1 SCAR=0.0
51 }
52 772 ▶ if (!*{"${from}::true"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{"${from}::true";}{"CODE"}) 702 70 772
53 702 ▶ *{"${from}::true"} = sub {require Venus; Venus::true()};
Subroutine __ANON__: called CC=1 SCAR=0.0
54 }
55 772 ▶ if ($exports{"with"} && !*{"${from}::with"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{"with"} and not *{"${from}::with";}{"CODE"}) 639 133 772
Condition100%3/3
$exports{"with"} and not *{"${from}::with";}{"CODE"}
!ll&&!rl&&r
6370639
MC/DC100%2/2
$exports{"with"} and not *{"${from}::with";}{"CODE"}
$exports{"with"} not *{"${from}::with";}{"CODE"}
Truth table100%3/3
A and not(B)
A= $exports{"with"}B= not *{"${from}::with";}{"CODE"}
ABresult
0X0
100
111
56 639 ▶ *{"${from}::with"} = sub {@_ = ($from, @_); goto \&test};
Subroutine __ANON__: called CC=1 SCAR=0.0
57 }
58  
59 772 ${"${from}::META"} = {};
60  
61 772 return $self;
62 }
63  
64 sub attr {
65 789 ▶ my ($from, @args) = @_;
Subroutine attr: called CC=1 SCAR=0.0
Pod documented
66  
67 789 $from->ATTR(@args);
68  
69 789 return $from;
70 }
71  
72 sub base {
73 637 ▶ my ($from, @args) = @_;
Subroutine base: called CC=1 SCAR=0.0
Pod documented
74  
75 637 $from->BASE(@args);
76  
77 637 return $from;
78 }
79  
80 sub from {
81 2 ▶ my ($from, @args) = @_;
Subroutine from: called CC=1 SCAR=0.0
Pod documented
82  
83 2 $from->FROM(@args);
84  
85 2 return $from;
86 }
87  
88 sub role {
89 4 ▶ my ($from, @args) = @_;
Subroutine role: called CC=1 SCAR=0.0
Pod documented
90  
91 4 $from->ROLE(@args);
92  
93 4 return $from;
94 }
95  
96 sub test {
97 1255 ▶ my ($from, @args) = @_;
Subroutine test: called CC=1 SCAR=0.0
Pod documented
98  
99 1255 $from->TEST(@args);
100  
101 1253 return $from;
102 }
103  
104 61 1;
105  
106  
107  
108 =head1 NAME
109  
110 Venus::Class - Class Builder
111  
112 =cut
113  
114 =head1 ABSTRACT
115  
116 Class Builder for Perl 5
117  
118 =cut
119  
120 =head1 SYNOPSIS
121  
122 package Person;
123  
124 use Venus::Class 'attr';
125  
126 attr 'fname';
127 attr 'lname';
128  
129 package Identity;
130  
131 use Venus::Role 'attr';
132  
133 attr 'id';
134 attr 'login';
135 attr 'password';
136  
137 sub EXPORT {
138 # explicitly declare routines to be consumed
139 ['id', 'login', 'password']
140 }
141  
142 package Authenticable;
143  
144 use Venus::Role;
145  
146 sub authenticate {
147 return true;
148 }
149  
150 sub AUDIT {
151 my ($self, $from) = @_;
152 # ensure the caller has a login and password when consumed
153 die "${from} missing the login attribute" if !$from->can('login');
154 die "${from} missing the password attribute" if !$from->can('password');
155 }
156  
157 sub BUILD {
158 my ($self, $data) = @_;
159 $self->{auth} = undef;
160 return $self;
161 }
162  
163 sub EXPORT {
164 # explicitly declare routines to be consumed
165 ['authenticate']
166 }
167  
168 package User;
169  
170 use Venus::Class;
171  
172 base 'Person';
173  
174 with 'Identity';
175  
176 attr 'email';
177  
178 test 'Authenticable';
179  
180 sub valid {
181 my ($self) = @_;
182 return $self->login && $self->password ? true : false;
183 }
184  
185 package main;
186  
187 my $user = User->new(
188 fname => 'Elliot',
189 lname => 'Alderson',
190 );
191  
192 # bless({fname => 'Elliot', lname => 'Alderson'}, 'User')
193  
194 =cut
195  
196 =head1 DESCRIPTION
197  
198 This package provides a class builder which when used causes the consumer to
199 inherit from L<Venus::Core::Class> which provides object construction and
200 lifecycle L<hooks|Venus::Core>.
201  
202 =cut
203  
204 =head1 METHODS
205  
206 This package provides the following methods:
207  
208 =cut
209  
210 =head2 attr
211  
212 attr(Str $name) (Str)
213  
214 The attr function creates attribute accessors for the calling package. This
215 function is always exported unless a routine of the same name already exists.
216  
217 I<Since C<0.10>>
218  
219 =over 4
220  
221 =item attr example 1
222  
223 package Example;
224  
225 use Venus::Class;
226  
227 attr 'name';
228  
229 # "Example"
230  
231 =back
232  
233 =cut
234  
235 =head2 base
236  
237 base(Str $name) (Str)
238  
239 The base function registers one or more base classes for the calling package.
240 This function is always exported unless a routine of the same name already
241 exists.
242  
243 I<Since C<0.10>>
244  
245 =over 4
246  
247 =item base example 1
248  
249 package Entity;
250  
251 use Venus::Class;
252  
253 sub output {
254 return;
255 }
256  
257 package Example;
258  
259 use Venus::Class;
260  
261 base 'Entity';
262  
263 # "Example"
264  
265 =back
266  
267 =cut
268  
269 =head2 false
270  
271 false() (Bool)
272  
273 The false function returns a falsy boolean value which is designed to be
274 practically indistinguishable from the conventional numerical C<0> value. This
275 function is always exported unless a routine of the same name already exists.
276  
277 I<Since C<0.10>>
278  
279 =over 4
280  
281 =item false example 1
282  
283 package Example;
284  
285 use Venus::Class;
286  
287 my $false = false;
288  
289 # 0
290  
291 =back
292  
293 =cut
294  
295 =head2 from
296  
297 from(Str $name) (Str)
298  
299 The from function registers one or more base classes for the calling package
300 and performs an L<"audit"|Venus::Core/AUDIT>. This function is always exported
301 unless a routine of the same name already exists.
302  
303 I<Since C<0.10>>
304  
305 =over 4
306  
307 =item from example 1
308  
309 package Entity;
310  
311 use Venus::Class;
312  
313 sub AUDIT {
314 my ($self, $from) = @_;
315 die "Missing startup" if !$from->can('startup');
316 die "Missing shutdown" if !$from->can('shutdown');
317 }
318  
319 package Example;
320  
321 use Venus::Class;
322  
323 attr 'startup';
324 attr 'shutdown';
325  
326 from 'Entity';
327  
328 # "Example"
329  
330 =back
331  
332 =cut
333  
334 =head2 role
335  
336 role(Str $name) (Str)
337  
338 The role function registers and consumes roles for the calling package. This
339 function is always exported unless a routine of the same name already exists.
340  
341 I<Since C<0.10>>
342  
343 =over 4
344  
345 =item role example 1
346  
347 package Ability;
348  
349 use Venus::Role;
350  
351 sub action {
352 return;
353 }
354  
355 package Example;
356  
357 use Venus::Class;
358  
359 role 'Ability';
360  
361 # "Example"
362  
363 =back
364  
365 =over 4
366  
367 =item role example 2
368  
369 package Ability;
370  
371 use Venus::Role;
372  
373 sub action {
374 return;
375 }
376  
377 sub EXPORT {
378 return ['action'];
379 }
380  
381 package Example;
382  
383 use Venus::Class;
384  
385 role 'Ability';
386  
387 # "Example"
388  
389 =back
390  
391 =cut
392  
393 =head2 test
394  
395 test(Str $name) (Str)
396  
397 The test function registers and consumes roles for the calling package and
398 performs an L<"audit"|Venus::Core/AUDIT>, effectively allowing a role to act as
399 an interface. This function is always exported unless a routine of the same
400 name already exists.
401  
402 I<Since C<0.10>>
403  
404 =over 4
405  
406 =item test example 1
407  
408 package Actual;
409  
410 use Venus::Role;
411  
412 package Example;
413  
414 use Venus::Class;
415  
416 test 'Actual';
417  
418 # "Example"
419  
420 =back
421  
422 =over 4
423  
424 =item test example 2
425  
426 package Actual;
427  
428 use Venus::Role;
429  
430 sub AUDIT {
431 die "Example is not an 'actual' thing" if $_[1]->isa('Example');
432 }
433  
434 package Example;
435  
436 use Venus::Class;
437  
438 test 'Actual';
439  
440 # "Example"
441  
442 =back
443  
444 =cut
445  
446 =head2 true
447  
448 true() (Bool)
449  
450 The true function returns a truthy boolean value which is designed to be
451 practically indistinguishable from the conventional numerical C<1> value. This
452 function is always exported unless a routine of the same name already exists.
453  
454 I<Since C<0.10>>
455  
456 =over 4
457  
458 =item true example 1
459  
460 package Example;
461  
462 use Venus::Class;
463  
464 my $true = true;
465  
466 # 1
467  
468 =back
469  
470 =over 4
471  
472 =item true example 2
473  
474 package Example;
475  
476 use Venus::Class;
477  
478 my $false = !true;
479  
480 # 0
481  
482 =back
483  
484 =cut
485  
486 =head2 with
487  
488 with(Str $name) (Str)
489  
490 The with function registers and consumes roles for the calling package. This
491 function is an alias of the L</test> function and will perform an
492 L<"audit"|Venus::Core/AUDIT> if present. This function is always exported
493 unless a routine of the same name already exists.
494  
495 I<Since C<0.10>>
496  
497 =over 4
498  
499 =item with example 1
500  
501 package Understanding;
502  
503 use Venus::Role;
504  
505 sub knowledge {
506 return;
507 }
508  
509 package Example;
510  
511 use Venus::Class;
512  
513 with 'Understanding';
514  
515 # "Example"
516  
517 =back
518  
519 =over 4
520  
521 =item with example 2
522  
523 package Understanding;
524  
525 use Venus::Role;
526  
527 sub knowledge {
528 return;
529 }
530  
531 sub EXPORT {
532 return ['knowledge'];
533 }
534  
535 package Example;
536  
537 use Venus::Class;
538  
539 with 'Understanding';
540  
541 # "Example"
542  
543 =back
544  
545 =cut
« Venus/Role/Buildable.pm ↑ Summary Venus/Space.pm »