blib/lib/Class/MOP/Method/Accessor.pm

filter: Statementstmt 100% 89 / 89 Branchbran 95.0% 19 / 20 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 36 / 36 Podpod 100% 1 / 1 totaltotal 96.8% 154 / 159 CC 44 SCAR 6.9
CC
44
Cov
97%
new
7.0
_new
4.0
_initialize_body
2.0
CRAP 2.0
« blib/lib/Moose/Meta/Method/Destructor.pm ↑ Summary blib/lib/Moose/Meta/TypeCoercion/Union.pm »
1 package Class::MOP::Method::Accessor;
2 459 our $VERSION = '2.4001';
3  
4 459 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 459 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 459 ▶use Scalar::Util 'blessed', 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 459 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 459 ▶use parent 'Class::MOP::Method::Generated';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 79294 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
13 79294 my $class = shift;
14 79294 my %options = @_;
15  
16 (exists $options{attribute})
17 79294 ▶ || $class->_throw_exception( MustSupplyAnAttributeToConstructWith => params => \%options,
Branch: 1 branch
BranchTrueFalseTotal
exists $options{"attribute"} or $class->_throw_exception("MustSupplyAnAttributeToConstructWith", "params", \%options, "class", $class) 79293 1 79294
18 class => $class,
19 );
20  
21 (exists $options{accessor_type})
22 79293 ▶ || $class->_throw_exception( MustSupplyAnAccessorTypeToConstructWith => params => \%options,
Branch: 1 branch
BranchTrueFalseTotal
exists $options{"accessor_type"} or $class->_throw_exception("MustSupplyAnAccessorTypeToConstructWith", "params", \%options, "class", $class) 79292 1 79293
23 class => $class,
24 );
25  
26 79292 ▶ (blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($options{"attribute"}) and $options{"attribute"}->isa("Class::MOP::Attribute") or $class->_throw_exception("MustSupplyAClassMOPAttributeInstance", "params", \%options, "class", $class) 79291 1 79292
Condition66%2/3
builtin::blessed($options{"attribute"}) and $options{"attribute"}->isa("Class::MOP::Attribute")
!ll&&!rl&&r
1079291
MC/DC50%1/2
builtin::blessed($options{"attribute"}) and $options{"attribute"}->isa("Class::MOP::Attribute")
builtin::blessed($options{"attribute"}) $options{"attribute"}->isa("Class::MOP::Attribute")
Truth table66%2/3
A and B
A= builtin::blessed($options{"attribute"})B= $options{"attribute"}->isa("Class::MOP::Attribute")
ABresult
0X0
100
111
27 || $class->_throw_exception( MustSupplyAClassMOPAttributeInstance => params => \%options,
28 class => $class
29 );
30  
31 ($options{package_name} && $options{name})
32 79291 ▶ || $class->_throw_exception( MustSupplyPackageNameAndName => params => \%options,
Branch: 1 branch
BranchTrueFalseTotal
$options{"package_name"} and $options{"name"} or $class->_throw_exception("MustSupplyPackageNameAndName", "params", \%options, "class", $class) 79290 1 79291
Condition66%2/3
$options{"package_name"} and $options{"name"}
!ll&&!rl&&r
1079290
MC/DC50%1/2
$options{"package_name"} and $options{"name"}
$options{"package_name"} $options{"name"}
Truth table66%2/3
A and B
A= $options{"package_name"}B= $options{"name"}
ABresult
0X0
100
111
33 class => $class
34 );
35  
36 79290 my $self = $class->_new(\%options);
37  
38 # we don't want this creating
39 # a cycle in the code, if not
40 # needed
41 79290 weaken($self->{'attribute'});
42  
43 79290 $self->_initialize_body;
44  
45 79289 return $self;
46 }
47  
48 110393 ▶sub _new {
Subroutine _new: called CC=4 SCAR=13.9
49 110393 my $class = shift;
50  
51 110393 ▶ return Class::MOP::Class->initialize($class)->new_object(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $class ne __PACKAGE__ 3241 107152 110393
52 if $class ne __PACKAGE__;
53  
54 107152 ▶ my $params = @_ == 1 ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 107152 0 107152
55  
56 return bless {
57 # inherited from Class::MOP::Method
58 body => $params->{body},
59 associated_metaclass => $params->{associated_metaclass},
60 package_name => $params->{package_name},
61 name => $params->{name},
62 original_method => $params->{original_method},
63  
64 # inherit from Class::MOP::Generated
65 is_inline => $params->{is_inline} || 0,
66 definition_context => $params->{definition_context},
67  
68 # defined in this class
69 attribute => $params->{attribute},
70 accessor_type => $params->{accessor_type},
71 107152 ▶ } => $class;
Condition100%2/2
$params->{'is_inline'} || 0
l!l
3705070102
MC/DC100%1/1
$params->{'is_inline'} || 0
$params->{'is_inline'}
Truth table100%2/2
A
A= $params->{'is_inline'}
Aresult
11
00
72 }
73  
74 ## accessors
75  
76 96404 ▶sub associated_attribute { (shift)->{'attribute'} }
Subroutine associated_attribute: called CC=1 SCAR=0.0
77 78260 ▶sub accessor_type { (shift)->{'accessor_type'} }
Subroutine accessor_type: called CC=1 SCAR=0.0
78  
79 ## factory
80  
81 78258 ▶sub _initialize_body {
Subroutine _initialize_body: called CC=2 SCAR=6.9
82 78258 my $self = shift;
83  
84 78258 ▶ my $method_name = join "_" => (
Branch: 1 branch
BranchTrueFalseTotal
$self->is_inline ? : 37436 40822 78258
85 '_generate',
86 $self->accessor_type,
87 'method',
88 ($self->is_inline ? 'inline' : ())
89 );
90  
91 78258 $self->{'body'} = $self->$method_name();
92 }
93  
94 ## generators
95  
96 8334 ▶sub _generate_accessor_method {
Subroutine _generate_accessor_method: called CC=1 SCAR=0.0
97 8334 my $self = shift;
98 8334 my $attr = $self->associated_attribute;
99  
100 33 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
101 33 ▶ if (@_ >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2) 4 29 33
102 4 $attr->set_value($_[0], $_[1]);
103 }
104 33 $attr->get_value($_[0]);
105 8334 };
106 }
107  
108 8569 ▶sub _generate_accessor_method_inline {
Subroutine _generate_accessor_method_inline: called CC=1 SCAR=0.0
109 8569 my $self = shift;
110 8569 my $attr = $self->associated_attribute;
111  
112 8569 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
113 8569 $self->_compile_code([
114 'sub {',
115 'if (@_ > 1) {',
116 $attr->_inline_set_value('$_[0]', '$_[1]'),
117 '}',
118 $attr->_inline_get_value('$_[0]'),
119 '}',
120 ]);
121 }
122 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
123 1 $self->_throw_exception( CouldNotGenerateInlineAttributeMethod => instance => $self,
124 error => $_,
125 option => "accessor"
126 );
127 8569 };
128 }
129  
130 21570 ▶sub _generate_reader_method {
Subroutine _generate_reader_method: called CC=1 SCAR=0.0
131 21570 my $self = shift;
132 21570 my $attr = $self->associated_attribute;
133 21570 my $class = $attr->associated_class;
134  
135 4576 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
136 4576 ▶ $self->_throw_exception( CannotAssignValueToReadOnlyAccessor => class_name => $class->name,
Branch: 2 branches
BranchTrueFalseTotal
if @_ > 1 2 4574 4576
if @_ > 1 18 436 454
137 value => $_[1],
138 attribute => $attr
139 )
140 if @_ > 1;
141 4574 $attr->get_value($_[0]);
142 21570 };
143 }
144  
145 23975 ▶sub _generate_reader_method_inline {
Subroutine _generate_reader_method_inline: called CC=1 SCAR=0.0
146 23975 my $self = shift;
147 23975 my $attr = $self->associated_attribute;
148 23975 my $attr_name = $attr->name;
149  
150 23975 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
151 23975 $self->_compile_code([
152 'sub {',
153 'if (@_ > 1) {',
154 $self->_inline_throw_exception( CannotAssignValueToReadOnlyAccessor =>
155 'class_name => ref $_[0],'.
156 'value => $_[1],'.
157 "attribute_name => '".$attr_name."'",
158 ) . ';',
159 '}',
160 $attr->_inline_get_value('$_[0]'),
161 '}',
162 ]);
163 }
164 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
165 1 $self->_throw_exception( CouldNotGenerateInlineAttributeMethod => instance => $self,
166 error => $_,
167 option => "reader"
168 );
169 23975 };
170 }
171  
172 1208 ▶sub _generate_writer_method {
Subroutine _generate_writer_method: called CC=1 SCAR=0.0
173 1208 my $self = shift;
174 1208 my $attr = $self->associated_attribute;
175  
176 60 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
177 60 $attr->set_value($_[0], $_[1]);
178 1208 };
179 }
180  
181 1198 ▶sub _generate_writer_method_inline {
Subroutine _generate_writer_method_inline: called CC=1 SCAR=0.0
182 1198 my $self = shift;
183 1198 my $attr = $self->associated_attribute;
184  
185 1198 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
186 1198 $self->_compile_code([
187 'sub {',
188 $attr->_inline_set_value('$_[0]', '$_[1]'),
189 '}',
190 ]);
191 }
192 2 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
193 2 $self->_throw_exception( CouldNotGenerateInlineAttributeMethod => instance => $self,
194 error => $_,
195 option => "writer"
196 );
197 1198 };
198 }
199  
200 6385 ▶sub _generate_predicate_method {
Subroutine _generate_predicate_method: called CC=1 SCAR=0.0
201 6385 my $self = shift;
202 6385 my $attr = $self->associated_attribute;
203  
204 4927 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
205 4927 $attr->has_value($_[0])
206 6385 };
207 }
208  
209 6456 ▶sub _generate_predicate_method_inline {
Subroutine _generate_predicate_method_inline: called CC=1 SCAR=0.0
210 6456 my $self = shift;
211 6456 my $attr = $self->associated_attribute;
212  
213 6456 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
214 6456 $self->_compile_code([
215 'sub {',
216 $attr->_inline_has_value('$_[0]'),
217 '}',
218 ]);
219 }
220 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
221 1 $self->_throw_exception( CouldNotGenerateInlineAttributeMethod => instance => $self,
222 error => $_,
223 option => "predicate"
224 );
225 6456 };
226 }
227  
228 472 ▶sub _generate_clearer_method {
Subroutine _generate_clearer_method: called CC=1 SCAR=0.0
229 472 my $self = shift;
230 472 my $attr = $self->associated_attribute;
231  
232 15 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _clear_overload_map: called CC=1 SCAR=0.0
233 15 $attr->clear_value($_[0])
234 472 };
235 }
236  
237 80 ▶sub _generate_clearer_method_inline {
Subroutine _generate_clearer_method_inline: called CC=1 SCAR=0.0
238 80 my $self = shift;
239 80 my $attr = $self->associated_attribute;
240  
241 80 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
242 80 $self->_compile_code([
243 'sub {',
244 $attr->_inline_clear_value('$_[0]'),
245 '}',
246 ]);
247 }
248 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
249 1 $self->_throw_exception( CouldNotGenerateInlineAttributeMethod => instance => $self,
250 error => $_,
251 option => "clearer"
252 );
253 80 };
254 }
255  
256 459 1;
257  
258 # ABSTRACT: Method Meta Object for accessors
259  
260 __END__
« blib/lib/Moose/Meta/Method/Destructor.pm ↑ Summary blib/lib/Moose/Meta/TypeCoercion/Union.pm »