AttributeFilter/Trait/Attribute.pm

filter: Statementstmt 98.7% 76 / 77 Branchbran 84.2% 32 / 38 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 92.1% 129 / 140 CC 24 SCAR 16.8
CC
24
Cov
93%
__ANON__
8.3
_inline_filter
8.0
__ANON__
5.0
CRAP 5.4
↑ Summary AttributeFilter.pm »
1 package MooseX::AttributeFilter::Trait::Attribute;
2 5 ▶use 5.008009;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = "0.09";
7  
8 5 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 has filter => (
11 is => 'ro',
12 isa => 'CodeRef|Str',
13 predicate => 'has_filter',
14 );
15  
16 5 has bypass_filter_method_check => (
17 is => 'ro',
18 isa => 'Bool',
19 default => 0,
20 );
21  
22 22 ▶after _process_options => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
23 22 my $this = shift;
24 22 my ($name, $options) = @_;
25
26 22 ▶ return unless defined $options->{filter};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"filter"} 5 17 22
27
28 17 ▶ if ($options->{filter} eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"filter"} eq 1) 2 15 17
29 2 $options->{filter} = "_filter_${name}";
30 }
31 5 };
32  
33 22 ▶before install_accessors => sub {
Subroutine __ANON__: called CC=8 SCAR=21.2
34 22 my $this = shift;
35 22 ▶ return if $this->bypass_filter_method_check;
Branch: 1 branch
BranchTrueFalseTotal
if $this->bypass_filter_method_check 0 22 22
36 22 my $filter = $this->filter;
37 22 ▶ if (defined $filter and not ref $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter and not ref $filter) 5 17 22
Condition100%3/3
defined $filter and not ref $filter
!ll&&!rl&&r
5125
MC/DC100%2/2
defined $filter and not ref $filter
defined $filter not ref $filter
Truth table100%3/3
A and not(B)
A= defined $filterB= not ref $filter
ABresult
0X0
100
111
38 5 my $class = $this->associated_class;
39 my $method = $class->find_method_by_name($filter)
40 || $class->name->can($filter)
41 5 ▶ || eval {
Condition66%4/6
$class->find_method_by_name($filter) || $class->name->can($filter) || eval { do { no strict 'refs'; exists &{$class->name . '::' . $filter;} ? \&{$class->name . '::' . $filter;} : undef } }
l!l&&r!l&&!r
401
$class->find_method_by_name($filter) or $class->name->can($filter)
l!l&&r!l&&!r
401
MC/DC33%1/3
$class->find_method_by_name($filter) || $class->name->can($filter) || eval { do { no strict 'refs'; exists &{$class->name . '::' . $filter;} ? \&{$class->name . '::' . $filter;} : undef } }
$class->find_method_by_name($filter) $class->name->can($filter) eval { do { no strict 'refs'; exists &{$class->name . '::' . $filter;} ? \&{$class->name . '::' . $filter;} : undef } }
Truth table50%2/4
A or B || C
A= $class->find_method_by_name($filter)B= $class->name->can($filter)C= eval { do { no strict 'refs'; exists &{$class->name . '::' . $filter;} ? \&{$class->name . '::' . $filter;} : undef } }
ABCresult
1XX1
01X1
0011
0000
42 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 exists &{ $class->name . "::" . $filter }
44 0 ▶ ? \&{ $class->name . "::" . $filter }
Branch: 1 branch
BranchTrueFalseTotal
exists &{$class->name . '::' . $filter;} ? : 0 1 1
45 : undef;
46 };
47
48 5 ▶ die sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if not $method 1 4 5
49 "No filter method '%s' defined for %s attribute '%s'",
50 $filter,
51 $class->name,
52 $this->name,
53 ) if !$method;
54 }
55 5 };
56  
57 9 ▶around _coerce_and_verify => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
58 9 my $next = shift;
59 9 my $this = shift;
60 9 my ($value, $instance) = @_;
61 9 my $filter = $this->filter;
62 9 ▶ if ($filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 7 2 9
63
64 # Need to figure out if we're being called by non-immutable
65 # constructor. This is ugly, but searching the call stack works.
66 7 my $in_ctor = 0;
67 7 for my $level (0 .. 8) { # should be enough
68 28 my (undef, undef, undef, $sub) = caller($level);
69 28 ▶ if ($sub eq 'Moose::Meta::Attribute::initialize_instance_slot') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub eq "Moose::Meta::Attribute::initialize_instance_slot") 7 21 28
70 7 $in_ctor = 1;
71 7 last;
72 }
73 }
74
75 7 ▶ $value = $instance->$filter($value, $in_ctor ? () : $this->get_value($instance));
Branch: 1 branch
BranchTrueFalseTotal
$in_ctor ? : 7 0 7
76 }
77 9 $this->$next($value, $instance);
78 5 };
79  
80 5 has _filter_ix => (
81 is => 'rw',
82 isa => 'Int',
83 );
84  
85 33 ▶sub _inline_filter {
Subroutine _inline_filter: called CC=8 SCAR=20.8
86 33 my $this = shift;
87 33 my ($instance, $value, $filtered, $for_constructor) = @_;
88
89 33 my @code;
90 33 my $filter = $this->filter;
91 33 ▶ if (ref $filter) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $filter) { } 25 8 33
elsif (defined $filter) { } 8 0 8
92 25 ▶ if (not defined $this->_filter_ix) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_filter_ix) 12 13 25
93 # going via metaobject would be smarter
94 # but this is faster. xD
95 12 push(our @FILTERS, $filter);
96 12 $this->_filter_ix($#FILTERS);
97 }
98 25 push @code, sprintf(
99 'my $filter = $%s::FILTERS[%d];',
100 __PACKAGE__,
101 $this->_filter_ix,
102 );
103 25 ▶ push @code, sprintf(
Branch: 2 branches
BranchTrueFalseTotal
$for_constructor ? : 13 12 25
$for_constructor ? : 13 12 25
104 $for_constructor
105 ? 'my %s = %s->%s(%s);'
106 : 'my %s = %s->%s(%s, %s);',
107 $filtered,
108 $instance,
109 '$filter',
110 $value,
111 $for_constructor
112 ? ()
113 : $this->_inline_instance_get($instance),
114 ),
115 }
116 elsif (defined $filter) {
117 8 ▶ push @code, sprintf(
Branch: 2 branches
BranchTrueFalseTotal
$for_constructor ? : 4 4 8
$for_constructor ? : 4 4 8
118 $for_constructor
119 ? 'my %s = %s->%s(%s);'
120 : 'my %s = %s->%s(%s, %s);',
121 $filtered,
122 $instance,
123 $filter,
124 $value,
125 $for_constructor
126 ? ()
127 : $this->_inline_instance_get($instance),
128 ),
129 }
130
131 33 return @code;
132 }
133  
134 44 ▶around _inline_set_value => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
135 44 my $next = shift;
136 44 my $this = shift;
137 44 my ($instance, $value, $tc, $coercion, $message, $for_constructor) = @_;
138 44 ▶ return $this->$next(@_) unless $this->has_filter;
Branch: 1 branch
BranchTrueFalseTotal
unless $this->has_filter 12 32 44
139 32 my @code = (
140 $this->_inline_filter($instance, $value, '$filtered', $for_constructor),
141 $this->$next($instance, '$filtered', $tc, $coercion, $message, $for_constructor),
142 );
143 #use Data::Dumper;
144 #warn Dumper \@code;
145 32 return @code;
146 5 };
147  
148 1 ▶around _inline_init_from_default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
149 1 my $next = shift;
150 1 my $this = shift;
151 1 my ($instance, $default, $tc, $coercion, $message, $for_lazy) = @_;
152 1 my $filtered = '$filtered';
153 1 ▶ return $this->$next(@_) unless $this->has_filter;
Branch: 1 branch
BranchTrueFalseTotal
unless $this->has_filter 0 1 1
154
155 1 ▶ my @code = (
Branch: 1 branch
BranchTrueFalseTotal
$this->has_type_constraint ? : 0 1 1
156 $this->_inline_generate_default($instance, $default),
157 $this->_inline_filter($instance, $default, $filtered, $for_lazy),
158 $this->has_type_constraint
159 ? ($this->_inline_check_coercion($filtered, $tc, $coercion, $for_lazy),
160 $this->_inline_check_constraint($filtered, $tc, $message, $for_lazy))
161 : (),
162 $this->_inline_init_slot($instance, $filtered),
163 $this->_inline_weaken_value($instance, $filtered),
164 );
165 #use Data::Dumper;
166 #warn Dumper \@code;
167 1 return @code;
168 5 };
169  
170 5 1;
171 __END__
↑ Summary AttributeFilter.pm »