| 1 | package Mouse::Meta::Role::Application; | ||||||||||||||||||||||||||||
| 2 | 82 | ▶ | use Mouse::Util qw(:meta); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 352 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=28.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 5 | 352 | my $class = shift; | |||||||||||||||||||||||||||
| 6 | 352 | my $args = $class->Mouse::Object::BUILDARGS(@_); | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 352 | ▶ | if(exists $args->{exclude} or exists $args->{alias}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args->{"exclude"} or exists $args->{"alias"}
MC/DC0%0/2
exists $args->{"exclude"} or exists $args->{"alias"}
exists $args->{"exclude"}
exists $args->{"alias"}
Truth table33%1/3
A or B
A= exists $args->{"exclude"}B= exists $args->{"alias"}
| |||||||||||||||||||||||||||||
| 9 | 0 | warnings::warnif(deprecated => | |||||||||||||||||||||||||||
| 10 | 'The alias and excludes options for role application have been' | ||||||||||||||||||||||||||||
| 11 | . ' renamed -alias and -exclude'); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | if($args->{alias} && !exists $args->{-alias}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"alias"} and not exists $args->{"-alias"}
MC/DC0%0/2
$args->{"alias"} and not exists $args->{"-alias"}
$args->{"alias"}
not exists $args->{"-alias"}
Truth table0%0/3
A and not(B)
A= $args->{"alias"}B= not exists $args->{"-alias"}
| |||||||||||||||||||||||||||||
| 14 | 0 | $args->{-alias} = $args->{alias}; | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | if($args->{excludes} && !exists $args->{-excludes}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"excludes"} and not exists $args->{"-excludes"}
MC/DC0%0/2
$args->{"excludes"} and not exists $args->{"-excludes"}
$args->{"excludes"}
not exists $args->{"-excludes"}
Truth table0%0/3
A and not(B)
A= $args->{"excludes"}B= not exists $args->{"-excludes"}
| |||||||||||||||||||||||||||||
| 17 | 0 | $args->{-excludes} = $args->{excludes}; | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 352 | $args->{aliased_methods} = {}; | |||||||||||||||||||||||||||
| 22 | 352 | ▶ | if(my $alias = $args->{-alias}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 30 | @{$args->{aliased_methods}}{ values %{$alias} } = (); | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 352 | ▶ | if(my $excludes = $args->{-excludes}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 24 | $args->{-excludes} = {}; # replace with a hash ref | |||||||||||||||||||||||||||
| 28 | 24 | ▶ | if(ref $excludes){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 13 | %{$args->{-excludes}} = (map{ $_ => undef } @{$excludes}); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | else{ | ||||||||||||||||||||||||||||
| 32 | 11 | $args->{-excludes}{$excludes} = undef; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 352 | my $self = bless $args, $class; | |||||||||||||||||||||||||||
| 36 | 352 | ▶ | if($class ne __PACKAGE__){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 50 | $self->meta->_initialize_object($self, $args); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 352 | return $self; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 352 | ▶ | sub apply { | ||||||||||||||||||||||||||
|
Subroutine
apply: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 352 | my($self, $role, $consumer, @extra) = @_; | |||||||||||||||||||||||||||
| 44 | 352 | my $instance; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 352 | ▶ | if(Mouse::Util::is_a_metaclass($consumer)) { # Application::ToClass | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 47 | 169 | $self->{_to} = 'class'; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | elsif(Mouse::Util::is_a_metarole($consumer)) { # Application::ToRole | ||||||||||||||||||||||||||||
| 50 | 159 | $self->{_to} = 'role'; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | else { # Appplication::ToInstance | ||||||||||||||||||||||||||||
| 53 | 24 | $self->{_to} = 'instance'; | |||||||||||||||||||||||||||
| 54 | 24 | $instance = $consumer; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 24 | my $meta = Mouse::Util::class_of($instance); | |||||||||||||||||||||||||||
| 57 | 24 | ▶ | $consumer = ($meta || 'Mouse::Meta::Class') | ||||||||||||||||||||||||||
|
Condition100%2/2
$meta || 'Mouse::Meta::Class'
MC/DC100%1/1
$meta || 'Mouse::Meta::Class'
$meta
Truth table100%2/2
A
A= $meta
| |||||||||||||||||||||||||||||
| 58 | ->create_anon_class( | ||||||||||||||||||||||||||||
| 59 | superclasses => [ref $instance], | ||||||||||||||||||||||||||||
| 60 | roles => [$role], | ||||||||||||||||||||||||||||
| 61 | cache => 0, | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | in_application_to_instance => 1, # suppress to apply roles | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | #$self->check_role_exclusions($role, $consumer, @extra); | ||||||||||||||||||||||||||||
| 68 | 351 | $self->check_required_methods($role, $consumer, @extra); | |||||||||||||||||||||||||||
| 69 | #$self->check_required_attributes($role, $consumer, @extra); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 344 | $self->apply_attributes($role, $consumer, @extra); | |||||||||||||||||||||||||||
| 72 | 342 | $self->apply_methods($role, $consumer, @extra); | |||||||||||||||||||||||||||
| 73 | #$self->apply_override_method_modifiers($role, $consumer, @extra); | ||||||||||||||||||||||||||||
| 74 | #$self->apply_before_method_modifiers($role, $consumer, @extra); | ||||||||||||||||||||||||||||
| 75 | #$self->apply_around_method_modifiers($role, $consumer, @extra); | ||||||||||||||||||||||||||||
| 76 | #$self->apply_after_method_modifiers($role, $consumer, @extra); | ||||||||||||||||||||||||||||
| 77 | 334 | $self->apply_modifiers($role, $consumer, @extra); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 331 | $self->_append_roles($role, $consumer); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 331 | ▶ | if(defined $instance){ # Application::ToInstance | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | # rebless instance | ||||||||||||||||||||||||||||
| 83 | 23 | bless $instance, $consumer->name; | |||||||||||||||||||||||||||
| 84 | 23 | $consumer->_initialize_object($instance, $instance, 1); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 331 | return; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 351 | ▶ | sub check_required_methods { | ||||||||||||||||||||||||||
|
Subroutine
check_required_methods: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | 351 | my($self, $role, $consumer) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 351 | ▶ | if($self->{_to} eq 'role'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 159 | $consumer->add_required_methods($role->get_required_method_list); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else{ # to class or instance | ||||||||||||||||||||||||||||
| 97 | 192 | my $consumer_class_name = $consumer->name; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 192 | my @missing; | |||||||||||||||||||||||||||
| 100 | 192 | foreach my $method_name(@{$role->{required_methods}}){ | |||||||||||||||||||||||||||
| 101 | 60 | ▶ | next if exists $self->{aliased_methods}{$method_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 58 | ▶ | next if exists $role->{methods}{$method_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 38 | ▶ | next if $consumer_class_name->can($method_name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 14 | push @missing, $method_name; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 192 | ▶ | if(@missing){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 7 | ▶ | $role->throw_error(sprintf "'%s' requires the method%s %s to be implemented by '%s'", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | $role->name, | ||||||||||||||||||||||||||||
| 110 | (@missing == 1 ? '' : 's'), # method or methods | ||||||||||||||||||||||||||||
| 111 | Mouse::Util::quoted_english_list(@missing), | ||||||||||||||||||||||||||||
| 112 | $consumer_class_name); | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 344 | return; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 338 | ▶ | sub apply_methods { | ||||||||||||||||||||||||||
|
Subroutine
apply_methods: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | 338 | my($self, $role, $consumer) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 338 | my $alias = $self->{-alias}; | |||||||||||||||||||||||||||
| 123 | 338 | my $excludes = $self->{-excludes}; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 338 | foreach my $method_name($role->get_method_list){ | |||||||||||||||||||||||||||
| 126 | 691 | ▶ | next if $method_name eq 'meta'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 355 | my $code = $role->get_method_body($method_name); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 355 | ▶ | if(!exists $excludes->{$method_name}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 319 | ▶ | if(!$consumer->has_method($method_name)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | # The third argument $role is used in Role::Composite | ||||||||||||||||||||||||||||
| 133 | 307 | $consumer->add_method($method_name => $code, $role); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 355 | ▶ | if(exists $alias->{$method_name}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 34 | my $dstname = $alias->{$method_name}; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 34 | my $dstcode = $consumer->get_method_body($dstname); | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 34 | ▶ | if(defined($dstcode) && $dstcode != $code){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $dstcode and $dstcode != $code
MC/DC50%1/2
defined $dstcode and $dstcode != $code
defined $dstcode
$dstcode != $code
Truth table66%2/3
A and B
A= defined $dstcodeB= $dstcode != $code
| |||||||||||||||||||||||||||||
| 143 | 4 | $role->throw_error("Cannot create a method alias if a local method of the same name exists"); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | else{ | ||||||||||||||||||||||||||||
| 146 | 30 | $consumer->add_method($dstname => $code, $role); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 334 | return; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 344 | ▶ | sub apply_attributes { | ||||||||||||||||||||||||||
|
Subroutine
apply_attributes: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 155 | 344 | my($self, $role, $consumer) = @_; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 344 | for my $attr_name ($role->get_attribute_list) { | |||||||||||||||||||||||||||
| 158 | 112 | ▶ | next if $consumer->has_attribute($attr_name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 109 | $consumer->add_attribute($attr_name | |||||||||||||||||||||||||||
| 161 | => $role->get_attribute($attr_name)); | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 342 | return; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 334 | ▶ | sub apply_modifiers { | ||||||||||||||||||||||||||
|
Subroutine
apply_modifiers: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 167 | 334 | my($self, $role, $consumer) = @_; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 334 | ▶ | if(my $modifiers = $role->{override_method_modifiers}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 16 | foreach my $method_name (keys %{$modifiers}){ | |||||||||||||||||||||||||||
| 171 | $consumer->add_override_method_modifier( | ||||||||||||||||||||||||||||
| 172 | 18 | $method_name => $modifiers->{$method_name}); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 331 | for my $modifier_type (qw/before around after/) { | |||||||||||||||||||||||||||
| 177 | 993 | ▶ | my $table = $role->{"${modifier_type}_method_modifiers"} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | or next; | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 53 | my $add_modifier = "add_${modifier_type}_method_modifier"; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 53 | ▶ | while(my($method_name, $modifiers) = each %{$table}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 53 | foreach my $code(@{ $modifiers }) { | |||||||||||||||||||||||||||
| 184 | # skip if the modifier is already applied | ||||||||||||||||||||||||||||
| 185 | 55 | ▶ | next if $consumer->{"_applied_$modifier_type"}{$method_name, $code}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 54 | $consumer->$add_modifier($method_name => $code); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 331 | return; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 331 | ▶ | sub _append_roles { | ||||||||||||||||||||||||||
|
Subroutine
_append_roles: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 194 | 331 | my($self, $role, $metaclass_or_role) = @_; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 331 | my $roles = $metaclass_or_role->{roles}; | |||||||||||||||||||||||||||
| 197 | 331 | foreach my $r($role, @{$role->get_roles}){ | |||||||||||||||||||||||||||
| 198 | 462 | ▶ | if(!$metaclass_or_role->does_role($r)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 325 | push @{$roles}, $r; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | 331 | return; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | 82 | 1; | |||||||||||||||||||||||||||
| 205 | __END__ | ||||||||||||||||||||||||||||