| 1 | package MooseX::Traits::Pluggable; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 4 | $MooseX::Traits::Pluggable::VERSION = '0.12'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Scalar::Util qw/blessed reftype/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use List::MoreUtils 'uniq'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Moose::Util qw/find_meta/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Class::Load qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | our $AUTHORITY = 'id:RKITOVER'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # stolen from MX::Object::Pluggable | ||||||||||||||||||||||||||||
| 17 | has _original_class_name => ( | ||||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||
| 19 | required => 1, | ||||||||||||||||||||||||||||
| 20 | isa => 'Str', | ||||||||||||||||||||||||||||
| 21 | 9 | ▶ | default => sub { blessed $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 4 | ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 4 | ▶ | has '_trait_namespace' => ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | # no accessors or init_arg | ||||||||||||||||||||||||||||
| 26 | init_arg => undef, | ||||||||||||||||||||||||||||
| 27 | (Moose->VERSION >= 0.84 ) ? (is => 'bare') : (), | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 4 | ▶ | has '_traits_behave_like_roles' => ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | init_arg => undef, | ||||||||||||||||||||||||||||
| 32 | (Moose->VERSION >= 0.84 ) ? (is => 'bare') : (), | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | has _traits => ( | ||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||
| 37 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 38 | 11 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 4 | ); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | has _resolved_traits => ( | ||||||||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||||||||
| 43 | isa => 'ArrayRef[ClassName]', | ||||||||||||||||||||||||||||
| 44 | 11 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 4 | ); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 8 | ▶ | sub _find_trait { | ||||||||||||||||||||||||||
|
Subroutine
_find_trait: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 48 | 8 | my ($class, $base, $name) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 8 | my @search_ns = $class->meta->class_precedence_list; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 8 | for my $ns (@search_ns) { | |||||||||||||||||||||||||||
| 53 | 24 | my $full = "${ns}::${base}::${name}"; | |||||||||||||||||||||||||||
| 54 | 24 | ▶ | return $full if eval { Class::Load::load_class($full) }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | croak "Could not find a class for trait: $name"; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 37 | ▶ | my $config_val = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 61 | 37 | my ($class, $attr, @args) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 37 | my $val; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 37 | ▶ | if ($class->can($attr)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $val = $class->$attr(@args); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||
| 69 | 37 | my $attr_inst = find_meta($class)->find_attribute_by_name($attr); | |||||||||||||||||||||||||||
| 70 | 37 | ▶ | if($attr_inst->has_default) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 21 | $val = $attr_inst->default; | |||||||||||||||||||||||||||
| 72 | 21 | ▶ | if (ref($val) && reftype($val) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $val and builtin::reftype($val) eq "CODE"
MC/DC50%1/2
ref $val and builtin::reftype($val) eq "CODE"
ref $val
builtin::reftype($val) eq "CODE"
Truth table66%2/3
A and B
A= ref $valB= builtin::reftype($val) eq "CODE"
| |||||||||||||||||||||||||||||
| 73 | 10 | $val = $class->$val(@args); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 37 | return $val; | |||||||||||||||||||||||||||
| 79 | 4 | }; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 24 | ▶ | sub _transform_trait { | ||||||||||||||||||||||||||
|
Subroutine
_transform_trait: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 82 | 24 | my ($class, $name) = @_; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 24 | my $base = $config_val->($class, '_trait_namespace', $name); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 24 | ▶ | return $name unless $base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 18 | ▶ | return $1 if $name =~ /^[+](.+)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 14 | ▶ | $base = [ $base ] if !ref($base) || reftype($base) ne 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $base or builtin::reftype($base) ne "ARRAY"
MC/DC50%1/2
not ref $base or builtin::reftype($base) ne "ARRAY"
not ref $base
builtin::reftype($base) ne "ARRAY"
Truth table66%2/3
not(A) or B
A= not ref $baseB= builtin::reftype($base) ne "ARRAY"
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 14 | for my $ns (@$base) { | |||||||||||||||||||||||||||
| 92 | 18 | ▶ | if ($ns =~ /^\+(.*)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 8 | my $trait = eval { $class->_find_trait($1, $name) }; | |||||||||||||||||||||||||||
| 94 | 8 | ▶ | return $trait if defined $trait; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 14 | my $trait = join '::', $ns, $name; | |||||||||||||||||||||||||||
| 98 | 14 | ▶ | return $trait if eval { Class::Load::load_class($trait) }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 2 | croak "Could not find a class for trait: $name"; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 16 | ▶ | sub _resolve_traits { | ||||||||||||||||||||||||||
|
Subroutine
_resolve_traits: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 16 | my ($class, @traits) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | return map { | ||||||||||||||||||||||||||||
| 108 | 16 | my $transformed = $class->_transform_trait($_); | |||||||||||||||||||||||||||
| 109 | 22 | Class::Load::load_class($transformed); | |||||||||||||||||||||||||||
| 110 | 22 | $transformed; | |||||||||||||||||||||||||||
| 111 | } @traits; | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 11 | ▶ | sub new_with_traits { | ||||||||||||||||||||||||||
|
Subroutine
new_with_traits: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | 11 | my $class = shift; | |||||||||||||||||||||||||||
| 116 | 11 | $class->_build_instance_with_traits($class, @_); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 3 | ▶ | my $remove_role_methods_conflicting_with_class = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 120 | 3 | my ($meta, $orig_class, $resolved_traits) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 3 | my %class_methods; | |||||||||||||||||||||||||||
| 123 | 3 | @class_methods{ $orig_class->meta->get_method_list } = (); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 3 | delete $class_methods{meta}; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 3 | my %trait_methods; | |||||||||||||||||||||||||||
| 128 | 3 | foreach my $trait (@$resolved_traits) { | |||||||||||||||||||||||||||
| 129 | 3 | @trait_methods{ $trait->meta->get_method_list } = (); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 3 | delete $trait_methods{meta}; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 3 | foreach my $class_method (keys %class_methods) { | |||||||||||||||||||||||||||
| 135 | 30 | ▶ | $meta->remove_method($class_method) if exists $trait_methods{$class_method}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 4 | }; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 11 | ▶ | sub _build_instance_with_traits { | ||||||||||||||||||||||||||
|
Subroutine
_build_instance_with_traits: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 140 | 11 | my ($this_class, $class) = (shift, shift); | |||||||||||||||||||||||||||
| 141 | 11 | my ($hashref, %args, @others) = 0; | |||||||||||||||||||||||||||
| 142 | 11 | ▶ | if (ref($_[-1]) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 2 | %args = %{ +pop }; | |||||||||||||||||||||||||||
| 144 | 2 | @others = @_; | |||||||||||||||||||||||||||
| 145 | 2 | $hashref = 1; | |||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||
| 147 | 9 | %args = @_; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 11 | $args{_original_class_name} = $class; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 11 | ▶ | if (my $traits = delete $args{traits}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 10 | ▶ | my @traits = ref($traits) ? @$traits : ($traits); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 10 | ▶ | if (@traits) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 9 | $args{_traits} = \@traits; | |||||||||||||||||||||||||||
| 157 | 9 | my @resolved_traits = $this_class->_resolve_traits(@traits); | |||||||||||||||||||||||||||
| 158 | 8 | $args{_resolved_traits} = \@resolved_traits; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 8 | my $meta = $class->meta->create_anon_class( | |||||||||||||||||||||||||||
| 161 | superclasses => [ $class->meta->name ], | ||||||||||||||||||||||||||||
| 162 | roles => \@resolved_traits, | ||||||||||||||||||||||||||||
| 163 | cache => 1, | ||||||||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # Method attributes in inherited roles may have turned metaclass | ||||||||||||||||||||||||||||
| 167 | # to lies. CatalystX::Component::Traits related special move | ||||||||||||||||||||||||||||
| 168 | # to deal with this here. | ||||||||||||||||||||||||||||
| 169 | 8 | $meta = find_meta($meta->name); | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 8 | ▶ | $meta->add_method('meta' => sub { $meta }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
meta: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 172 | 8 | my $orig_class = $class; | |||||||||||||||||||||||||||
| 173 | 8 | $class = $meta->name; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 8 | ▶ | if ($config_val->($orig_class, '_traits_behave_like_roles')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 2 | $remove_role_methods_conflicting_with_class->($meta, $orig_class, \@resolved_traits); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 10 | my $constructor = $class->meta->constructor_name; | |||||||||||||||||||||||||||
| 182 | 10 | ▶ | confess "$class does not have a constructor defined via the MOP?" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | if !$constructor; | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 10 | ▶ | return $class->$constructor($hashref ? (@others, \%args) : %args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 9 | ▶ | sub apply_traits { | ||||||||||||||||||||||||||
|
Subroutine
apply_traits: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 189 | 9 | my ($self, $traits, $rebless_params) = @_; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 9 | ▶ | my @traits = ref($traits) ? @$traits : ($traits); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 9 | ▶ | if (@traits) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 7 | my @resolved_traits = $self->_resolve_traits(@traits); | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 6 | ▶ | $rebless_params ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$rebless_params ||= {}
MC/DC0%0/1
$rebless_params ||= {}
$rebless_params
Truth table50%1/2
A
A= $rebless_params
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 6 | $rebless_params->{_traits} = [ uniq @{ $self->_traits }, @traits ]; | |||||||||||||||||||||||||||
| 199 | $rebless_params->{_resolved_traits} = [ | ||||||||||||||||||||||||||||
| 200 | 6 | uniq @{ $self->_resolved_traits }, @resolved_traits | |||||||||||||||||||||||||||
| 201 | ]; | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 6 | for my $trait (@resolved_traits){ | |||||||||||||||||||||||||||
| 204 | 10 | $trait->meta->apply($self, rebless_params => $rebless_params); | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 5 | my $orig_class = $self->_original_class_name; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 5 | ▶ | if ($config_val->($orig_class, '_traits_behave_like_roles')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 1 | $remove_role_methods_conflicting_with_class->($self->meta, $orig_class, \@resolved_traits); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 4 | ▶ | no Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 4 | 1; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | __END__ | ||||||||||||||||||||||||||||