LazyInflator/Meta/Role/Attribute.pm

filter: Statementstmt 69.7% 69 / 99 Branchbran 32.6% 15 / 46 Conditioncond 30.2% 13 / 43 MC/DCmcdc 7.6% 2 / 26 Subroutinesub 70.5% 12 / 17 Podpod 100% 1 / 1 totaltotal 48.2% 112 / 232 CC 35 SCAR 33.2
CC
35
Cov
52%
is_inflated
110.0
__ANON__
20.0
__ANON__
20.0
CRAP 27.7
↑ Summary Deflator/Meta/Role/Attribute.pm »
1 #
2 # This file is part of MooseX-Attribute-Deflator
3 #
4 # This software is Copyright (c) 2012 by Moritz Onken.
5 #
6 # This is free software, licensed under:
7 #
8 # The (three-clause) BSD License
9 #
10 package MooseX::Attribute::LazyInflator::Meta::Role::Attribute;
11 {
12 6 $MooseX::Attribute::LazyInflator::Meta::Role::Attribute::VERSION = '2.2.2';
13 }
14  
15 # ABSTRACT: Lazy inflate attributes
16 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use Eval::Closure;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 6 with 'MooseX::Attribute::Deflator::Meta::Role::Attribute';
22  
23 62 ▶override verify_against_type_constraint => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
24 62 my ( $self, $value, undef, $instance ) = @_;
25 62 ▶ return $value if ( !$self->is_inflated( $instance, undef, $value ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_inflated($instance, undef, $value) 19 43 62
26 43 return super();
27 6 };
28  
29 24 ▶before get_value => sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
30 24 my ( $self, $instance ) = @_;
31 return
32 24 ▶ if ( !$self->has_value($instance) || $self->is_inflated($instance) );
Branch: 1 branch
BranchTrueFalseTotal
if not $self->has_value($instance) or $self->is_inflated($instance) 16 8 24
Condition100%3/3
not $self->has_value($instance) or $self->is_inflated($instance)
l!l&&r!l&&!r
888
MC/DC100%2/2
not $self->has_value($instance) or $self->is_inflated($instance)
not $self->has_value($instance) $self->is_inflated($instance)
Truth table100%3/3
not(A) or B
A= not $self->has_value($instance)B= $self->is_inflated($instance)
ABresult
1X1
011
000
33 8 my $value = $self->inflate( $instance, $self->get_raw_value($instance) );
34 8 ▶ $value = $self->type_constraint->coerce($value)
Branch: 1 branch
BranchTrueFalseTotal
if $self->should_coerce and $self->type_constraint->has_coercion 0 8 8
Condition33%1/3
$self->should_coerce and $self->type_constraint->has_coercion
!ll&&!rl&&r
800
MC/DC0%0/2
$self->should_coerce and $self->type_constraint->has_coercion
$self->should_coerce $self->type_constraint->has_coercion
Truth table33%1/3
A and B
A= $self->should_coerceB= $self->type_constraint->has_coercion
ABresult
0X0
100
111
35 if ( $self->should_coerce && $self->type_constraint->has_coercion );
36 8 $self->verify_against_type_constraint( $value, instance => $instance );
37 8 $self->set_raw_value( $instance, $value );
38 6 };
39  
40 # this is just for reference, this method is replaced with an inlined version
41 0 ▶sub is_inflated {
Subroutine is_inflated: not called CC=10 SCAR=47.0
Pod documented
42 0 my ( $self, $instance, $value, $from_constructor ) = @_;
43 0 ▶ return $instance->_inflated_attributes->{ $self->name } = $value
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
44 if ( defined $value );
45 0 ▶ if ( $instance->_inflated_attributes->{ $self->name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance->_inflated_attributes->{$self->name}) { } 0 0 0
46 0 return 1;
47 }
48 else {
49 0 ▶ my $value
Branch: 1 branch
BranchTrueFalseTotal
defined $from_constructor ? : 0 0 0
50 = defined $from_constructor
51 ? $from_constructor
52 : $self->get_raw_value($instance);
53 0 ▶ return 1 if(!defined $value && $self->is_required);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value and $self->is_required 0 0 0
Condition0%0/3
not defined $value and $self->is_required
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $value and $self->is_required
not defined $value $self->is_required
Truth table0%0/3
not(A) and B
A= not defined $valueB= $self->is_required
ABresult
0X0
100
111
54 0 ▶ $value = $self->type_constraint->coerce($value)
Branch: 1 branch
BranchTrueFalseTotal
if $self->should_coerce and $self->type_constraint->has_coercion 0 0 0
Condition0%0/3
$self->should_coerce and $self->type_constraint->has_coercion
!ll&&!rl&&r
000
MC/DC0%0/2
$self->should_coerce and $self->type_constraint->has_coercion
$self->should_coerce $self->type_constraint->has_coercion
Truth table0%0/3
A and B
A= $self->should_coerceB= $self->type_constraint->has_coercion
ABresult
0X0
100
111
55 if ( $self->should_coerce
56 && $self->type_constraint->has_coercion );
57 return
58 $self->has_type_constraint
59 && $self->type_constraint->check($value)
60 0 ▶ && ++$instance->_inflated_attributes->{ $self->name };
Condition0%0/6
$self->has_type_constraint && $self->type_constraint->check($value) && ++$instance->_inflated_attributes->{$self->name}
!ll&&!rl&&r
000
$self->has_type_constraint and $self->type_constraint->check($value)
!ll&&!rl&&r
000
MC/DC0%0/3
$self->has_type_constraint && $self->type_constraint->check($value) && ++$instance->_inflated_attributes->{$self->name}
$self->has_type_constraint $self->type_constraint->check($value) ++$instance->_inflated_attributes->{$self->name}
Truth table0%0/4
A and B && C
A= $self->has_type_constraintB= $self->type_constraint->check($value)C= ++$instance->_inflated_attributes->{$self->name}
ABCresult
0XX0
10X0
1100
1111
61 }
62 }
63  
64 19 ▶after install_accessors => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 19 my $self = shift;
66 my @code
67 19 = $self->_inline_instance_is_inflated( '$_[1]', '$type_constraint',
68 '$type_coercion', '$value', );
69 19 my $role = Moose::Meta::Role->create_anon_role;
70 19 $role->add_method(
71 'is_inflated' => eval_closure(
72 environment => $self->_eval_environment,
73 source => join( "\n",
74 'sub {',
75 'return $_[1]->{_inflated_attributes}->{"'
76 . quotemeta( $self->name )
77 . '"} = $_[2] if(defined $_[2]);',
78 'my $value = defined $_[3] ? $_[3] : '
79 . $self->_inline_instance_get('$_[1]') . ';',
80 @code,
81 '}' ),
82 )
83 );
84 19 Moose::Util::apply_all_roles($self, $role);
85 6 ▶} if ( $Moose::VERSION >= 1.9 );
Branch: 1 branch
BranchTrueFalseTotal
if $Moose::VERSION >= "1.9" 6 0 6
86  
87 6 ▶if ( Moose->VERSION < 1.9900 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Moose"->VERSION < "1.99") 0 6 6
88 0 require MooseX::Attribute::LazyInflator::Meta::Role::Method::Accessor;
89 0 ▶ override accessor_metaclass => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 'MooseX::Attribute::LazyInflator::Meta::Role::Method::Accessor';
91 0 };
92 }
93  
94 sub _inline_instance_is_inflated {
95 my ( $self, $instance, $tc, $tc_obj, $value ) = @_;
96 my @code
97 = ( $instance
98 . '->{_inflated_attributes}->{"'
99 . quotemeta( $self->name )
100 . '"}' );
101 return 1 if ( !$self->has_type_constraint ); # TODO return 1 ?
102 $value ||= $self->_inline_instance_get($instance);
103 my $coerce
104 = $self->should_coerce && $self->type_constraint->has_coercion
105 ? $tc_obj . '->coerce(' . $value . ')'
106 : $value;
107 push @code,
108 ( ' || ('
109 . $tc . '->('
110 . $coerce
111 . ') && ++'
112 . $instance
113 . '->{_inflated_attributes}->{"'
114 . quotemeta( $self->name )
115 . '"})' );
116 return @code;
117 }
118  
119 6 ▶if ( Moose->VERSION >= 2.0100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Moose'->VERSION >= 2.01) { } 6 0 6
120 19 ▶ override _inline_get_value => sub {
Subroutine __ANON__: called CC=5 SCAR=16.8
121 19 my ( $self, $instance, $tc, $coercion, $message ) = @_;
122 19 ▶ $tc ||= '$type_constraint';
Condition50%1/2
$tc ||= "\$type_constraint"
l!l
019
MC/DC0%0/1
$tc ||= "\$type_constraint"
$tc
Truth table50%1/2
A
A= $tc
Aresult
11
00
123 19 ▶ $coercion ||= '$type_coercion';
Condition50%1/2
$coercion ||= "\$type_coercion"
l!l
019
MC/DC0%0/1
$coercion ||= "\$type_coercion"
$coercion
Truth table50%1/2
A
A= $coercion
Aresult
11
00
124 19 ▶ $message ||= '$type_message';
Condition50%1/2
$message ||= "\$type_message"
l!l
019
MC/DC0%0/1
$message ||= "\$type_message"
$message
Truth table50%1/2
A
A= $message
Aresult
11
00
125 19 my $slot_exists = $self->_inline_instance_has($instance);
126 19 ▶ my @code = (
Branch: 1 branch
BranchTrueFalseTotal
$self->has_type_constraint ? : 19 0 19
127 "if($slot_exists && !(",
128 $self->_inline_instance_is_inflated( $instance, $tc, $coercion ),
129 ")) {",
130 'my $inflated = '
131 . "\$attr->inflate($instance, "
132 . $self->_inline_instance_get($instance) . ");",
133 $self->has_type_constraint
134 ? ( $self->_inline_check_coercion(
135 '$inflated', $tc, $coercion, 1
136 ),
137 $self->_inline_check_constraint(
138 '$inflated', $tc, $message, 1
139 )
140 )
141 : (),
142 $self->_inline_init_slot( $instance, '$inflated' ),
143 "}"
144 );
145 19 push @code, super();
146 19 return @code;
147 6 };
148  
149 __PACKAGE__->meta->add_method(
150 36 ▶ _inline_instance_is_inflated => sub {
Subroutine _inline_instance_is_inflated: called CC=6 SCAR=19.4
Subroutine __ANON__: called CC=6 SCAR=19.4
151 36 my ( $self, $instance, $tc, $coercion, $value ) = @_;
152 my @code
153 36 = ( $instance
154 . '->{_inflated_attributes}->{"'
155 . quotemeta( $self->name )
156 . '"}' );
157 36 ▶ return @code if ( !$self->has_type_constraint );
Branch: 2 branches
BranchTrueFalseTotal
unless $self->has_type_constraint 0 36 36
unless $self->has_type_constraint 0 2 2
158 36 ▶ $value ||= $self->_inline_instance_get($instance);
Condition66%4/6
$value ||= $self->_inline_instance_get($instance)
l!l&&r!l&&!r
18180
$value ||= $self->_inline_instance_get($instance)
l!l&&r!l&&!r
110
MC/DC0%0/2
$value ||= $self->_inline_instance_get($instance)
$value $self->_inline_instance_get($instance)
MC/DC0%0/2
$value ||= $self->_inline_instance_get($instance)
$value $self->_inline_instance_get($instance)
Truth table66%2/3
A ||= B
A= $valueB= $self->_inline_instance_get($instance)
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $valueB= $self->_inline_instance_get($instance)
ABresult
1X1
011
000
159 36 ▶ my $coerce
Branch: 2 branches
BranchTrueFalseTotal
$self->should_coerce && $self->type_constraint->has_coercion ? : 0 36 36
$self->should_coerce && $self->type_constraint->has_coercion ? : 2 0 2
Condition33%2/6
$self->should_coerce and $self->type_constraint->has_coercion
!ll&&!rl&&r
3600
$self->should_coerce and $self->type_constraint->has_coercion
!ll&&!rl&&r
002
MC/DC0%0/2
$self->should_coerce and $self->type_constraint->has_coercion
$self->should_coerce $self->type_constraint->has_coercion
MC/DC0%0/2
$self->should_coerce and $self->type_constraint->has_coercion
$self->should_coerce $self->type_constraint->has_coercion
Truth table33%1/3
A and B
A= $self->should_coerceB= $self->type_constraint->has_coercion
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $self->should_coerceB= $self->type_constraint->has_coercion
ABresult
0X0
100
111
160 = $self->should_coerce && $self->type_constraint->has_coercion
161 ? $coercion . '->(' . $value . ')'
162 : $value;
163 36 ▶ my $check
Branch: 2 branches
BranchTrueFalseTotal
$self->type_constraint->can_be_inlined ? : 36 0 36
$self->type_constraint->can_be_inlined ? : 2 0 2
164 = $self->type_constraint->can_be_inlined
165 ? $self->type_constraint->_inline_check($coerce)
166 : $tc . '->(' . $coerce . ')';
167 36 push @code,
168 ( ' || ('
169 . $check
170 . ' && ++'
171 . $instance
172 . '->{_inflated_attributes}->{"'
173 . quotemeta( $self->name )
174 . '"})' );
175 36 return @code;
176 }
177 6 );
178  
179 42 ▶ override _inline_tc_code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
180 42 my $self = shift;
181 42 my ( $value, $tc, $coercion, $message, $is_lazy ) = @_;
182 return (
183 42 $self->_inline_check_coercion(
184 $value, $tc, $coercion, $is_lazy,
185 ),
186  
187 # $self->_inline_check_constraint(
188 # $value, $tc, $message, $is_lazy,
189 # ),
190 );
191 6 };
192  
193 76 ▶ override _eval_environment => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
194 76 my $self = shift;
195 76 return { %{ super() }, '$attr' => \$self, };
196 6 };
197 }
198 else {
199 0 ▶ override _inline_get_value => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
200 0 my ( $self, $instance, $tc, $tc_obj ) = @_;
201 0 ▶ $tc ||= '$type_constraint';
Condition0%0/2
$tc ||= "\$type_constraint"
l!l
00
MC/DC0%0/1
$tc ||= "\$type_constraint"
$tc
Truth table0%0/2
A
A= $tc
Aresult
11
00
202 0 ▶ $tc_obj ||= '$type_constraint_obj';
Condition0%0/2
$tc_obj ||= "\$type_constraint_obj"
l!l
00
MC/DC0%0/1
$tc_obj ||= "\$type_constraint_obj"
$tc_obj
Truth table0%0/2
A
A= $tc_obj
Aresult
11
00
203 0 my $slot_exists = $self->_inline_instance_has($instance);
204 0 ▶ my @code = (
Branch: 1 branch
BranchTrueFalseTotal
$self->has_type_constraint ? : 0 0 0
205 "if($slot_exists && !(",
206 $self->_inline_instance_is_inflated( $instance, $tc, $tc_obj ),
207 ")) {",
208 'my $inflated = '
209 . "\$attr->inflate($instance, "
210 . $self->_inline_instance_get($instance) . ");",
211 $self->has_type_constraint
212 ? ( $self->_inline_check_coercion( '$inflated', $tc, $tc_obj, 1 ),
213 $self->_inline_check_constraint(
214 '$inflated', $tc, $tc_obj, 1
215 )
216 )
217 : (),
218 $self->_inline_init_slot( $instance, '$inflated' ),
219 "}"
220 );
221 0 push @code, super();
222 0 return @code;
223 }
224 0 ▶ if Moose->VERSION >= 1.9900;
Branch: 1 branch
BranchTrueFalseTotal
if "Moose"->VERSION >= "1.99" 0 0 0
225  
226 __PACKAGE__->meta->add_method(
227 0 ▶ _inline_instance_is_inflated => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
228 0 my ( $self, $instance, $tc, $tc_obj ) = @_;
229 my @code
230 0 = ( $instance
231 . '->{_inflated_attributes}->{"'
232 . quotemeta( $self->name )
233 . '"}' );
234 0 ▶ return @code if ( !$self->has_type_constraint );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_type_constraint 0 0 0
235 0 my $value = $self->_inline_instance_get($instance);
236 0 ▶ my $coerce
Branch: 1 branch
BranchTrueFalseTotal
$self->should_coerce && $self->type_constraint->has_coercion ? : 0 0 0
Condition0%0/3
$self->should_coerce and $self->type_constraint->has_coercion
!ll&&!rl&&r
000
MC/DC0%0/2
$self->should_coerce and $self->type_constraint->has_coercion
$self->should_coerce $self->type_constraint->has_coercion
Truth table0%0/3
A and B
A= $self->should_coerceB= $self->type_constraint->has_coercion
ABresult
0X0
100
111
237 = $self->should_coerce && $self->type_constraint->has_coercion
238 ? $tc_obj . '->coerce(' . $value . ')'
239 : $value;
240 0 push @code,
241 ( ' || ('
242 . $tc . '->('
243 . $coerce
244 . ') && ++'
245 . $instance
246 . '->{_inflated_attributes}->{"'
247 . quotemeta( $self->name )
248 . '"})' );
249 0 return @code;
250 }
251 0 );
252  
253 0 ▶ override _inline_tc_code => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
254 0 my $self = shift;
255 return (
256 0 $self->_inline_check_coercion(@_),
257  
258 # $self->_inline_check_constraint(@_),
259 );
260 }
261 0 ▶ if Moose->VERSION >= 1.9900;
Branch: 1 branch
BranchTrueFalseTotal
if "Moose"->VERSION >= "1.99" 0 0 0
262 }
263  
264 6 1;
265  
266  
267  
268 =pod
269  
270 =head1 NAME
271  
272 MooseX::Attribute::LazyInflator::Meta::Role::Attribute - Lazy inflate attributes
273  
274 =head1 VERSION
275  
276 version 2.2.2
277  
278 =head1 SYNOPSIS
279  
280 package Test;
281  
282 use Moose;
283 use MooseX::Attribute::LazyInflator;
284 # Load default deflators and inflators
285 use MooseX::Attribute::Deflator::Moose;
286  
287 has hash => ( is => 'rw',
288 isa => 'HashRef',
289 traits => ['LazyInflator'] );
290  
291 package main;
292
293 my $obj = Test->new( hash => '{"foo":"bar"}' );
294 # Attribute 'hash' is being inflated to a HashRef on access
295 $obj->hash;
296  
297 =head1 ROLES
298  
299 This role consumes L<MooseX::Attribute::Deflator::Meta::Role::Attribute>.
300  
301 =head1 METHODS
302  
303 =over 8
304  
305 =item B<is_inflated( $intance )>
306  
307 Returns a true value if the value of the attribute passes the type contraint
308 or has been inflated.
309  
310 =item before B<get_value>
311  
312 The attribute's value is being inflated and set if it has a value and hasn't been inflated yet.
313  
314 =item override B<verify_against_type_constraint>
315  
316 Will return true if the attribute hasn't been inflated yet.
317  
318 =back
319  
320 =head1 FUNCTIONS
321  
322 =over 8
323  
324 =item B<accessor_metaclass>
325  
326 The accessor metaclass is set to L<MooseX::Attribute::LazyInflator::Meta::Role::Method::Accessor>.
327  
328 =back
329  
330 =head1 AUTHOR
331  
332 Moritz Onken
333  
334 =head1 COPYRIGHT AND LICENSE
335  
336 This software is Copyright (c) 2012 by Moritz Onken.
337  
338 This is free software, licensed under:
339  
340 The (three-clause) BSD License
341  
342 =cut
343  
344  
345 __END__
↑ Summary Deflator/Meta/Role/Attribute.pm »