AttributeShortcuts/Trait/Attribute.pm

filter: Statementstmt 95.9% 190 / 198 Branchbran 78.7% 63 / 80 Conditioncond 80.0% 12 / 15 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 97.5% 39 / 40 Podpod 100% 1 / 1 totaltotal 90.7% 312 / 344 CC 47 SCAR 13.8
CC
47
Cov
90%
_mxas_coerce
10.5
_mxas_init_arg
7.0
_mxas_lazy_build_private
6.0
CRAP 4.0
« AttributeShortcuts.pm ↑ Summary AttributeShortcuts/Trait/Role/Attribute.pm »
1 #
2 # This file is part of MooseX-AttributeShortcuts
3 #
4 # This software is Copyright (c) 2017, 2015, 2014, 2013, 2012, 2011 by Chris Weyl.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package MooseX::AttributeShortcuts::Trait::Attribute;
11 9 our $AUTHORITY = 'cpan:RSRCHBOY';
12 9 $MooseX::AttributeShortcuts::Trait::Attribute::VERSION = '0.037';
13 # ABSTRACT: Shortcuts attribute trait proper
14  
15 30 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 30 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 30 ▶use Moose::Util::TypeConstraints ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 30 ▶use MooseX::Types::Moose ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 30 ▶use MooseX::Types::Common::String ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 30 ▶use MooseX::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 30 ▶use aliased 'MooseX::Meta::TypeConstraint::Mooish' => 'MooishTC';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 30 ▶use aliased 'MooseX::AttributeShortcuts::Trait::Method::Builder' => 'BuilderTrait';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 30 ▶use List::Util 1.33 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # lazy...
28 5 ▶my $_acquire_isa_tc = sub { goto \&Moose::Util::TypeConstraints::find_or_create_isa_type_constraint };
Subroutine __ANON__: called CC=1 SCAR=0.0
29  
30  
31 9 parameter writer_prefix => (isa => NonEmptySimpleStr, default => '_set_');
32 9 parameter builder_prefix => (isa => NonEmptySimpleStr, default => '_build_');
33  
34 9 with 'MooseX::AttributeShortcuts::Trait::Attribute::HasAnonBuilder';
35  
36  
37 9 has constraint => (
38 is => 'ro',
39 isa => 'CodeRef',
40 predicate => 'has_constraint',
41 );
42  
43 9 has original_isa => (
44 is => 'ro',
45 predicate => 'has_original_isa',
46 );
47  
48 9 has trigger_method => (
49 is => 'ro',
50 predicate => 'has_trigger_method',
51 );
52  
53  
54 64 ▶after attach_to_class => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
55 64 my ($self, $class) = @_;
56  
57 64 ▶ return unless $self->has_anon_builder && !$self->anon_builder_installed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_anon_builder and not $self->anon_builder_installed 61 3 64
Condition100%3/3
$self->has_anon_builder and not $self->anon_builder_installed
!ll&&!rl&&r
5563
MC/DC100%2/2
$self->has_anon_builder and not $self->anon_builder_installed
$self->has_anon_builder not $self->anon_builder_installed
Truth table100%3/3
A and not(B)
A= $self->has_anon_builderB= not $self->anon_builder_installed
ABresult
0X0
100
111
58  
59 ### install our anon builder as a method: $class->name
60 # $class->add_method($self->builder => $self->anon_builder);
61 3 $class->add_method($self->builder => $self->_builder_method_meta($class));
62 3 $self->_set_anon_builder_installed;
63  
64 3 return;
65 9 };
66  
67  
68 63 ▶before _process_options => sub { shift->_mxas_process_options(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
69  
70 # this feels... bad. But I'm not sure there's any way to ensure we
71 # process options on a clone/extends without wrapping new().
72  
73 64 ▶around new => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
74 64 my ($orig, $self) = (shift, shift);
75 64 my ($name, %options) = @_;
76  
77 $self->_mxas_process_options($name, \%options)
78 64 ▶ if $options{__hack_no_process_options};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"__hack_no_process_options"} 1 63 64
79  
80 64 return $self->$orig($name, %options);
81 9 };
82  
83  
84 # NOTE: remove_delegation() will also automagically remove any custom
85 # accessors we create here
86  
87 # handle: handles => { name => sub { ... }, ... }
88 3 ▶around _make_delegation_method => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
89 3 my ($orig, $self) = (shift, shift);
90 3 my ($name, $coderef) = @_;
91  
92 ### _make_delegation_method() called with a: ref $coderef
93 3 ▶ return $self->$orig(@_)
Branch: 1 branch
BranchTrueFalseTotal
unless "CODE" eq ref $coderef 1 2 3
94 unless 'CODE' eq ref $coderef;
95  
96 # this coderef will be installed as a method on the associated class itself.
97 1 ▶ my $custom_coderef = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine our_accessor: called CC=1 SCAR=0.0
98 # aka $self from the class instance's perspective
99 1 my $associated_class_instance = shift @_;
100  
101 # in $coderef, $_ will be the attribute metaclass
102 1 local $_ = $self;
103 1 return $associated_class_instance->$coderef(@_);
104 2 };
105  
106 2 return $self->_process_accessors(custom => { $name => $custom_coderef });
107 9 };
108  
109 64 ▶sub _mxas_process_options {
Subroutine _mxas_process_options: called CC=4 SCAR=13.9
110 64 my ($class, $name, $options) = @_;
111  
112 64 ▶ my $_has = sub { defined $options->{$_[0]} };
Subroutine __ANON__: called CC=1 SCAR=0.0
113 64 ▶ my $_opt = sub { $_has->(@_) ? $options->{$_[0]} : q{} };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&$_has(@_) ? : 196 121 317
114 64 ▶ my $_ref = sub { ref $_opt->(@_) || q{} };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref &$_opt(@_) or "" 14 144 158
115  
116 # handle: is => ...
117 64 $class->_mxas_is_rwp($name, $options, $_has, $_opt, $_ref);
118 64 $class->_mxas_is_lazy($name, $options, $_has, $_opt, $_ref);
119  
120 # handle: init_arg => 1/-1
121 64 $class->_mxas_init_arg($name, $options, $_has, $_opt, $_ref);
122  
123 # handle: builder => 1, builder => sub { ... }
124 64 $class->_mxas_builder($name, $options, $_has, $_opt, $_ref);
125  
126 # handle: isa_instance_of => ...
127 64 $class->_mxas_isa_instance_of($name, $options, $_has, $_opt, $_ref);
128 # handle: isa => sub { ... }
129 64 $class->_mxas_isa_mooish($name, $options, $_has, $_opt, $_ref);
130  
131 # handle: constraint => ...
132 64 $class->_mxas_constraint($name, $options, $_has, $_opt, $_ref);
133 # handle: coerce => [ ... ]
134 64 $class->_mxas_coerce($name, $options, $_has, $_opt, $_ref);
135  
136  
137 64 my %prefix = (
138 predicate => 'has',
139 clearer => 'clear',
140 trigger => '_trigger_',
141 );
142  
143 64 ▶ my $is_private = sub { $name =~ /^_/ ? $_[0] : $_[1] };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^_/ ? : 16 16 32
144 128 ▶ my $default_for = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
145 128 my ($opt) = @_;
146  
147 128 ▶ return unless $_has->($opt);
Branch: 1 branch
BranchTrueFalseTotal
unless &$_has($opt) 112 16 128
148 16 my $opt_val = $_opt->($opt);
149  
150 16 ▶ my ($head, $mid)
Branch: 2 branches
BranchTrueFalseTotal
$opt_val eq '1' ? : 8 8 16
$opt_val eq '-1' ? : 8 0 8
151 = $opt_val eq '1' ? ($is_private->('_', q{}), $is_private->(q{}, '_'))
152 : $opt_val eq '-1' ? ($is_private->(q{}, '_'), $is_private->(q{}, '_'))
153 : return;
154  
155 16 $options->{$opt} = $head . $prefix{$opt} . $mid . $name;
156 16 return;
157 64 };
158  
159 ### set our other defaults, if requested...
160 64 $default_for->($_) for qw{ predicate clearer };
161 64 my $trigger = "$prefix{trigger}$name";
162 5 ▶ do { $options->{trigger} = sub { shift->$trigger(@_) }; $options->{trigger_method} = $trigger }
Subroutine __ANON__: called CC=1 SCAR=0.0
163 64 ▶ if $options->{trigger} && $options->{trigger} eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"trigger"} and $options->{"trigger"} eq 1) 5 59 64
Condition66%2/3
$options->{"trigger"} and $options->{"trigger"} eq 1
!ll&&!rl&&r
5905
MC/DC50%1/2
$options->{"trigger"} and $options->{"trigger"} eq 1
$options->{"trigger"} $options->{"trigger"} eq 1
Truth table66%2/3
A and B
A= $options->{"trigger"}B= $options->{"trigger"} eq 1
ABresult
0X0
100
111
164  
165 64 return;
166 }
167  
168 # The following two methods are here both to help ensure compatibility with
169 # MooseX::SemiAffordanceAccessor and to enable other packages to modify our
170 # behaviour.
171  
172 14 ▶sub _mxas_writer_name {
Subroutine _mxas_writer_name: called CC=3 SCAR=15.4
173 14 my ($class, $name) = @_;
174  
175 14 ▶ return $class->canonical_writer_prefix . $name
Branch: 1 branch
BranchTrueFalseTotal
unless $class->meta->does_role("MooseX::SemiAffordanceAccessor::Role::Attribute") 14 0 14
176 unless $class->meta->does_role('MooseX::SemiAffordanceAccessor::Role::Attribute');
177  
178 # ok, if we're here then we need to follow that role's scheme
179 0 ▶ return $name =~ /^_/ ? "_set$name" : "set_$name";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^_/ ? : 0 0 0
180 };
181  
182 14 ▶sub _mxas_private_writer_name {
Subroutine _mxas_private_writer_name: called CC=2 SCAR=7.1
183 14 my ($class, $name) = @_;
184  
185 14 $name = $class->_mxas_writer_name($name);
186 14 ▶ return $name =~ /^_/ ? $name : "_$name";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^_/ ? : 14 0 14
187 }
188  
189 # handle: is => 'rwp'
190 64 ▶sub _mxas_is_rwp {
Subroutine _mxas_is_rwp: called CC=2 SCAR=6.9
191 64 my ($class, $name, $options, $_has, $_opt, $_ref) = @_;
192  
193 64 ▶ return unless $_opt->('is') eq 'rwp';
Branch: 1 branch
BranchTrueFalseTotal
unless &$_opt("is") eq "rwp" 50 14 64
194  
195 14 $options->{is} = 'ro';
196 14 $options->{writer} = $class->_mxas_private_writer_name($name);
197  
198 14 return;
199 }
200  
201 # handle: init_arg => 1/-1
202 64 ▶sub _mxas_init_arg {
Subroutine _mxas_init_arg: called CC=7 SCAR=19.5
203 64 my ($class, $name, $options, $_has, $_opt, $_ref) = @_;
204  
205 64 ▶ return unless exists $options->{init_arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"init_arg"} 59 5 64
206  
207 5 ▶ my $one = ($name !~ /^_/) ? $name : $name;
Branch: 1 branch
BranchTrueFalseTotal
!($name =~ /^_/) ? : 3 2 5
208 5 ▶ my $not_one = ($name !~ /^_/) ? "_$name" : do { (local $_ = $name) =~ s/^_//; $_ };
Branch: 1 branch
BranchTrueFalseTotal
!($name =~ /^_/) ? : 3 2 5
209  
210 $options->{init_arg}
211 = ! defined $options->{init_arg} ? return
212 : "$options->{init_arg}" eq 1 ? $one
213 : "$options->{init_arg}" eq -1 ? $not_one
214 : $options->{init_arg}
215 5 ▶ ;
Branch: 3 branches
BranchTrueFalseTotal
!defined($options->{'init_arg'}) ? : 0 5 5
"$options->{'init_arg'}" eq 1 ? : 2 3 5
"$options->{'init_arg'}" eq -1 ? : 2 1 3
216  
217 5 return;
218 }
219  
220 # handle: is => 'lazy'
221 64 ▶sub _mxas_is_lazy {
Subroutine _mxas_is_lazy: called CC=4 SCAR=13.9
222 64 my ($class, $name, $options, $_has, $_opt, $_ref) = @_;
223  
224 64 ▶ return unless $_opt->('is') eq 'lazy';
Branch: 1 branch
BranchTrueFalseTotal
unless &$_opt("is") eq "lazy" 59 5 64
225  
226 5 $options->{is} = 'ro';
227 5 $options->{lazy} = 1;
228 5 ▶ $options->{builder} = 1
Branch: 1 branch
BranchTrueFalseTotal
unless &$_has("builder") or &$_has("default") 1 4 5
Condition100%3/3
&$_has("builder") or &$_has("default")
l!l&&r!l&&!r
221
MC/DC100%2/2
&$_has("builder") or &$_has("default")
&$_has("builder") &$_has("default")
Truth table100%3/3
A or B
A= &$_has("builder")B= &$_has("default")
ABresult
1X1
011
000
229 unless $_has->('builder') || $_has->('default');
230  
231 5 return;
232 }
233  
234 # handle: lazy_build => 'private'
235 0 ▶sub _mxas_lazy_build_private {
Subroutine _mxas_lazy_build_private: not called CC=2 SCAR=17.9
236 0 my ($class, $name, $options, $_has, $_opt, $_ref) = @_;
237  
238 0 ▶ return unless $_opt->('lazy_build') eq 'private';
Branch: 1 branch
BranchTrueFalseTotal
unless &$_opt("lazy_build") eq "private" 0 0 0
239  
240 0 $options->{lazy_build} = 1;
241 0 $options->{clearer} = "_clear_$name";
242 0 $options->{predicate} = "_has_$name";
243  
244 0 return;
245 }
246  
247 # handle: builder => 1, builder => sub { ... }
248 64 ▶sub _mxas_builder {
Subroutine _mxas_builder: called CC=4 SCAR=13.9
249 64 my ($class, $name, $options, $_has, $_opt, $_ref) = @_;
250  
251 64 ▶ return unless $_has->('builder');
Branch: 1 branch
BranchTrueFalseTotal
unless &$_has("builder") 40 24 64
252  
253 24 ▶ if ($_ref->('builder') eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (&$_ref("builder") eq "CODE") 3 21 24
254  
255 3 $options->{anon_builder} = $options->{builder};
256 3 $options->{builder} = 1;
257 }
258  
259 $options->{builder} = $class->_mxas_builder_name($name)
260 24 ▶ if $options->{builder} eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"builder"} eq 1 18 6 24
261  
262 24 return;
263 }
264  
265 64 ▶sub _mxas_isa_mooish {
Subroutine _mxas_isa_mooish: called CC=2 SCAR=6.9
266 64 my ($class, $name, $options, $_has, $_opt, $_ref) = @_;
267  
268 64 ▶ return unless $_ref->('isa') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless &$_ref("isa") eq "CODE" 63 1 64
269  
270 ### build a mooish type constraint...
271 1 $options->{original_isa} = $options->{isa};
272 1 $options->{isa} = MooishTC->new(constraint => $options->{isa});
273  
274 1 return;
275 }
276  
277 # handle: isa_instance_of => ...
278 64 ▶sub _mxas_isa_instance_of {
Subroutine _mxas_isa_instance_of: called CC=3 SCAR=11.2
279 64 my ($class, $name, $options, $_has, $_opt, $_ref) = @_;
280  
281 64 ▶ return unless $_has->('isa_instance_of');
Branch: 1 branch
BranchTrueFalseTotal
unless &$_has("isa_instance_of") 63 1 64
282  
283 1 ▶ if ($_has->('isa')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$_has("isa")) 0 1 1
284  
285 $class->throw_error(
286 q{Cannot use 'isa_instance_of' and 'isa' together for attribute }
287 0 . $_opt->('definition_context')->{package} . '::' . $name
288 );
289 }
290  
291 1 $options->{isa} = class_type(delete $options->{isa_instance_of});
292  
293 1 return;
294 }
295  
296 # handle: coerce => [ ... ]
297 64 ▶sub _mxas_coerce {
Subroutine _mxas_coerce: called CC=10 SCAR=23.5
298 64 my ($class, $name, $options, $_has, $_opt, $_ref) = @_;
299  
300 64 ▶ if ($_ref->('coerce') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (&$_ref("coerce") eq "ARRAY") 1 63 64
301  
302 ### must be type => sub { ... } pairs...
303 1 my @coercions = @{ $_opt->('coerce') };
304 1 ▶ confess 'You must specify an "isa" when declaring "coercion"'
Branch: 1 branch
BranchTrueFalseTotal
unless &$_has("isa") 0 1 1
305 unless $_has->('isa');
306 1 ▶ confess 'coercion array must be in pairs!'
Branch: 1 branch
BranchTrueFalseTotal
if @coercions % 2 0 1 1
307 if @coercions % 2;
308 1 ▶ confess 'must define at least one coercion pair!'
Branch: 1 branch
BranchTrueFalseTotal
unless @coercions > 0 0 1 1
309 unless @coercions > 0;
310  
311 1 my $our_coercion = Moose::Meta::TypeCoercion->new;
312 my $our_type
313 = $options->{original_isa}
314 ? $options->{isa}
315 1 ▶ : $_acquire_isa_tc->($_opt->('isa'))->create_child_type
Branch: 1 branch
BranchTrueFalseTotal
$options->{'original_isa'} ? : 0 1 1
316 ;
317  
318 1 $our_coercion->add_type_coercions(@coercions);
319 1 $our_type->coercion($our_coercion);
320  
321 1 ▶ $options->{original_isa} ||= $options->{isa};
Condition33%1/3
$options->{"original_isa"} ||= $options->{"isa"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$options->{"original_isa"} ||= $options->{"isa"}
$options->{"original_isa"} $options->{"isa"}
Truth table33%1/3
A ||= B
A= $options->{"original_isa"}B= $options->{"isa"}
ABresult
1X1
011
000
322 1 $options->{isa} = $our_type;
323 1 $options->{coerce} = 1;
324  
325 1 return;
326 }
327  
328 # If our original constraint has coercions and our created subtype
329 # did not have any (as specified in the 'coerce' option), then
330 # copy the parent's coercions over.
331  
332 63 ▶ if ($_has->('original_isa') && $_opt->('coerce') eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (&$_has("original_isa") and &$_opt("coerce") eq 1) 1 62 63
Condition100%3/3
&$_has("original_isa") and &$_opt("coerce") eq 1
!ll&&!rl&&r
5931
MC/DC100%2/2
&$_has("original_isa") and &$_opt("coerce") eq 1
&$_has("original_isa") &$_opt("coerce") eq 1
Truth table100%3/3
A and B
A= &$_has("original_isa")B= &$_opt("coerce") eq 1
ABresult
0X0
100
111
333  
334 1 my $isa_type = $_acquire_isa_tc->($_opt->('original_isa'));
335  
336 1 ▶ if ($isa_type->has_coercion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isa_type->has_coercion) 1 0 1
337  
338 # create our coercion as a copy of the parent
339 $_opt->('isa')->coercion(Moose::Meta::TypeCoercion->new(
340 type_constraint => $_opt->('isa'),
341 1 type_coercion_map => [ @{ $isa_type->coercion->type_coercion_map } ],
342 ));
343 }
344  
345 }
346  
347 63 return;
348 }
349  
350 64 ▶sub _mxas_constraint {
Subroutine _mxas_constraint: called CC=5 SCAR=16.4
351 64 my ($class, $name, $options, $_has, $_opt, $_ref) = @_;
352  
353 64 ▶ return unless $_has->('constraint');
Branch: 1 branch
BranchTrueFalseTotal
unless &$_has("constraint") 61 3 64
354  
355 # check for errors...
356 3 ▶ $class->throw_error('You must specify an "isa" when declaring a "constraint"')
Branch: 1 branch
BranchTrueFalseTotal
unless &$_has("isa") 0 3 3
357 if !$_has->('isa');
358 3 ▶ $class->throw_error('"constraint" must be a CODE reference')
Branch: 1 branch
BranchTrueFalseTotal
if &$_ref("constraint") ne "CODE" 0 3 3
359 if $_ref->('constraint') ne 'CODE';
360  
361 # constraint checking! XXX message, etc?
362 3 ▶ push my @opts, constraint => $_opt->('constraint')
Branch: 1 branch
BranchTrueFalseTotal
if &$_ref("constraint") eq "CODE" 3 0 3
363 if $_ref->('constraint') eq 'CODE';
364  
365 # stash our original option away and construct our new one
366 3 my $isa = $options->{original_isa} = $_opt->('isa');
367 3 $options->{isa} = $_acquire_isa_tc->($isa)->create_child_type(@opts);
368  
369 3 return;
370 }
371  
372  
373 3 ▶sub builder_method_metaclass {
Subroutine builder_method_metaclass: called CC=1 SCAR=0.0
Pod documented
374 3 my $self = shift @_;
375  
376 3 return with_traits($self->associated_class->method_metaclass => BuilderTrait);
377 }
378  
379  
380 28 ▶role {
Subroutine __ANON__: called CC=1 SCAR=0.0
381 28 my $p = shift @_;
382  
383 28 ▶ method canonical_writer_prefix => sub { $p->writer_prefix };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine canonical_writer_prefix: called CC=1 SCAR=0.0
384 28 ▶ method canonical_builder_prefix => sub { $p->builder_prefix };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine canonical_builder_prefix: called CC=1 SCAR=0.0
385 9 };
386  
387 9 !!42;
388  
389 __END__
« AttributeShortcuts.pm ↑ Summary AttributeShortcuts/Trait/Role/Attribute.pm »