Mars/Role.pm

filter: Statementstmt 70.7% 63 / 89 Branchbran 100% 16 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 45.0% 9 / 20 Podpod 100% 5 / 5 totaltotal 71.5% 93 / 130 SCAR 23.3
CC
9
Cov
75%
import
9.4
base
2.0
from
2.0
CRAP 10.2
« Mars/Kind.pm ↑ Summary Mars/Class.pm »
1 package Mars::Role;
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 19 ▶ my ($self, @args) = @_;
Subroutine import: called CC=9 SCAR=22.4
12  
13 19 my $from = caller;
14  
15 19 require Mars::Kind::Role;
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 19 @{"${from}::ISA"} = 'Mars::Kind::Role';
22  
23 19 ▶ if (!*{"${from}::attr"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{"${from}::attr";}{"CODE"}) 12 7 19
24 12 ▶ *{"${from}::attr"} = sub {@_ = ($from, @_); goto \&attr};
Subroutine __ANON__: called CC=1 SCAR=0.0
25 }
26 19 ▶ if (!*{"${from}::base"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{"${from}::base";}{"CODE"}) 12 7 19
27 12 ▶ *{"${from}::base"} = sub {@_ = ($from, @_); goto \&base};
Subroutine __ANON__: not called CC=1 SCAR=1.2
28 }
29 19 ▶ if (!*{"${from}::false"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{"${from}::false";}{"CODE"}) 12 7 19
30 12 ▶ *{"${from}::false"} = sub {require Mars; Mars::false()};
Subroutine __ANON__: not called CC=1 SCAR=1.2
31 }
32 19 ▶ if (!*{"${from}::from"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{"${from}::from";}{"CODE"}) 12 7 19
33 12 ▶ *{"${from}::from"} = sub {@_ = ($from, @_); goto \&from};
Subroutine __ANON__: not called CC=1 SCAR=1.2
34 }
35 19 ▶ if (!*{"${from}::role"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{"${from}::role";}{"CODE"}) 12 7 19
36 12 ▶ *{"${from}::role"} = sub {@_ = ($from, @_); goto \&role};
Subroutine __ANON__: not called CC=1 SCAR=1.2
37 }
38 19 ▶ if (!*{"${from}::test"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{"${from}::test";}{"CODE"}) 12 7 19
39 12 ▶ *{"${from}::test"} = sub {@_ = ($from, @_); goto \&test};
Subroutine __ANON__: not called CC=1 SCAR=1.2
40 }
41 19 ▶ if (!*{"${from}::true"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{"${from}::true";}{"CODE"}) 12 7 19
42 12 ▶ *{"${from}::true"} = sub {require Mars; Mars::true()};
Subroutine __ANON__: not called CC=1 SCAR=1.2
43 }
44 19 ▶ if (!*{"${from}::with"}{"CODE"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{"${from}::with";}{"CODE"}) 12 7 19
45 12 ▶ *{"${from}::with"} = sub {@_ = ($from, @_); goto \&test};
Subroutine __ANON__: not called CC=1 SCAR=1.2
46 }
47  
48 19 ${"${from}::META"} = {};
49  
50 19 return $self;
51 }
52  
53 sub attr {
54 9 ▶ my ($from, @args) = @_;
Subroutine attr: called CC=1 SCAR=0.0
Pod documented
55  
56 9 $from->ATTR(@args);
57  
58 9 return $from;
59 }
60  
61 sub base {
62 0 ▶ my ($from, @args) = @_;
Subroutine base: not called CC=1 SCAR=6.9
Pod documented
63  
64 0 $from->BASE(@args);
65  
66 0 return $from;
67 }
68  
69 sub from {
70 0 ▶ my ($from, @args) = @_;
Subroutine from: not called CC=1 SCAR=6.9
Pod documented
71  
72 0 $from->FROM(@args);
73  
74 0 return $from;
75 }
76  
77 sub role {
78 0 ▶ my ($from, @args) = @_;
Subroutine role: not called CC=1 SCAR=6.9
Pod documented
79  
80 0 $from->ROLE(@args);
81  
82 0 return $from;
83 }
84  
85 sub test {
86 0 ▶ my ($from, @args) = @_;
Subroutine test: not called CC=1 SCAR=6.9
Pod documented
87  
88 0 $from->TEST(@args);
89  
90 0 return $from;
91 }
92  
93 3 1;
94  
95  
96  
97 =head1 NAME
98  
99 Mars::Role - Role Declaration
100  
101 =cut
102  
103 =head1 ABSTRACT
104  
105 Role 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 role builder which when used causes the consumer to
182 inherit from L<Mars::Kind::Role> which provides role construction and lifecycle
183 L<hooks|Mars::Kind>. A role differs from a L<"class"|Mars::Class> in that it
184 can't be instantiated using the C<new> method. A role can act as an interface
185 by defining the an L<"audit"|Mars::Kind/AUDIT> hook, which is invoked
186 automatically by the L<"test"|Mars::Class/test> function. The role composition
187 semantics are as follows: Routines to be consumed must be explicitly declared
188 via the L<"export"|Mars::Kind/EXPORT> hook. Routines will be copied to the
189 consumer unless they already exist (excluding routines from base classes, which
190 will be overridden). If multiple roles are consumed having routines with the
191 same name (i.e. naming collisions) the first routine copied wins.
192  
193 =cut
194  
195 =head1 FUNCTIONS
196  
197 This package provides the following functions:
198  
199 =cut
200  
201 =head2 attr
202  
203 attr(Str $name) (Str)
204  
205 The attr function creates attribute accessors for the calling package. This
206 function is always exported unless a routine of the same name already exists.
207  
208 I<Since C<0.01>>
209  
210 =over 4
211  
212 =item attr example 1
213  
214 package Example;
215  
216 use Mars::Class;
217  
218 attr 'name';
219  
220 # "Example"
221  
222 =back
223  
224 =cut
225  
226 =head2 base
227  
228 base(Str $name) (Str)
229  
230 The base function registers one or more base classes for the calling package.
231 This function is always exported unless a routine of the same name already
232 exists.
233  
234 I<Since C<0.01>>
235  
236 =over 4
237  
238 =item base example 1
239  
240 package Entity;
241  
242 use Mars::Class;
243  
244 sub output {
245 return;
246 }
247  
248 package Example;
249  
250 use Mars::Class;
251  
252 base 'Entity';
253  
254 # "Example"
255  
256 =back
257  
258 =cut
259  
260 =head2 false
261  
262 false() (Bool)
263  
264 The false function returns a falsy boolean value which is designed to be
265 practically indistinguishable from the conventional numerical C<0> value. This
266 function is always exported unless a routine of the same name already exists.
267  
268 I<Since C<0.01>>
269  
270 =over 4
271  
272 =item false example 1
273  
274 package Example;
275  
276 use Mars::Class;
277  
278 my $false = false;
279  
280 # 0
281  
282 =back
283  
284 =over 4
285  
286 =item false example 2
287  
288 package Example;
289  
290 use Mars::Class;
291  
292 my $true = !false;
293  
294 # 1
295  
296 =back
297  
298 =cut
299  
300 =head2 from
301  
302 from(Str $name) (Str)
303  
304 The from function registers one or more base classes for the calling package
305 and performs an L<"audit"|Mars::Kind/AUDIT>. This function is always exported
306 unless a routine of the same name already exists.
307  
308 I<Since C<0.03>>
309  
310 =over 4
311  
312 =item from example 1
313  
314 package Entity;
315  
316 use Mars::Role;
317  
318 attr 'startup';
319 attr 'shutdown';
320  
321 sub EXPORT {
322 ['startup', 'shutdown']
323 }
324  
325 package Record;
326  
327 use Mars::Class;
328  
329 sub AUDIT {
330 my ($self, $from) = @_;
331 die "Missing startup" if !$from->can('startup');
332 die "Missing shutdown" if !$from->can('shutdown');
333 }
334  
335 package Example;
336  
337 use Mars::Class;
338  
339 with 'Entity';
340  
341 from 'Record';
342  
343 # "Example"
344  
345 =back
346  
347 =cut
348  
349 =head2 role
350  
351 role(Str $name) (Str)
352  
353 The role function registers and consumes roles for the calling package. This
354 function is always exported unless a routine of the same name already exists.
355  
356 I<Since C<0.01>>
357  
358 =over 4
359  
360 =item role example 1
361  
362 package Ability;
363  
364 use Mars::Role;
365  
366 sub action {
367 return;
368 }
369  
370 package Example;
371  
372 use Mars::Class;
373  
374 role 'Ability';
375  
376 # "Example"
377  
378 =back
379  
380 =over 4
381  
382 =item role example 2
383  
384 package Ability;
385  
386 use Mars::Role;
387  
388 sub action {
389 return;
390 }
391  
392 sub EXPORT {
393 return ['action'];
394 }
395  
396 package Example;
397  
398 use Mars::Class;
399  
400 role 'Ability';
401  
402 # "Example"
403  
404 =back
405  
406 =cut
407  
408 =head2 test
409  
410 test(Str $name) (Str)
411  
412 The test function registers and consumes roles for the calling package and
413 performs an L<"audit"|Mars::Kind/AUDIT>, effectively allowing a role to act as
414 an interface. This function is always exported unless a routine of the same
415 name already exists.
416  
417 I<Since C<0.01>>
418  
419 =over 4
420  
421 =item test example 1
422  
423 package Actual;
424  
425 use Mars::Role;
426  
427 package Example;
428  
429 use Mars::Class;
430  
431 test 'Actual';
432  
433 # "Example"
434  
435 =back
436  
437 =over 4
438  
439 =item test example 2
440  
441 package Actual;
442  
443 use Mars::Role;
444  
445 sub AUDIT {
446 die "Example is not an 'actual' thing" if $_[1]->isa('Example');
447 }
448  
449 package Example;
450  
451 use Mars::Class;
452  
453 test 'Actual';
454  
455 # "Example"
456  
457 =back
458  
459 =cut
460  
461 =head2 true
462  
463 true() (Bool)
464  
465 The true function returns a truthy boolean value which is designed to be
466 practically indistinguishable from the conventional numerical C<1> value. This
467 function is always exported unless a routine of the same name already exists.
468  
469 I<Since C<0.01>>
470  
471 =over 4
472  
473 =item true example 1
474  
475 package Example;
476  
477 use Mars::Class;
478  
479 my $true = true;
480  
481 # 1
482  
483 =back
484  
485 =over 4
486  
487 =item true example 2
488  
489 package Example;
490  
491 use Mars::Class;
492  
493 my $false = !true;
494  
495 # 0
496  
497 =back
498  
499 =cut
500  
501 =head2 with
502  
503 with(Str $name) (Str)
504  
505 The with function registers and consumes roles for the calling package. This
506 function is an alias of the L</test> function and will perform an
507 L<"audit"|Mars::Kind/AUDIT> if present. This function is always exported unless
508 a routine of the same name already exists.
509  
510 I<Since C<0.01>>
511  
512 =over 4
513  
514 =item with example 1
515  
516 package Understanding;
517  
518 use Mars::Role;
519  
520 sub knowledge {
521 return;
522 }
523  
524 package Example;
525  
526 use Mars::Class;
527  
528 with 'Understanding';
529  
530 # "Example"
531  
532 =back
533  
534 =over 4
535  
536 =item with example 2
537  
538 package Understanding;
539  
540 use Mars::Role;
541  
542 sub knowledge {
543 return;
544 }
545  
546 sub EXPORT {
547 return ['knowledge'];
548 }
549  
550 package Example;
551  
552 use Mars::Class;
553  
554 with 'Understanding';
555  
556 # "Example"
557  
558 =back
559  
560 =cut
561  
562 =head1 AUTHORS
563  
564 Awncorp, C<awncorp@cpan.org>
565  
566 =cut
« Mars/Kind.pm ↑ Summary Mars/Class.pm »