Deflator/Meta/Role/Attribute.pm

filter: Statementstmt 65.1% 71 / 109 Branchbran 44.0% 22 / 50 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 63.1% 12 / 19 Podpod 100% 4 / 4 totaltotal 57.8% 111 / 192 CC 31 SCAR 21.2
CC
31
Cov
58%
:after
14.3
_inline_deflator
14.3
has_deflator
12.0
CRAP 8.4
« LazyInflator/Meta/Role/Attribute.pm ↑ Summary Deflator/Structured.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::Deflator::Meta::Role::Attribute;
11 {
12 10 $MooseX::Attribute::Deflator::Meta::Role::Attribute::VERSION = '2.2.2';
13 }
14  
15 # ABSTRACT: Attribute meta role to support deflation
16 10 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Eval::Closure;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Devel::PartialDump;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use MooseX::Attribute::Deflator;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 my $REGISTRY = MooseX::Attribute::Deflator->get_registry;
22 10 ▶no MooseX::Attribute::Deflator;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 10 has is_deflator_inlined => ( is => 'rw', isa => 'Bool', default => 0 );
25 10 has is_inflator_inlined => ( is => 'rw', isa => 'Bool', default => 0 );
26  
27 0 ▶sub _inline_deflator {
Subroutine _inline_deflator: not called CC=8 SCAR=26.6
Subroutine :after: called CC=8 SCAR=26.6
28 0 my $self = shift;
29 0 my $role = Moose::Meta::Role->create_anon_role;
30 0 foreach my $type (qw(deflator inflator)) {
31 0 my $find = "find_$type";
32 0 ▶ my $method = $type eq 'deflator' ? 'deflate' : 'inflate';
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'deflator' ? : 0 0 0
$type eq 'deflator' ? : 36 36 72
33 0 my $tc = $self->type_constraint;
34 0 my $slot_access = $self->_inline_instance_get('$_[1]');
35 0 my $has_value = $self->_inline_instance_has('$_[1]');
36 0 my @check_lazy = $self->_inline_check_lazy(
37 '$_[1]', '$type_constraint',
38 '$type_coercion', '$type_message',
39 );
40 my @deflator = $tc
41 0 ▶ ? do {
Branch: 2 branches
BranchTrueFalseTotal
$tc ? : 0 0 0
$tc ? : 70 2 72
42 0 ( $tc, undef, my $inline ) = $REGISTRY->$find($tc);
43 0 ▶ next unless $inline;
Branch: 2 branches
BranchTrueFalseTotal
unless $inline 0 0 0
unless $inline 3 67 70
44 0 my $find_sub;
45 100 ▶ $find_sub = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
46 100 my $type_constraint = shift;
47 100 my @tc = $REGISTRY->$find($type_constraint);
48 100 ▶ return join( "\n",
Branch: 1 branch
BranchTrueFalseTotal
unless $tc[2] 2 98 100
49 'my ($tc, $via) = $registry->find_'
50 . $type
51 . '(Moose::Util::TypeConstraints::find_type_constraint("'
52 . quotemeta($type_constraint) . '"));',
53 'my ($attr, $obj, @rest) = @_;',
54 '$via->($attr, $tc, sub { $attr->deflate($obj, @rest) });'
55 ) unless ( $tc[2] );
56 98 return $tc[2]->( $self, $tc[0], $find_sub );
57 0 };
58 0 $inline->( $self, $tc, $find_sub );
59 }
60 : '$value';
61 0 ▶ @deflator = (
Branch: 2 branches
BranchTrueFalseTotal
if $tc 0 0 0
if $tc 67 2 69
62 'my $deflated = eval {',
63 @deflator,
64 '};',
65 'if($@) {',
66 'Moose->throw_error("Failed to '
67 . $method
68 . ' value " . Devel::PartialDump->new->dump($value) . " ('
69 . $tc->name
70 . '): $@");',
71 '}',
72 'return $deflated;',
73 ) if ($tc);
74 0 my @code = ( 'sub {', 'my $value = $_[2];' );
75 0 ▶ if ( $type eq 'deflator' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq "deflator") 0 0 0
if ($type eq "deflator") 34 35 69
76 0 push( @code,
77 'unless(defined $_[2]) {',
78 @check_lazy,
79 "return undef unless($has_value);",
80 '$value = ' . $slot_access . ';', '}', );
81 }
82 $role->add_method(
83 $method => eval_closure(
84 environment => {
85 0 %{ $self->_eval_environment },
86 '$registry' => \$REGISTRY
87 },
88 source => join( "\n", @code, @deflator, '}' )
89 )
90 );
91 0 ▶ $type eq 'deflator'
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'deflator' ? : 0 0 0
$type eq 'deflator' ? : 34 35 69
92 ? $self->is_deflator_inlined(1)
93 : $self->is_inflator_inlined(1);
94 }
95 0 Moose::Util::apply_all_roles( $self, $role );
96 }
97  
98 4 ▶sub deflate {
Subroutine deflate: called CC=6 SCAR=21.2
Pod documented
99 4 my ( $self, $obj, $value, $constraint, @rest ) = @_;
100 4 ▶ $value = $self->get_value($obj) unless ( defined $value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 4 0 4
101 4 ▶ return undef unless ( defined $value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
102 4 ▶ $constraint ||= $self->type_constraint;
Condition33%1/3
$constraint ||= $self->type_constraint
l!l&&r!l&&!r
040
MC/DC0%0/2
$constraint ||= $self->type_constraint
$constraint $self->type_constraint
Truth table33%1/3
A ||= B
A= $constraintB= $self->type_constraint
ABresult
1X1
011
000
103 4 ▶ return $value unless ($constraint);
Branch: 1 branch
BranchTrueFalseTotal
unless $constraint 0 4 4
104 4 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
unless ($constraint, my $via) = $REGISTRY->find_deflator($constraint) 0 4 4
105 unless ( ( $constraint, my $via )
106 = $REGISTRY->find_deflator($constraint) );
107 4 my $return;
108 4 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=9.2
109 $return = $via->(
110 0 ▶ $self, $constraint, sub { $self->deflate( $obj, @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 $obj, @rest
112 4 ) for ($value);
113 }
114 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 my $dump = Devel::PartialDump->new->dump($value);
116 0 Moose->throw_error(
117 0 qq{Failed to deflate value $dump (${\($constraint->name)}): $_});
118 4 };
119 4 return $return;
120 }
121  
122 1 ▶sub inflate {
Subroutine inflate: called CC=5 SCAR=18.5
Pod documented
123 1 my ( $self, $obj, $value, $constraint, @rest ) = @_;
124 1 ▶ return undef unless ( defined $value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 1 1
125 1 ▶ $constraint ||= $self->type_constraint;
Condition33%1/3
$constraint ||= $self->type_constraint
l!l&&r!l&&!r
010
MC/DC0%0/2
$constraint ||= $self->type_constraint
$constraint $self->type_constraint
Truth table33%1/3
A ||= B
A= $constraintB= $self->type_constraint
ABresult
1X1
011
000
126 1 ▶ return $value unless ($constraint);
Branch: 1 branch
BranchTrueFalseTotal
unless $constraint 0 1 1
127 1 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
unless ($constraint, my $via) = $REGISTRY->find_inflator($constraint) 0 1 1
128 unless ( ( $constraint, my $via )
129 = $REGISTRY->find_inflator($constraint) );
130 1 my $return;
131 1 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=9.2
132 $return = $via->(
133 0 ▶ $self, $constraint, sub { $self->inflate( $obj, @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 $obj, @rest
135 1 ) for ($value);
136 }
137 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 die
139 0 qq{Failed to inflate value "$value" (${\($constraint->name)}): $_};
140 1 };
141 1 return $return;
142 }
143  
144 0 ▶sub has_deflator {
Subroutine has_deflator: not called CC=3 SCAR=24.8
Pod documented
145 0 my $self = shift;
146 0 ▶ return unless ( $self->has_type_constraint );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_type_constraint 0 0 0
147 0 my @tc = $REGISTRY->find_deflator( $self->type_constraint, 'norecurse' );
148 0 ▶ return @tc ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@tc ? : 0 0 0
149 }
150  
151 0 ▶sub has_inflator {
Subroutine has_inflator: not called CC=3 SCAR=24.8
Pod documented
152 0 my $self = shift;
153 0 ▶ return unless ( $self->has_type_constraint );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_type_constraint 0 0 0
154 0 my @tc = $REGISTRY->find_inflator( $self->type_constraint, 'norecurse' );
155 0 ▶ return @tc ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@tc ? : 0 0 0
156 }
157  
158 10 ▶after install_accessors => \&_inline_deflator if ( $Moose::VERSION >= 1.9 );
Branch: 1 branch
BranchTrueFalseTotal
if $Moose::VERSION >= "1.9" 10 0 10
159  
160 10 1;
161  
162  
163  
164 =pod
165  
166 =head1 NAME
167  
168 MooseX::Attribute::Deflator::Meta::Role::Attribute - Attribute meta role to support deflation
169  
170 =head1 VERSION
171  
172 version 2.2.2
173  
174 =head1 SYNOPSIS
175  
176 package Test;
177  
178 use Moose;
179 use DateTime;
180  
181 use MooseX::Attribute::Deflator;
182  
183 deflate 'DateTime', via { $_->epoch };
184 inflate 'DateTime', via { DateTime->from_epoch( epoch => $_ ) };
185  
186 no MooseX::Attribute::Deflator;
187  
188 has now => ( is => 'rw',
189 isa => 'DateTime',
190 required => 1,
191 default => sub { DateTime->now },
192 traits => ['Deflator'] );
193  
194 package main;
195
196 my $obj = Test->new;
197 my $attr = $obj->meta->get_attribute('now');
198
199 my $deflated = $attr->deflate($obj);
200 # $deflated is now a number
201
202 my inflated = $attr->inflate($obj, $deflated);
203 # $inflated is now a DateTime object
204  
205 =head1 METHODS
206  
207 These two methods work basically the same. They look up the type constraint
208 which is associated with the attribute and try to find an appropriate
209 deflator/inflator. If there is no deflator/inflator for the exact type
210 constraint, the method will bubble up the type constraint hierarchy
211 until it finds one.
212  
213 =over 4
214  
215 =item B<< $attr->deflate($instance) >>
216  
217 Returns the deflated value of the attribute. It does not change the value
218 of the attribute.
219  
220 =item B<< $attr->inflate($instance, $string) >>
221  
222 Inflates a string C<$string>. This method does not set the value of
223 the attribute to the inflated value.
224  
225 =item B<< $attr->has_inflator >>
226 =item B<< $attr->has_deflator >>
227  
228 =back
229  
230 =head1 AUTHOR
231  
232 Moritz Onken
233  
234 =head1 COPYRIGHT AND LICENSE
235  
236 This software is Copyright (c) 2012 by Moritz Onken.
237  
238 This is free software, licensed under:
239  
240 The (three-clause) BSD License
241  
242 =cut
243  
244  
245 __END__
« LazyInflator/Meta/Role/Attribute.pm ↑ Summary Deflator/Structured.pm »