blib/lib/Mouse/Meta/Role/Composite.pm

filter: Statementstmt 98.9% 97 / 98 Branchbran 89.2% 25 / 28 Conditioncond 92.3% 12 / 13 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 10 totaltotal 90.1% 155 / 172 CC 21 SCAR 15.3
CC
21
Cov
96%
apply_methods
9.0
add_method
6.0
add_attribute
4.0
CRAP 4.6
« blib/lib/Mouse/Meta/Attribute.pm ↑ Summary blib/lib/Mouse/Util.pm »
1 package Mouse::Meta::Role::Composite;
2 24 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
3 24 ▶use Mouse::Util; # enables strict and warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use Mouse::Meta::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use Mouse::Meta::Role::Application;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 our @ISA = qw(Mouse::Meta::Role);
7  
8 # FIXME: Mouse::Meta::Role::Composite does things in different way from Moose's
9 # Moose: creates a new class for the consumer, and applies roles to it.
10 # Mouse: creates a composite role and apply roles to the role,
11 # and then applies it to the consumer.
12  
13 54 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
14 54 my $class = shift;
15 54 my $args = $class->Mouse::Object::BUILDARGS(@_);
16 54 my $roles = delete $args->{roles};
17 54 my $self = $class->create_anon_role(%{$args});
18 54 foreach my $role_spec(@{$roles}) {
19 my($role, $args) = ref($role_spec) eq 'ARRAY'
20 120 ▶ ? @{$role_spec}
Branch: 1 branch
BranchTrueFalseTotal
ref $role_spec eq 'ARRAY' ? : 115 5 120
21 : ($role_spec, {});
22 120 $role->apply($self, %{$args});
23 }
24 49 return $self;
25 }
26  
27 45 ▶sub get_method_list {
Subroutine get_method_list: called CC=1 SCAR=0.0
Pod undocumented
28 45 my($self) = @_;
29 128 return grep { ! $self->{conflicting_methods}{$_} }
30 45 keys %{ $self->{methods} };
31 }
32  
33 163 ▶sub add_method {
Subroutine add_method: called CC=6 SCAR=17.9
Pod undocumented
34 163 my($self, $method_name, $code, $role) = @_;
35  
36 163 ▶ if( ($self->{methods}{$method_name} || 0) == $code){
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"methods"}{$method_name} || 0) == $code) 12 151 163
Condition100%2/2
$self->{'methods'}{$method_name} || 0
l!l
19144
MC/DC100%1/1
$self->{'methods'}{$method_name} || 0
$self->{'methods'}{$method_name}
Truth table100%2/2
A
A= $self->{'methods'}{$method_name}
Aresult
11
00
37 # This role already has the same method.
38 12 return;
39 }
40  
41 151 ▶ if($method_name eq 'meta'){
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name eq 'meta') { } 54 97 151
42 54 $self->SUPER::add_method($method_name => $code);
43 }
44 else{
45 # no need to add a subroutine to the stash
46 97 ▶ my $roles = $self->{composed_roles_by_method}{$method_name} ||= [];
Condition100%2/2
$self->{"composed_roles_by_method"}{$method_name} ||= []
l!l
790
MC/DC100%1/1
$self->{"composed_roles_by_method"}{$method_name} ||= []
$self->{"composed_roles_by_method"}{$method_name}
Truth table100%2/2
A
A= $self->{"composed_roles_by_method"}{$method_name}
Aresult
11
00
47 97 push @{$roles}, $role;
48 97 ▶ if(@{$roles} > 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$roles;} > 1) 7 90 97
49 7 $self->{conflicting_methods}{$method_name}++;
50 }
51 97 $self->{methods}{$method_name} = $code;
52 }
53 151 return;
54 }
55  
56 92 ▶sub get_method_body {
Subroutine get_method_body: called CC=1 SCAR=0.0
Pod undocumented
57 92 my($self, $method_name) = @_;
58 92 return $self->{methods}{$method_name};
59 }
60  
61 126 ▶sub has_method {
Subroutine has_method: called CC=1 SCAR=0.0
Pod undocumented
62 # my($self, $method_name) = @_;
63 126 return 0; # to fool apply_methods() in combine()
64 }
65  
66 16 ▶sub has_attribute {
Subroutine has_attribute: called CC=1 SCAR=0.0
Pod undocumented
67 # my($self, $method_name) = @_;
68 16 return 0; # to fool appply_attributes() in combine()
69 }
70  
71 0 ▶sub has_override_method_modifier {
Subroutine has_override_method_modifier: not called CC=1 SCAR=6.9
Pod undocumented
72 # my($self, $method_name) = @_;
73 0 return 0; # to fool apply_modifiers() in combine()
74 }
75  
76 16 ▶sub add_attribute {
Subroutine add_attribute: called CC=4 SCAR=13.9
Pod undocumented
77 16 my $self = shift;
78 16 my $attr_name = shift;
79 16 ▶ my $spec = (@_ == 1 ? $_[0] : {@_});
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 16 0 16
80  
81 16 my $existing = $self->{attributes}{$attr_name};
82 16 ▶ if($existing && $existing != $spec){
Branch: 1 branch
BranchTrueFalseTotal
if ($existing and $existing != $spec) 2 14 16
Condition100%3/3
$existing and $existing != $spec
!ll&&!rl&&r
1312
MC/DC100%2/2
$existing and $existing != $spec
$existing $existing != $spec
Truth table100%3/3
A and B
A= $existingB= $existing != $spec
ABresult
0X0
100
111
83 2 $self->throw_error("We have encountered an attribute conflict with '$attr_name' "
84 . "during composition. This is fatal error and cannot be disambiguated.");
85 }
86 14 $self->SUPER::add_attribute($attr_name, $spec);
87 14 return;
88 }
89  
90 5 ▶sub add_override_method_modifier {
Subroutine add_override_method_modifier: called CC=3 SCAR=11.0
Pod undocumented
91 5 my($self, $method_name, $code) = @_;
92  
93 5 my $existing = $self->{override_method_modifiers}{$method_name};
94 5 ▶ if($existing && $existing != $code){
Branch: 1 branch
BranchTrueFalseTotal
if ($existing and $existing != $code) 1 4 5
Condition100%3/3
$existing and $existing != $code
!ll&&!rl&&r
311
MC/DC100%2/2
$existing and $existing != $code
$existing $existing != $code
Truth table100%3/3
A and B
A= $existingB= $existing != $code
ABresult
0X0
100
111
95 1 $self->throw_error( "We have encountered an 'override' method conflict with '$method_name' during "
96 . "composition (Two 'override' methods of the same name encountered). "
97 . "This is fatal error.")
98 }
99 4 $self->SUPER::add_override_method_modifier($method_name, $code);
100 4 return;
101 }
102  
103 49 ▶sub apply {
Subroutine apply: called CC=1 SCAR=0.0
Pod undocumented
104 49 my $self = shift;
105 49 my $consumer = shift;
106  
107 49 Mouse::Meta::Role::Application::RoleSummation->new(@_)->apply($self, $consumer);
108 45 return;
109 }
110  
111 package Mouse::Meta::Role::Application::RoleSummation;
112 24 our @ISA = qw(Mouse::Meta::Role::Application);
113  
114 49 ▶sub apply_methods {
Subroutine apply_methods: called CC=9 SCAR=22.0
115 49 my($self, $role, $consumer, @extra) = @_;
116  
117 49 ▶ if(exists $role->{conflicting_methods}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $role->{"conflicting_methods"}) 7 42 49
118 7 my $consumer_class_name = $consumer->name;
119  
120 7 my @conflicting = grep{ !$consumer_class_name->can($_) }
121 7 keys %{ $role->{conflicting_methods} };
122  
123 7 ▶ if(@conflicting) {
Branch: 1 branch
BranchTrueFalseTotal
if (@conflicting) 4 3 7
124 4 ▶ my $method_name_conflict = (@conflicting == 1
Branch: 1 branch
BranchTrueFalseTotal
@conflicting == 1 ? : 3 1 4
125 ? 'a method name conflict'
126 : 'method name conflicts');
127  
128 4 my %seen;
129 my $roles = Mouse::Util::quoted_english_list(
130 10 grep{ !$seen{$_}++ } # uniq
131 10 map { $_->name }
132 5 map { @{$_} }
133 4 @{ $role->{composed_roles_by_method} }{@conflicting}
134 );
135  
136 4 ▶ $self->throw_error(sprintf
Branch: 1 branch
BranchTrueFalseTotal
@conflicting > 1 ? : 1 3 4
137 q{Due to %s in roles %s,}
138 . q{ the method%s %s must be implemented or excluded by '%s'},
139 $method_name_conflict,
140 $roles,
141 (@conflicting > 1 ? 's' : ''),
142 Mouse::Util::quoted_english_list(@conflicting),
143 $consumer_class_name);
144 }
145  
146 my @changed_in_v2_0_0 = grep {
147 2 ▶ $consumer_class_name->can($_) && ! $consumer->has_method($_)
Condition66%2/3
$consumer_class_name->can($_) && !$consumer->has_method($_)
!ll&&!rl&&r
011
MC/DC50%1/2
$consumer_class_name->can($_) && !$consumer->has_method($_)
$consumer_class_name->can($_) !$consumer->has_method($_)
Truth table66%2/3
A && not(B)
A= $consumer_class_name->can($_)B= !$consumer->has_method($_)
ABresult
0X0
100
111
148 3 } keys %{ $role->{conflicting_methods} };
149 3 ▶ if (@changed_in_v2_0_0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@changed_in_v2_0_0) 1 2 3
150 1 ▶ my $method_name_conflict = (@changed_in_v2_0_0 == 1
Branch: 1 branch
BranchTrueFalseTotal
@changed_in_v2_0_0 == 1 ? : 1 0 1
151 ? 'a method name conflict'
152 : 'method name conflicts');
153  
154 1 my %seen;
155 my $roles = Mouse::Util::quoted_english_list(
156 2 grep{ !$seen{$_}++ } # uniq
157 2 map { $_->name }
158 1 map { @{$_} }
159 1 @{ $role->{composed_roles_by_method} }{@changed_in_v2_0_0}
160 );
161  
162 1 ▶ Carp::cluck(sprintf
Branch: 1 branch
BranchTrueFalseTotal
@changed_in_v2_0_0 > 1 ? : 0 1 1
163 q{Due to %s in roles %s,}
164 . q{ the behavior of method%s %s might be incompatible with Moose}
165 . q{, check out %s},
166 $method_name_conflict,
167 $roles,
168 (@changed_in_v2_0_0 > 1 ? 's' : ''),
169 Mouse::Util::quoted_english_list(@changed_in_v2_0_0),
170 $consumer_class_name);
171 }
172 }
173  
174 45 $self->SUPER::apply_methods($role, $consumer, @extra);
175 45 return;
176 }
177  
178 package Mouse::Meta::Role::Composite;
179 24 1;
180 __END__
« blib/lib/Mouse/Meta/Attribute.pm ↑ Summary blib/lib/Mouse/Util.pm »