Mars/Class.pm

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