| 1 | package MooX::TaggedAttributes::Role; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # ABSTRACT: "Parent" Tag Role | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | ## no critic | ||||||||||||||||||||
| 6 | 6 | our $VERSION = '0.18'; | |||||||||||||||||||
| 7 | ## use critic | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 6 | ▶ | use MRO::Compat; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 6 | ▶ | use Scalar::Util (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 6 | ▶ | use MooX::TaggedAttributes (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 6 | ▶ | use Sub::Name (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # Moo::Role won't compose anything before it was used into a consuming | ||||||||||||||||||||
| 16 | # package. | ||||||||||||||||||||
| 17 | 6 | ▶ | use Moo::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # sub import; | ||||||||||||||||||||
| 20 | # *import = \&MooX::TaggedAttributes::import; | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 92 | ▶ | my $maybe_next_method = sub { ( shift )->maybe::next::method }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | # this modifier is run once for each composition of a tag role into | ||||||||||||||||||||
| 25 | # the class. role composition is orthogonal to class inheritance, so we | ||||||||||||||||||||
| 26 | # need to carefully handle both | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | # see http://www.nntp.perl.org/group/perl.moose/2015/01/msg287{6,7,8}.html, | ||||||||||||||||||||
| 29 | # but note that djerius' published solution was incomplete. | ||||||||||||||||||||
| 30 | 92 | ▶ | around _tag_list => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | # 1. call &$orig to handle tag role compositions into the current class | ||||||||||||||||||||
| 33 | # 2. call up the inheritance stack to handle parent class tag role compositions. | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 92 | my $orig = shift; | |||||||||||||||||||
| 36 | 92 | my $package = caller; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | # create the proper environment context for next::can | ||||||||||||||||||||
| 39 | 92 | my $code | |||||||||||||||||||
| 40 | = Sub::Name::subname( "${package}::_tag_list" => $maybe_next_method ); | ||||||||||||||||||||
| 41 | 92 | my $next = $_[0]->$code; | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | # say STDERR "$package => $code";; | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 92 | ▶ | return [ @{&$orig}, $next ? @{$next} : () ]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 6 | }; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | # _tags can't be lazy; we must resolve the tags and attributes at | ||||||||||||||||||||
| 50 | # object creation time in case a role is modified after this object | ||||||||||||||||||||
| 51 | # is created, as we scan both clsses and roles to gather the tags. | ||||||||||||||||||||
| 52 | # classes should be immutable after the first instantiation | ||||||||||||||||||||
| 53 | # of an object (but see RT#101631), but roles aren't. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # We also need to identify when a role has been added to an *object* | ||||||||||||||||||||
| 56 | # which adds tagged attributes. TODO: make this work. | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | # this is where all of the tags get stored while a class is being | ||||||||||||||||||||
| 59 | # built up. eventually they are condensed into a simple hash via | ||||||||||||||||||||
| 60 | # _build_cache | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 92 | ▶ | sub _tag_list { [] } | ||||||||||||||||||
|
Subroutine
_tag_list: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 68 | ▶ | sub _tags { | ||||||||||||||||||
|
Subroutine
_tags: called CC=4 SCAR=15.0
| |||||||||||||||||||||
| 78 | 68 | my $class = Scalar::Util::blessed $_[0]; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | # called as an object method? | ||||||||||||||||||||
| 81 | 68 | ▶ | if ( defined $class ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 16 | ▶ | return $MooX::TaggedAttributes::TAGCACHE{$class} | ||||||||||||||||||
|
Condition33%1/3
$MooX::TaggedAttributes::TAGCACHE{$class} //= "MooX::TaggedAttributes::Cache"->new($class)
MC/DC0%0/2
$MooX::TaggedAttributes::TAGCACHE{$class} //= "MooX::TaggedAttributes::Cache"->new($class)
$MooX::TaggedAttributes::TAGCACHE{$class}
"MooX::TaggedAttributes::Cache"->new($class)
Truth table33%1/3
A //= B
A= $MooX::TaggedAttributes::TAGCACHE{$class}B= "MooX::TaggedAttributes::Cache"->new($class)
| |||||||||||||||||||||
| 83 | //= MooX::TaggedAttributes::Cache->new( $class ); | ||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||
| 87 | 52 | $class = $_[0]; | |||||||||||||||||||
| 88 | 52 | ▶ | return $MooX::TaggedAttributes::TAGCACHE{$class} | ||||||||||||||||||
|
Condition33%1/3
$MooX::TaggedAttributes::TAGCACHE{$class} // "MooX::TaggedAttributes::Cache"->new($class)
MC/DC0%0/2
$MooX::TaggedAttributes::TAGCACHE{$class} // "MooX::TaggedAttributes::Cache"->new($class)
$MooX::TaggedAttributes::TAGCACHE{$class}
"MooX::TaggedAttributes::Cache"->new($class)
Truth table33%1/3
A // B
A= $MooX::TaggedAttributes::TAGCACHE{$class}B= "MooX::TaggedAttributes::Cache"->new($class)
| |||||||||||||||||||||
| 89 | // MooX::TaggedAttributes::Cache->new( $class ); | ||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 6 | 1; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||