blib/lib/MooseX/CustomInitArgs.pm

filter: Statementstmt 99.2% 132 / 133 Branchbran 95.0% 19 / 20 Conditioncond 72.2% 13 / 18 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 25 / 25 Podpod 100% 2 / 2 totaltotal 94.2% 196 / 208 CC 18 SCAR 10.2
CC
18
Cov
96%
__ANON__
6.0
_inline_param_negotiation
4.1
_run_init_coderef
4.0
CRAP 2.8
↑ Summary
1 package MooseX::CustomInitArgs;
2  
3 8 our $AUTHORITY = 'cpan:TOBYINK';
4 8 our $VERSION = '0.004';
5  
6 8 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use constant _AttrTrait => do
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 {
13 package MooseX::CustomInitArgs::Trait::Attribute;
14 8 our $AUTHORITY = 'cpan:TOBYINK';
15 8 our $VERSION = '0.004';
16
17 8 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶ use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶ use B 'perlstring';
Subroutine BEGIN: called CC=1 SCAR=0.0
20
21 8 subtype 'OptList', as 'ArrayRef[ArrayRef]';
22 coerce 'OptList',
23 8 ▶ from 'ArrayRef' => via {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 8 require Data::OptList;
25 8 Data::OptList::mkopt $_;
26 },
27 1 ▶ from 'HashRef' => via {
Subroutine __ANON__: called CC=1 SCAR=0.0
28 1 my $hash = $_;
29 1 [ map { [ $_ => $hash->{$_} ] } sort keys %$hash ];
30 8 };
31
32 8 has init_args => (
33 is => 'ro',
34 isa => 'OptList',
35 predicate => 'has_init_args',
36 coerce => 1,
37 );
38
39 has _init_args_hashref => (
40 is => 'ro',
41 isa => 'HashRef',
42 lazy => 1,
43 8 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 8 my $self = shift;
45 8 +{ map { ;$_->[0] => $_->[1] } @{$self->init_args} };
46 },
47 8 );
48
49 around new => sub
50 10 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
51 10 my $orig = shift;
52 10 my $class = shift;
53 10 my $self = $class->$orig(@_);
54
55 10 ▶ if ($self->has_init_args and not $self->has_init_arg)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_init_args and not $self->has_init_arg) 1 9 10
Condition100%3/3
$self->has_init_args and not $self->has_init_arg
!ll&&!rl&&r
181
MC/DC100%2/2
$self->has_init_args and not $self->has_init_arg
$self->has_init_args not $self->has_init_arg
Truth table100%3/3
A and not(B)
A= $self->has_init_argsB= not $self->has_init_arg
ABresult
0X0
100
111
56 {
57 1 confess "Attribute ${\$self->name} defined with init_args but no init_arg";
58 }
59
60 9 return $self;
61 8 };
62
63 sub _inline_param_negotiation
64 9 ▶ {
Subroutine _inline_param_negotiation: called CC=4 SCAR=14.0
65 9 my ($self, $param) = @_;
66 9 my $init = $self->init_arg;
67
68 9 ▶ my $regex = join '|', map quotemeta, $self->init_arg, map $_->[0], @{$self->init_args||[]};
Condition50%1/2
$self->init_args || []
l!l
90
MC/DC0%0/1
$self->init_args || []
$self->init_args
Truth table50%1/2
A
A= $self->init_args
Aresult
11
00
69 9 ▶ my $with_coderef = join '|', map quotemeta, map $_->[0], grep { defined($_->[1]) } @{$self->init_args||[]};
Condition50%1/2
$self->init_args || []
l!l
90
MC/DC0%0/1
$self->init_args || []
$self->init_args
Truth table50%1/2
A
A= $self->init_args
Aresult
11
00
70 9 ▶ my $no_coderef = join '|', map quotemeta, map $_->[0], grep { !defined($_->[1]) } @{$self->init_args||[]};
Condition50%1/2
$self->init_args || []
l!l
90
MC/DC0%0/1
$self->init_args || []
$self->init_args
Truth table50%1/2
A
A= $self->init_args
Aresult
11
00
71
72 return (
73 9 "if (my \@supplied = grep /^(?:$regex)\$/, keys \%${param}) {",
74 ' if (@supplied > 1) {',
75 ' Carp::confess("Conflicting init_args (@{[join q(, ), sort @supplied]})");',
76 ' }',
77 " elsif (grep /^(?:$no_coderef)\$/, \@supplied) { ",
78 9 " ${param}->{${\perlstring $self->init_arg}} = delete ${param}->{\$supplied[0]};",
79 " }",
80 " elsif (grep /^($with_coderef)\$/, \@supplied) { ",
81 " my \$x = delete ${param}->{\$supplied[0]};",
82 9 " ${param}->{${\perlstring $self->init_arg}} = \$MxCIA_attrs{${\$self->name}}->_run_init_coderef(\$supplied[0], \$class, \$x);",
83 " }",
84 "}",
85 );
86 }
87
88 sub _run_init_coderef
89 22 ▶ {
Subroutine _run_init_coderef: called CC=4 SCAR=14.0
90 22 my ($self, $arg, $class, $value) = @_;
91
92 22 my $code = $self->_init_args_hashref->{$arg};
93 22 ▶ ref $code eq 'SCALAR' and $code = $$code;
Branch: 1 branch
BranchTrueFalseTotal
ref $code eq "SCALAR" and $code = $$code 2 20 22
94
95 22 ▶ if (ref $code eq "MooseX::CustomInitArgs::Sub::AfterTC")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code eq "MooseX::CustomInitArgs::Sub::AfterTC") 2 20 22
96 {
97 2 ▶ if ($self->should_coerce) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->should_coerce) { } 2 0 2
98 2 $value = $self->type_constraint->assert_coerce($value);
99 }
100 else {
101 0 $self->type_constraint->assert_valid($value);
102 }
103 }
104
105 22 local $_ = $value;
106 22 $class->$code($value);
107 }
108
109 around initialize_instance_slot => sub
110 39 ▶ {
Subroutine __ANON__: called CC=6 SCAR=17.9
111 39 my $orig = shift;
112 39 my $self = shift;
113 39 my ($meta_instance, $instance, $params) = @_;
114
115 39 ▶ $self->has_init_args
Branch: 1 branch
BranchTrueFalseTotal
$self->has_init_args or return $self->$orig(@_) 33 6 39
116 or return $self->$orig(@_);
117
118 33 ▶ my @supplied = grep { exists $params->{$_->[0]} } @{$self->init_args}
Branch: 1 branch
BranchTrueFalseTotal
my(@supplied) = grep({exists $$params{$_->[0]};} @{$self->init_args;}) or return $self->$orig(@_) 24 9 33
119 or return $self->$orig(@_);
120
121 24 ▶ if (exists $params->{$self->init_arg})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$params{$self->init_arg}) 3 21 24
122 {
123 3 push @supplied, [ $self->init_arg => undef ];
124 }
125
126 24 ▶ if (@supplied > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@supplied > 1) 4 20 24
127 {
128 4 confess sprintf(
129 'Conflicting init_args (%s)',
130 join(', ', sort map $_->[0], @supplied)
131 );
132 }
133
134 20 ▶ if ($supplied[0][1])
Branch: 1 branch
BranchTrueFalseTotal
if ($supplied[0][1]) { } 11 9 20
135 {
136 $self->_set_initial_slot_value(
137 $meta_instance,
138 $instance,
139 11 $self->_run_init_coderef($supplied[0][0], $instance, delete $params->{ $supplied[0][0] }),
140 );
141 }
142 else
143 {
144 $self->_set_initial_slot_value(
145 $meta_instance,
146 $instance,
147 9 delete $params->{$supplied[0][0]},
148 );
149 }
150
151 20 return;
152 8 };
153
154 8 __PACKAGE__;
155 8 };
156  
157 8 ▶use constant _ClassTrait => do
Subroutine BEGIN: called CC=1 SCAR=0.0
158 8 {
159 package MooseX::CustomInitArgs::Trait::Class;
160 8 our $AUTHORITY = 'cpan:TOBYINK';
161 8 our $VERSION = '0.004';
162
163 8 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
164
165 8 has _mxcia_hash => (
166 is => 'ro',
167 isa => 'HashRef',
168 lazy => 1,
169 builder => '_build__mxcia_hash',
170 );
171
172 sub _build__mxcia_hash
173 9 ▶ {
Subroutine _build__mxcia_hash: called CC=2 SCAR=7.0
174 9 my $self = shift;
175 return +{
176 10 map { ;$_->name => $_ }
177 9 ▶ grep { ;$_->can('does') && $_->does(MooseX::CustomInitArgs->_AttrTrait) }
Condition66%2/3
$_->can('does') && $_->does('MooseX::CustomInitArgs'->_AttrTrait)
!ll&&!rl&&r
0110
MC/DC50%1/2
$_->can('does') && $_->does('MooseX::CustomInitArgs'->_AttrTrait)
$_->can('does') $_->does('MooseX::CustomInitArgs'->_AttrTrait)
Truth table66%2/3
A && B
A= $_->can('does')B= $_->does('MooseX::CustomInitArgs'->_AttrTrait)
ABresult
0X0
100
111
178 $self->get_all_attributes
179 };
180 }
181
182 around _eval_environment => sub
183 9 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
184 9 my $orig = shift;
185 9 my $self = shift;
186 9 my $eval = $self->$orig(@_);
187 9 $eval->{'%MxCIA_attrs'} = $self->_mxcia_hash;
188 9 return $eval;
189 8 };
190
191 around _inline_slot_initializer => sub
192 11 ▶ {
Subroutine __ANON__: called CC=4 SCAR=13.9
193 11 my $orig = shift;
194 11 my $self = shift;
195 11 my ($attr, $idx) = @_;
196
197 11 ▶ return $self->$orig(@_)
Branch: 1 branch
BranchTrueFalseTotal
unless $attr->can("does") and $attr->does("MooseX::CustomInitArgs"->_AttrTrait) and $attr->has_init_args 2 9 11
Condition83%5/6
$attr->can("does") and $attr->does("MooseX::CustomInitArgs"->_AttrTrait) and $attr->has_init_args
!ll&&!rl&&r
119
$attr->can("does") and $attr->does("MooseX::CustomInitArgs"->_AttrTrait)
!ll&&!rl&&r
0110
MC/DC66%2/3
$attr->can("does") and $attr->does("MooseX::CustomInitArgs"->_AttrTrait) and $attr->has_init_args
$attr->can("does") $attr->does("MooseX::CustomInitArgs"->_AttrTrait) $attr->has_init_args
Truth table75%3/4
A and B and C
A= $attr->can("does")B= $attr->does("MooseX::CustomInitArgs"->_AttrTrait)C= $attr->has_init_args
ABCresult
0XX0
10X0
1100
1111
198 unless $attr->can('does')
199 && $attr->does(MooseX::CustomInitArgs->_AttrTrait)
200 && $attr->has_init_args;
201
202 return (
203 9 $attr->_inline_param_negotiation('$params'),
204 $self->$orig(@_),
205 );
206 8 };
207
208 8 __PACKAGE__;
209 8 };
210  
211 8 ▶use constant _ApplicationTrait => do
Subroutine BEGIN: called CC=1 SCAR=0.0
212 8 {
213 package MooseX::CustomInitArgs::Trait::Application;
214 8 our $AUTHORITY = 'cpan:TOBYINK';
215 8 our $VERSION = '0.004';
216
217 8 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
218  
219 around apply => sub
220 5 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
221 5 my $orig = shift;
222 5 my $self = shift;
223 5 my ($role, $applied_to) = @_;
224 5 $applied_to = Moose::Util::MetaRole::apply_metaroles(
225 for => $applied_to->name,
226 class_metaroles => {
227 class => [ MooseX::CustomInitArgs->_ClassTrait ],
228 },
229 role_metaroles => {
230 application_to_class => [ MooseX::CustomInitArgs->_ApplicationTrait ],
231 application_to_role => [ MooseX::CustomInitArgs->_ApplicationTrait ],
232 },
233 );
234 5 $self->$orig($role, $applied_to);
235 8 };
236
237 8 __PACKAGE__;
238 8 };
239  
240 1 ▶sub after_typecheck (&) {
Subroutine after_typecheck: called CC=1 SCAR=0.0
Pod documented
241 1 bless $_[0], "MooseX::CustomInitArgs::Sub::AfterTC";
242 }
243  
244 1 ▶sub before_typecheck (&) { $_[0] }
Subroutine before_typecheck: called CC=1 SCAR=0.0
Pod documented
245  
246 8 Moose::Exporter->setup_import_methods(
247 as_is => [
248 qw( before_typecheck after_typecheck )
249 ],
250 class_metaroles => {
251 class => [ _ClassTrait ],
252 attribute => [ _AttrTrait ],
253 },
254 role_metaroles => {
255 application_to_class => [ _ApplicationTrait ],
256 application_to_role => [ _ApplicationTrait ],
257 applied_attribute => [ _AttrTrait ],
258 },
259 );
260  
261 8 1;
262  
263 __END__
↑ Summary