Mars/Class.pm

filter: Statementstmt 100% 90 / 90 Branchbran 100% 18 / 18 Conditioncond 94.4% 17 / 18 MC/DCmcdc 91.6% 11 / 12 Subroutinesub 100% 20 / 20 Podpod 100% 5 / 5 totaltotal 98.7% 161 / 163 SCAR 27.7
CC
16
Cov
99%
import
16.0
BEGIN
1.0
BEGIN
1.0
CRAP 16.0
« Mars/Kind.pm ↑ Summary Mars.pm »
1 package Mars::Class;
2  
3 3 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORT
9  
10 sub import {
11 35 ▶ my ($self, @args) = @_;
Subroutine import: called CC=16 SCAR=27.7
12  
13 35 my $from = caller;
14  
15 35 require Mars::Kind::Class;
16  
17 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 35 ▶ my %exports = map +($_,$_), @args ? @args : qw(
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 2 33 35
22 attr
23 base
24 false
25 from
26 role
27 test
28 true
29 with
30 );
31  
32 35 @{"${from}::ISA"} = 'Mars::Kind::Class';
33  
34 35 ▶ if ($exports{"attr"} && !*{"${from}::attr"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{"attr"} and not *{"${from}::attr";}{"CODE"}) 11 24 35
Condition66%2/3
$exports{"attr"} and not *{"${from}::attr";}{"CODE"}
!ll&&!rl&&r
02411
MC/DC50%1/2
$exports{"attr"} and not *{"${from}::attr";}{"CODE"}
$exports{"attr"} not *{"${from}::attr";}{"CODE"}
Truth table66%2/3
A and not(B)
A= $exports{"attr"}B= not *{"${from}::attr";}{"CODE"}
ABresult
0X0
100
111
35 11 ▶ *{"${from}::attr"} = sub {@_ = ($from, @_); goto \&attr};
Subroutine __ANON__: called CC=1 SCAR=0.0
36 }
37 35 ▶ if ($exports{"base"} && !*{"${from}::base"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{"base"} and not *{"${from}::base";}{"CODE"}) 9 26 35
Condition100%3/3
$exports{"base"} and not *{"${from}::base";}{"CODE"}
!ll&&!rl&&r
2249
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 9 ▶ *{"${from}::base"} = sub {@_ = ($from, @_); goto \&base};
Subroutine __ANON__: called CC=1 SCAR=0.0
39 }
40 35 ▶ if (!*{"${from}::false"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{"${from}::false";}{"CODE"}) 11 24 35
41 11 ▶ *{"${from}::false"} = sub {require Mars; Mars::false()};
Subroutine __ANON__: called CC=1 SCAR=0.0
42 }
43 35 ▶ if ($exports{"from"} && !*{"${from}::from"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{"from"} and not *{"${from}::from";}{"CODE"}) 9 26 35
Condition100%3/3
$exports{"from"} and not *{"${from}::from";}{"CODE"}
!ll&&!rl&&r
2249
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 9 ▶ *{"${from}::from"} = sub {@_ = ($from, @_); goto \&from};
Subroutine __ANON__: called CC=1 SCAR=0.0
45 }
46 35 ▶ if ($exports{"role"} && !*{"${from}::role"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{"role"} and not *{"${from}::role";}{"CODE"}) 9 26 35
Condition100%3/3
$exports{"role"} and not *{"${from}::role";}{"CODE"}
!ll&&!rl&&r
2249
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 9 ▶ *{"${from}::role"} = sub {@_ = ($from, @_); goto \&role};
Subroutine __ANON__: called CC=1 SCAR=0.0
48 }
49 35 ▶ if ($exports{"test"} && !*{"${from}::test"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{"test"} and not *{"${from}::test";}{"CODE"}) 9 26 35
Condition100%3/3
$exports{"test"} and not *{"${from}::test";}{"CODE"}
!ll&&!rl&&r
2249
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 9 ▶ *{"${from}::test"} = sub {@_ = ($from, @_); goto \&test};
Subroutine __ANON__: called CC=1 SCAR=0.0
51 }
52 35 ▶ if (!*{"${from}::true"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{"${from}::true";}{"CODE"}) 11 24 35
53 11 ▶ *{"${from}::true"} = sub {require Mars; Mars::true()};
Subroutine __ANON__: called CC=1 SCAR=0.0
54 }
55 35 ▶ if ($exports{"with"} && !*{"${from}::with"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{"with"} and not *{"${from}::with";}{"CODE"}) 9 26 35
Condition100%3/3
$exports{"with"} and not *{"${from}::with";}{"CODE"}
!ll&&!rl&&r
2249
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 9 ▶ *{"${from}::with"} = sub {@_ = ($from, @_); goto \&test};
Subroutine __ANON__: called CC=1 SCAR=0.0
57 }
58  
59 35 ${"${from}::META"} = {};
60  
61 35 return $self;
62 }
63  
64 sub attr {
65 15 ▶ my ($from, @args) = @_;
Subroutine attr: called CC=1 SCAR=0.0
Pod documented
66  
67 15 $from->ATTR(@args);
68  
69 15 return $from;
70 }
71  
72 sub base {
73 5 ▶ my ($from, @args) = @_;
Subroutine base: called CC=1 SCAR=0.0
Pod documented
74  
75 5 $from->BASE(@args);
76  
77 5 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 15 ▶ my ($from, @args) = @_;
Subroutine test: called CC=1 SCAR=0.0
Pod documented
98  
99 15 $from->TEST(@args);
100  
101 13 return $from;
102 }
103  
104 3 1;
105  
106  
107  
108 =head1 NAME
109  
110 Mars::Class - Class Declaration
111  
112 =cut
113  
114 =head1 ABSTRACT
115  
116 Class Declaration for Perl 5
117  
118 =cut
119  
120 =head1 SYNOPSIS
121  
122 package Person;
123  
124 use Mars::Class 'attr';
125  
126 attr 'fname';
127 attr 'lname';
128  
129 package Identity;
130  
131 use Mars::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 Mars::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 Mars::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<Mars::Kind::Class> which provides object construction and
200 lifecycle L<hooks|Mars::Kind>.
201  
202 =cut
203  
204 =head1 FUNCTIONS
205  
206 This package provides the following functions:
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.01>>
218  
219 =over 4
220  
221 =item attr example 1
222  
223 package Example;
224  
225 use Mars::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.01>>
244  
245 =over 4
246  
247 =item base example 1
248  
249 package Entity;
250  
251 use Mars::Class;
252  
253 sub output {
254 return;
255 }
256  
257 package Example;
258  
259 use Mars::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.01>>
278  
279 =over 4
280  
281 =item false example 1
282  
283 package Example;
284  
285 use Mars::Class;
286  
287 my $false = false;
288  
289 # 0
290  
291 =back
292  
293 =over 4
294  
295 =item false example 2
296  
297 package Example;
298  
299 use Mars::Class;
300  
301 my $true = !false;
302  
303 # 1
304  
305 =back
306  
307 =cut
308  
309 =head2 from
310  
311 from(Str $name) (Str)
312  
313 The from function registers one or more base classes for the calling package
314 and performs an L<"audit"|Mars::Kind/AUDIT>. This function is always exported
315 unless a routine of the same name already exists.
316  
317 I<Since C<0.03>>
318  
319 =over 4
320  
321 =item from example 1
322  
323 package Entity;
324  
325 use Mars::Class;
326  
327 sub AUDIT {
328 my ($self, $from) = @_;
329 die "Missing startup" if !$from->can('startup');
330 die "Missing shutdown" if !$from->can('shutdown');
331 }
332  
333 package Example;
334  
335 use Mars::Class;
336  
337 attr 'startup';
338 attr 'shutdown';
339  
340 from 'Entity';
341  
342 # "Example"
343  
344 =back
345  
346 =cut
347  
348 =head2 role
349  
350 role(Str $name) (Str)
351  
352 The role function registers and consumes roles for the calling package. This
353 function is always exported unless a routine of the same name already exists.
354  
355 I<Since C<0.01>>
356  
357 =over 4
358  
359 =item role example 1
360  
361 package Ability;
362  
363 use Mars::Role;
364  
365 sub action {
366 return;
367 }
368  
369 package Example;
370  
371 use Mars::Class;
372  
373 role 'Ability';
374  
375 # "Example"
376  
377 =back
378  
379 =over 4
380  
381 =item role example 2
382  
383 package Ability;
384  
385 use Mars::Role;
386  
387 sub action {
388 return;
389 }
390  
391 sub EXPORT {
392 return ['action'];
393 }
394  
395 package Example;
396  
397 use Mars::Class;
398  
399 role 'Ability';
400  
401 # "Example"
402  
403 =back
404  
405 =cut
406  
407 =head2 test
408  
409 test(Str $name) (Str)
410  
411 The test function registers and consumes roles for the calling package and
412 performs an L<"audit"|Mars::Kind/AUDIT>, effectively allowing a role to act as
413 an interface. This function is always exported unless a routine of the same
414 name already exists.
415  
416 I<Since C<0.01>>
417  
418 =over 4
419  
420 =item test example 1
421  
422 package Actual;
423  
424 use Mars::Role;
425  
426 package Example;
427  
428 use Mars::Class;
429  
430 test 'Actual';
431  
432 # "Example"
433  
434 =back
435  
436 =over 4
437  
438 =item test example 2
439  
440 package Actual;
441  
442 use Mars::Role;
443  
444 sub AUDIT {
445 die "Example is not an 'actual' thing" if $_[1]->isa('Example');
446 }
447  
448 package Example;
449  
450 use Mars::Class;
451  
452 test 'Actual';
453  
454 # "Example"
455  
456 =back
457  
458 =cut
459  
460 =head2 true
461  
462 true() (Bool)
463  
464 The true function returns a truthy boolean value which is designed to be
465 practically indistinguishable from the conventional numerical C<1> value. This
466 function is always exported unless a routine of the same name already exists.
467  
468 I<Since C<0.01>>
469  
470 =over 4
471  
472 =item true example 1
473  
474 package Example;
475  
476 use Mars::Class;
477  
478 my $true = true;
479  
480 # 1
481  
482 =back
483  
484 =over 4
485  
486 =item true example 2
487  
488 package Example;
489  
490 use Mars::Class;
491  
492 my $false = !true;
493  
494 # 0
495  
496 =back
497  
498 =cut
499  
500 =head2 with
501  
502 with(Str $name) (Str)
503  
504 The with function registers and consumes roles for the calling package. This
505 function is an alias of the L</test> function and will perform an
506 L<"audit"|Mars::Kind/AUDIT> if present. This function is always exported unless
507 a routine of the same name already exists.
508  
509 I<Since C<0.01>>
510  
511 =over 4
512  
513 =item with example 1
514  
515 package Understanding;
516  
517 use Mars::Role;
518  
519 sub knowledge {
520 return;
521 }
522  
523 package Example;
524  
525 use Mars::Class;
526  
527 with 'Understanding';
528  
529 # "Example"
530  
531 =back
532  
533 =over 4
534  
535 =item with example 2
536  
537 package Understanding;
538  
539 use Mars::Role;
540  
541 sub knowledge {
542 return;
543 }
544  
545 sub EXPORT {
546 return ['knowledge'];
547 }
548  
549 package Example;
550  
551 use Mars::Class;
552  
553 with 'Understanding';
554  
555 # "Example"
556  
557 =back
558  
559 =cut
560  
561 =head1 AUTHORS
562  
563 Awncorp, C<awncorp@cpan.org>
564  
565 =cut
« Mars/Kind.pm ↑ Summary Mars.pm »