blib/lib/Mouse/Meta/Attribute.pm

filter: Statementstmt 94.3% 134 / 142 Branchbran 83.8% 57 / 68 Conditioncond 71.4% 15 / 21 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 88.0% 22 / 25 Podpod 35.2% 6 / 17 totaltotal 82.9% 238 / 287 CC 46 SCAR 16.3
CC
46
Cov
89%
clone_and_inherit_options
10.0
interpolate_class
7.0
install_accessors
7.0
CRAP 5.1
« tool/generate-mouse-tiny.pl ↑ Summary blib/lib/Mouse/Meta/Role/Composite.pm »
1 package Mouse::Meta::Attribute;
2 285 ▶use Mouse::Util qw(:meta); # enables strict and warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 285 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 285 ▶use Mouse::Meta::TypeConstraint;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 285 my %valid_options = map { $_ => undef } (
9 'accessor',
10 'auto_deref',
11 'builder',
12 'clearer',
13 'coerce',
14 'default',
15 'documentation',
16 'does',
17 'handles',
18 'init_arg',
19 'insertion_order',
20 'is',
21 'isa',
22 'lazy',
23 'lazy_build',
24 'name',
25 'predicate',
26 'reader',
27 'required',
28 'traits',
29 'trigger',
30 'type_constraint',
31 'weak_ref',
32 'writer',
33  
34 # internally used
35 'associated_class',
36 'associated_methods',
37 '__METACLASS__',
38  
39 # Moose defines, but Mouse doesn't
40 #'definition_context',
41 #'initializer',
42  
43 # special case for AttributeHelpers
44 'provides',
45 'curries',
46 );
47  
48 285 our @CARP_NOT = qw(Mouse::Meta::Class);
49  
50 602 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
51 602 my $class = shift;
52 602 my $name = shift;
53  
54 602 my $args = $class->Mouse::Object::BUILDARGS(@_);
55  
56 602 $class->_process_options($name, $args);
57  
58 590 $args->{name} = $name;
59  
60 # check options
61 # (1) known by core
62 590 my @bad = grep{ !exists $valid_options{$_} } keys %{$args};
63  
64 # (2) known by subclasses
65 590 ▶ if(@bad && $class ne __PACKAGE__){
Branch: 1 branch
BranchTrueFalseTotal
if (@bad and $class ne __PACKAGE__) 8 582 590
Condition100%3/3
@bad and $class ne __PACKAGE__
!ll&&!rl&&r
58118
MC/DC100%2/2
@bad and $class ne __PACKAGE__
@bad $class ne __PACKAGE__
Truth table100%3/3
A and B
A= @badB= $class ne __PACKAGE__
ABresult
0X0
100
111
66 my %valid_attrs = (
67 12 map { $_ => undef }
68 12 grep { defined }
69 8 map { $_->init_arg() }
70 $class->meta->get_all_attributes()
71 );
72 8 @bad = grep{ !exists $valid_attrs{$_} } @bad;
73 }
74  
75 # (3) bad options found
76 590 ▶ if(@bad){
Branch: 1 branch
BranchTrueFalseTotal
if (@bad) 1 589 590
77 1 Carp::carp(
78 "Found unknown argument(s) passed to '$name' attribute constructor in '$class': "
79 . Mouse::Util::english_list(@bad));
80 }
81  
82 590 my $self = bless $args, $class;
83 590 ▶ if($class ne __PACKAGE__){
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne __PACKAGE__) 25 565 590
84 25 $class->meta->_initialize_object($self, $args);
85 }
86 590 return $self;
87 }
88  
89 0 ▶sub has_read_method { $_[0]->has_reader || $_[0]->has_accessor }
Subroutine has_read_method: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_reader or $_[0]->has_accessor 0 0 0
90 0 ▶sub has_write_method { $_[0]->has_writer || $_[0]->has_accessor }
Subroutine has_write_method: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_writer or $_[0]->has_accessor 0 0 0
91  
92 35 ▶sub get_read_method { $_[0]->reader || $_[0]->accessor }
Subroutine get_read_method: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->reader or $_[0]->accessor 20 15 35
93 4 ▶sub get_write_method { $_[0]->writer || $_[0]->accessor }
Subroutine get_write_method: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->writer or $_[0]->accessor 0 4 4
94  
95 12 ▶sub get_read_method_ref{
Subroutine get_read_method_ref: called CC=2 SCAR=7.1
Pod documented
96 12 my($self) = @_;
97 return $self->{_mouse_cache_read_method_ref}
98 12 ▶ ||= $self->_get_accessor_method_ref('get_read_method', '_generate_reader');
Condition66%2/3
$self->{"_mouse_cache_read_method_ref"} ||= $self->_get_accessor_method_ref("get_read_method", "_generate_reader")
l!l&&r!l&&!r
1110
MC/DC0%0/2
$self->{"_mouse_cache_read_method_ref"} ||= $self->_get_accessor_method_ref("get_read_method", "_generate_reader")
$self->{"_mouse_cache_read_method_ref"} $self->_get_accessor_method_ref("get_read_method", "_generate_reader")
Truth table66%2/3
A ||= B
A= $self->{"_mouse_cache_read_method_ref"}B= $self->_get_accessor_method_ref("get_read_method", "_generate_reader")
ABresult
1X1
011
000
99 }
100  
101 6 ▶sub get_write_method_ref{
Subroutine get_write_method_ref: called CC=2 SCAR=7.1
Pod documented
102 6 my($self) = @_;
103 return $self->{_mouse_cache_write_method_ref}
104 6 ▶ ||= $self->_get_accessor_method_ref('get_write_method', '_generate_writer');
Condition66%2/3
$self->{"_mouse_cache_write_method_ref"} ||= $self->_get_accessor_method_ref("get_write_method", "_generate_writer")
l!l&&r!l&&!r
240
MC/DC0%0/2
$self->{"_mouse_cache_write_method_ref"} ||= $self->_get_accessor_method_ref("get_write_method", "_generate_writer")
$self->{"_mouse_cache_write_method_ref"} $self->_get_accessor_method_ref("get_write_method", "_generate_writer")
Truth table66%2/3
A ||= B
A= $self->{"_mouse_cache_write_method_ref"}B= $self->_get_accessor_method_ref("get_write_method", "_generate_writer")
ABresult
1X1
011
000
105 }
106  
107 595 ▶sub interpolate_class{
Subroutine interpolate_class: called CC=7 SCAR=19.5
Pod undocumented
108 595 my($class, $args) = @_;
109  
110 595 ▶ if(my $metaclass = delete $args->{metaclass}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $metaclass = delete $args->{"metaclass"}) 12 583 595
111 12 $class = Mouse::Util::resolve_metaclass_alias( Attribute => $metaclass );
112 }
113  
114 595 my @traits;
115 595 ▶ if(my $traits_ref = delete $args->{traits}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $traits_ref = delete $args->{"traits"}) 11 584 595
116  
117 11 ▶ for (my $i = 0; $i < @{$traits_ref}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$traits_ref;}) 11 11 22
118 11 my $trait = Mouse::Util::resolve_metaclass_alias(Attribute => $traits_ref->[$i], trait => 1);
119  
120 11 ▶ next if $class->does($trait);
Branch: 1 branch
BranchTrueFalseTotal
if $class->does($trait) 1 10 11
121  
122 10 push @traits, $trait;
123  
124 # are there options?
125 10 ▶ push @traits, $traits_ref->[++$i]
Branch: 1 branch
BranchTrueFalseTotal
if ref $$traits_ref[$i + 1] 2 8 10
126 if ref($traits_ref->[$i+1]);
127 }
128  
129 11 ▶ if (@traits) {
Branch: 1 branch
BranchTrueFalseTotal
if (@traits) 10 1 11
130 10 $class = Mouse::Meta::Class->create_anon_class(
131 superclasses => [ $class ],
132 roles => \@traits,
133 cache => 1,
134 )->name;
135 }
136 }
137  
138 595 return( $class, @traits );
139 }
140  
141 1 ▶sub verify_against_type_constraint {
Subroutine verify_against_type_constraint: called CC=3 SCAR=15.1
Pod documented
142 1 my ($self, $value) = @_;
143  
144 1 my $type_constraint = $self->{type_constraint};
145 1 ▶ return 1 if !$type_constraint;
Branch: 1 branch
BranchTrueFalseTotal
unless $type_constraint 1 0 1
146 0 ▶ return 1 if $type_constraint->check($value);
Branch: 1 branch
BranchTrueFalseTotal
if $type_constraint->check($value) 0 0 0
147  
148 0 $self->_throw_type_constraint_error($value, $type_constraint);
149 }
150  
151 0 ▶sub _throw_type_constraint_error {
Subroutine _throw_type_constraint_error: not called CC=1 SCAR=6.9
152 0 my($self, $value, $type) = @_;
153  
154 0 $self->throw_error(
155 sprintf q{Attribute (%s) does not pass the type constraint because: %s},
156 $self->name,
157 $type->get_message($value),
158 );
159 }
160  
161 34 ▶sub illegal_options_for_inheritance {
Subroutine illegal_options_for_inheritance: called CC=1 SCAR=0.0
Pod undocumented
162 34 return qw(reader writer accessor clearer predicate);
163 }
164  
165 33 ▶sub clone_and_inherit_options{
Subroutine clone_and_inherit_options: called CC=10 SCAR=23.0
Pod documented
166 33 my $self = shift;
167 33 my $args = $self->Mouse::Object::BUILDARGS(@_);
168  
169 33 foreach my $illegal($self->illegal_options_for_inheritance) {
170 163 ▶ if(exists $args->{$illegal} and exists $self->{$illegal}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{$illegal} and exists $self->{$illegal}) 4 159 163
Condition100%3/3
exists $args->{$illegal} and exists $self->{$illegal}
!ll&&!rl&&r
15634
MC/DC100%2/2
exists $args->{$illegal} and exists $self->{$illegal}
exists $args->{$illegal} exists $self->{$illegal}
Truth table100%3/3
A and B
A= exists $args->{$illegal}B= exists $self->{$illegal}
ABresult
0X0
100
111
171 4 $self->throw_error("Illegal inherited option: $illegal");
172 }
173 }
174  
175 29 foreach my $name(keys %{$self}){
176 243 ▶ if(!exists $args->{$name}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{$name}) 226 17 243
177 226 $args->{$name} = $self->{$name}; # inherit from self
178 }
179 }
180  
181 29 my($attribute_class, @traits) = ref($self)->interpolate_class($args);
182 29 ▶ $args->{traits} = \@traits if @traits;
Branch: 1 branch
BranchTrueFalseTotal
if @traits 1 28 29
183  
184 # remove temporary caches
185 29 foreach my $attr(keys %{$args}){
186 254 ▶ if($attr =~ /\A _mouse_cache_/xms){
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /\A _mouse_cache_/msx) 2 252 254
187 2 delete $args->{$attr};
188 }
189 }
190  
191 # remove default if lazy_build => 1
192 29 ▶ if($args->{lazy_build}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"lazy_build"}) 1 28 29
193 1 delete $args->{default};
194 }
195  
196 29 return $attribute_class->new($self->name, $args);
197 }
198  
199  
200 17 ▶sub _get_accessor_method_ref {
Subroutine _get_accessor_method_ref: called CC=3 SCAR=11.2
201 17 my($self, $type, $generator) = @_;
202  
203 17 ▶ my $metaclass = $self->associated_class
Condition33%1/3
$self->associated_class || $self->throw_error('No asocciated class for ' . $self->name)
l!l&&r!l&&!r
1700
MC/DC0%0/2
$self->associated_class || $self->throw_error('No asocciated class for ' . $self->name)
$self->associated_class $self->throw_error('No asocciated class for ' . $self->name)
Truth table33%1/3
A || B
A= $self->associated_classB= $self->throw_error('No asocciated class for ' . $self->name)
ABresult
1X1
011
000
204 || $self->throw_error('No asocciated class for ' . $self->name);
205  
206 17 my $accessor = $self->$type();
207 17 ▶ if($accessor){
Branch: 1 branch
BranchTrueFalseTotal
if ($accessor) { } 10 7 17
208 10 return $metaclass->get_method_body($accessor);
209 }
210 else{
211 7 return $self->accessor_metaclass->$generator($self, $metaclass);
212 }
213 }
214  
215 4 ▶sub set_value {
Subroutine set_value: called CC=1 SCAR=0.0
Pod undocumented
216 4 my($self, $object, $value) = @_;
217 4 return $self->get_write_method_ref()->($object, $value);
218 }
219  
220 3 ▶sub get_value {
Subroutine get_value: called CC=1 SCAR=0.0
Pod undocumented
221 3 my($self, $object) = @_;
222 3 return $self->get_read_method_ref()->($object);
223 }
224  
225 6 ▶sub has_value {
Subroutine has_value: called CC=2 SCAR=7.0
Pod undocumented
226 6 my($self, $object) = @_;
227 my $accessor_ref = $self->{_mouse_cache_predicate_ref}
228 6 ▶ ||= $self->_get_accessor_method_ref('predicate', '_generate_predicate');
Condition66%2/3
$self->{"_mouse_cache_predicate_ref"} ||= $self->_get_accessor_method_ref("predicate", "_generate_predicate")
l!l&&r!l&&!r
510
MC/DC0%0/2
$self->{"_mouse_cache_predicate_ref"} ||= $self->_get_accessor_method_ref("predicate", "_generate_predicate")
$self->{"_mouse_cache_predicate_ref"} $self->_get_accessor_method_ref("predicate", "_generate_predicate")
Truth table66%2/3
A ||= B
A= $self->{"_mouse_cache_predicate_ref"}B= $self->_get_accessor_method_ref("predicate", "_generate_predicate")
ABresult
1X1
011
000
229  
230 6 return $accessor_ref->($object);
231 }
232  
233 2 ▶sub clear_value {
Subroutine clear_value: called CC=2 SCAR=7.0
Pod undocumented
234 2 my($self, $object) = @_;
235 my $accessor_ref = $self->{_mouse_cache_crealer_ref}
236 2 ▶ ||= $self->_get_accessor_method_ref('clearer', '_generate_clearer');
Condition66%2/3
$self->{"_mouse_cache_crealer_ref"} ||= $self->_get_accessor_method_ref("clearer", "_generate_clearer")
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{"_mouse_cache_crealer_ref"} ||= $self->_get_accessor_method_ref("clearer", "_generate_clearer")
$self->{"_mouse_cache_crealer_ref"} $self->_get_accessor_method_ref("clearer", "_generate_clearer")
Truth table66%2/3
A ||= B
A= $self->{"_mouse_cache_crealer_ref"}B= $self->_get_accessor_method_ref("clearer", "_generate_clearer")
ABresult
1X1
011
000
237  
238 2 return $accessor_ref->($object);
239 }
240  
241 632 ▶sub associate_method{
Subroutine associate_method: called CC=1 SCAR=0.0
Pod documented
242 #my($attribute, $method_name) = @_;
243 632 my($attribute) = @_;
244 632 $attribute->{associated_methods}++;
245 632 return;
246 }
247  
248 587 ▶sub install_accessors{
Subroutine install_accessors: called CC=7 SCAR=19.5
Pod undocumented
249 587 my($attribute) = @_;
250  
251 587 my $metaclass = $attribute->associated_class;
252 587 my $accessor_class = $attribute->accessor_metaclass;
253  
254 587 foreach my $type(qw(accessor reader writer predicate clearer)){
255 2935 ▶ if(exists $attribute->{$type}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attribute->{$type}) 610 2325 2935
256 610 my $generator = '_generate_' . $type;
257 610 my $code = $accessor_class->$generator($attribute, $metaclass);
258 610 my $name = $attribute->{$type};
259 # TODO: do something for compatibility
260 # if( $metaclass->name->can($name) ) {
261 # my $t = $metaclass->has_method($name) ? 'method' : 'function';
262 # Carp::cluck("You are overwriting a locally defined $t"
263 # . " ($name) with an accessor");
264 # }
265 610 $metaclass->add_method($name => $code);
266 610 $attribute->associate_method($name);
267 }
268 }
269  
270 # install delegation
271 587 ▶ if(exists $attribute->{handles}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attribute->{"handles"}) 19 568 587
272 19 my %handles = $attribute->_canonicalize_handles();
273 17 ▶ while(my($handle, $method_to_call) = each %handles){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %handles) 42 15 57
274 42 ▶ next if Mouse::Object->can($handle);
Branch: 1 branch
BranchTrueFalseTotal
if "Mouse::Object"->can($handle) 18 24 42
275  
276 24 ▶ if($metaclass->has_method($handle)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metaclass->has_method($handle)) 2 22 24
277 2 $attribute->throw_error("You cannot overwrite a locally defined method ($handle) with a delegation");
278 }
279  
280 22 $metaclass->add_method($handle =>
281 $attribute->_make_delegation_method(
282 $handle, $method_to_call));
283  
284 22 $attribute->associate_method($handle);
285 }
286 }
287  
288 583 return;
289 }
290  
291 sub delegation_metaclass() { ## no critic
292 'Mouse::Meta::Method::Delegation'
293 }
294  
295 19 ▶sub _canonicalize_handles {
Subroutine _canonicalize_handles: called CC=6 SCAR=18.0
296 19 my($self) = @_;
297 19 my $handles = $self->{handles};
298  
299 19 my $handle_type = ref $handles;
300 19 ▶ if ($handle_type eq 'HASH') {
Branch: 4 branches
BranchTrueFalseTotal
if ($handle_type eq 'HASH') { } 2 17 19
elsif ($handle_type eq 'ARRAY') { } 8 9 17
elsif ($handle_type eq 'Regexp') { } 4 5 9
elsif ($handle_type eq 'CODE') { } 5 0 5
301 2 return %$handles;
302 }
303 elsif ($handle_type eq 'ARRAY') {
304 8 return map { $_ => $_ } @$handles;
305 }
306 elsif ($handle_type eq 'Regexp') {
307 4 my $meta = $self->_find_delegate_metaclass();
308 13 return map { $_ => $_ }
309 3 ▶ grep { /$handles/ }
Branch: 1 branch
BranchTrueFalseTotal
Mouse::Util::is_a_metarole($meta) ? : 0 3 3
310 Mouse::Util::is_a_metarole($meta)
311 ? $meta->get_method_list
312 : $meta->get_all_method_names;
313 }
314 elsif ($handle_type eq 'CODE') {
315 5 return $handles->( $self, $self->_find_delegate_metaclass() );
316 }
317 else {
318 0 $self->throw_error("Unable to canonicalize the 'handles' option with $handles");
319 }
320 }
321  
322 9 ▶sub _find_delegate_metaclass {
Subroutine _find_delegate_metaclass: called CC=4 SCAR=14.0
323 9 my($self) = @_;
324 9 my $meta;
325 9 ▶ if($self->{isa}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'isa'}) { } 7 2 9
elsif ($self->{'does'}) { } 0 2 2
326 7 $meta = Mouse::Meta::Class->initialize("$self->{isa}");
327 }
328 elsif($self->{does}) {
329 0 $meta = Mouse::Util::get_metaclass_by_name("$self->{does}");
330 }
331 9 ▶ defined($meta) or $self->throw_error(
Branch: 1 branch
BranchTrueFalseTotal
defined $meta or $self->throw_error('Cannot find delegate metaclass for attribute ' . $self->name) 7 2 9
332 "Cannot find delegate metaclass for attribute " . $self->name);
333 7 return $meta;
334 }
335  
336  
337 22 ▶sub _make_delegation_method {
Subroutine _make_delegation_method: called CC=1 SCAR=0.0
338 22 my($self, $handle, $method_to_call) = @_;
339 22 return Mouse::Util::load_class($self->delegation_metaclass)
340 ->_generate_delegation($self, $handle, $method_to_call);
341 }
342  
343 285 1;
344 __END__
« tool/generate-mouse-tiny.pl ↑ Summary blib/lib/Mouse/Meta/Role/Composite.pm »