blib/lib/MooX/AttributeFilter.pm

filter: Statementstmt 93.2% 97 / 104 Branchbran 92.3% 24 / 26 Conditioncond 84.0% 21 / 25 MC/DCmcdc 80.0% 12 / 15 Subroutinesub 94.1% 16 / 17 Podpod n/a totaltotal 90.9% 170 / 187 CC 23 SCAR 14.3
CC
23
Cov
92%
__ANON__
9.0
__ANON__
4.8
__ANON__
4.0
CRAP 4.2
↑ Summary
1 # ABSTRACT: Implements 'filter' option for Moo-class attributes
2  
3  
4 package MooX::AttributeFilter;
5 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.002002';
9  
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Scalar::Util qw<looks_like_number>;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Class::Method::Modifiers qw(install_modifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Sub::Quote qw<quotify>;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 require Method::Generate::Accessor;
15  
16 3 my %filterClasses;
17  
18 101 ▶sub _generate_filter_source {
Subroutine _generate_filter_source: called CC=3 SCAR=11.0
19 101 my $this = shift;
20 101 my ( $me, $name, $spec, $source ) = @_;
21  
22 101 ▶ if ( $spec->{filter} && $spec->{filter_sub} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"filter"} and $spec->{"filter_sub"}) 63 38 101
Condition100%3/3
$spec->{"filter"} and $spec->{"filter_sub"}
!ll&&!rl&&r
36263
MC/DC100%2/2
$spec->{"filter"} and $spec->{"filter_sub"}
$spec->{"filter"} $spec->{"filter_sub"}
Truth table100%3/3
A and B
A= $spec->{"filter"}B= $spec->{"filter_sub"}
ABresult
0X0
100
111
23 63 $this->{captures}{ '$filter_for_' . $name } = \$spec->{filter_sub};
24 $source =
25 $this->_generate_call_code( $name, 'filter', "${me}, ${source}",
26 63 $spec->{filter_sub} );
27 }
28  
29 101 return $source;
30 }
31  
32 install_modifier "Method::Generate::Accessor", 'around', '_generate_core_set',
33 168 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
34 168 my $orig = shift;
35 168 my $this = shift;
36 168 my ( $me, $name, $spec, $source ) = @_;
37  
38 168 ▶ unless ( $spec->{".filter_dont_generate"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec->{".filter_dont_generate"}) 0 168 168
39 0 $source = _generate_filter_source( $this, $me, $name, $spec, $source );
40 }
41  
42 168 return $orig->( $this, $me, $name, $spec, $source );
43 3 };
44  
45 144 ▶install_modifier "Method::Generate::Accessor", 'around', 'is_simple_set', sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
46 144 my $orig = shift;
47 144 my $this = shift;
48 144 my ( $name, $spec ) = @_;
49 144 ▶ return $orig->( $this, @_ ) && !( $spec->{filter} && $spec->{filter_sub} );
Condition100%6/6
&$orig($this, @_) && !($spec->{'filter'} && $spec->{'filter_sub'})
!ll&&!rl&&r
923814
$spec->{'filter'} && $spec->{'filter_sub'}
!ll&&!rl&&r
12238
MC/DC100%3/3
&$orig($this, @_) && !($spec->{'filter'} && $spec->{'filter_sub'})
&$orig($this, @_) $spec->{'filter'} $spec->{'filter_sub'}
Truth table100%4/4
A && not(B && C)
A= &$orig($this, @_)B= $spec->{'filter'}C= $spec->{'filter_sub'}
ABCresult
0XX0
1110
10X1
1101
50 3 };
51  
52 install_modifier "Method::Generate::Accessor", 'around',
53 4 ▶ '_generate_use_default', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 4 my $orig = shift;
55 4 my $this = shift;
56 4 my ( $me, $name, $spec, $test ) = @_;
57  
58 # Prevent double generation for lazy attributes with default/builder.
59 4 $spec->{'.filter_dont_generate'} = 1;
60 4 return $orig->( $this, @_ );
61 3 };
62  
63 install_modifier "Method::Generate::Accessor", 'around',
64 10 ▶ '_generate_get_default', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 10 my $orig = shift;
66 10 my $this = shift;
67 10 my ( $me, $name, $spec ) = @_;
68  
69 10 my $default = $orig->( $this, $me, $name, $spec );
70  
71 10 return _generate_filter_source( $this, $me, $name, $spec, $default );
72 3 };
73  
74 install_modifier "Method::Generate::Accessor", 'around',
75 97 ▶ '_generate_populate_set', sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
76 97 my $orig = shift;
77 97 my $this = shift;
78 97 my ( $me, $name, $spec, $source, $test, $init_arg ) = @_;
79 97 local $spec->{".filter_dont_generate"} = 1;
80  
81 97 ▶ if ( !$this->has_eager_default( $name, $spec ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->has_eager_default($name, $spec)) 91 6 97
82 91 $source = _generate_filter_source( $this, $me, $name, $spec, $source );
83 }
84  
85 97 return $orig->( $this, $me, $name, $spec, $source, $test, $init_arg );
86 3 };
87  
88 67 ▶install_modifier "Method::Generate::Accessor", 'around', '_generate_set', sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
89 67 my $orig = shift;
90 67 my $this = shift;
91 67 my ( $name, $spec ) = @_;
92 67 local $spec->{".filter_dont_generate"} = 1;
93  
94 67 my $rc = $orig->( $this, @_ );
95  
96 67 ▶ return $rc unless $spec->{filter} && $spec->{filter_sub};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"filter"} and $spec->{"filter_sub"} 10 57 67
Condition66%2/3
$spec->{"filter"} and $spec->{"filter_sub"}
!ll&&!rl&&r
10057
MC/DC50%1/2
$spec->{"filter"} and $spec->{"filter_sub"}
$spec->{"filter"} $spec->{"filter_sub"}
Truth table66%2/3
A and B
A= $spec->{"filter"}B= $spec->{"filter_sub"}
ABresult
0X0
100
111
97  
98 57 my $capName = '$filter_for_' . $name;
99  
100 # Call to the filter was generated already.
101 57 ▶ unless ( $this->{captures}{$capName} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"captures"}{$capName}) 57 0 57
102  
103 # Work around Method::Generate::Accessor limitation: it predefines
104 # source as being $_[1] only and not acceping any argument to define it
105 # externally. For this purpose the only solution we have is to wrap it
106 # into a sub and pass the filter as sub's argument.
107  
108 57 my $name_str = quotify $name;
109 $rc = "sub { $rc }->( \$_[0], "
110 . $this->_generate_call_code( $name, 'filter',
111 "\$_[0], \$_[1], \$_[0]->{${name_str}}",
112 $spec->{filter_sub} )
113 57 . " )";
114 }
115  
116 57 return $rc;
117 3 };
118  
119 install_modifier "Method::Generate::Accessor", 'around', 'generate_method',
120 84 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
121 84 my $orig = shift;
122 84 my $this = shift;
123 84 my ( $into, $name, $spec, $quote_opts ) = @_;
124  
125 84 ▶ if ( $filterClasses{$into} && $spec->{filter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filterClasses{$into} and $spec->{"filter"}) 60 24 84
Condition100%3/3
$filterClasses{$into} and $spec->{"filter"}
!ll&&!rl&&r
42060
MC/DC100%2/2
$filterClasses{$into} and $spec->{"filter"}
$filterClasses{$into} $spec->{"filter"}
Truth table100%3/3
A and B
A= $filterClasses{$into}B= $spec->{"filter"}
ABresult
0X0
100
111
126  
127 croak "Incompatibe 'is' option '$spec->{is}': can't install filter"
128 60 ▶ unless $spec->{is} =~ /^rwp?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"is"} =~ /^rwp?$/ 1 59 60
129  
130 59 my $filterSub;
131 59 ▶ if ( $spec->{filter} eq 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{'filter'} eq 1) { } 14 45 59
132 14 $filterSub = "_filter_${name}";
133 }
134 else {
135 45 $filterSub = $spec->{filter};
136 }
137  
138 # $spec->{filter} = 1;
139  
140 59 ▶ croak "Attribute '$name' filter option has invalid value"
Branch: 1 branch
BranchTrueFalseTotal
if ref $filterSub and ref $filterSub ne "CODE" 1 58 59
Condition100%3/3
ref $filterSub and ref $filterSub ne "CODE"
!ll&&!rl&&r
33251
MC/DC100%2/2
ref $filterSub and ref $filterSub ne "CODE"
ref $filterSub ref $filterSub ne "CODE"
Truth table100%3/3
A and B
A= ref $filterSubB= ref $filterSub ne "CODE"
ABresult
0X0
100
111
141 if ref($filterSub) && ref($filterSub) ne 'CODE';
142  
143 58 ▶ my $filterCode = ref($filterSub) ? $filterSub : $into->can($filterSub);
Branch: 1 branch
BranchTrueFalseTotal
ref $filterSub ? : 25 33 58
144  
145 58 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $filterCode 1 57 58
146 "No filter method '$filterSub' defined for $into attribute '$name'"
147 unless $filterCode;
148  
149 57 $spec->{filter_sub} = $filterCode;
150 }
151  
152 81 return $orig->( $this, @_ );
153 3 };
154  
155 61 ▶sub import {
Subroutine import: called CC=3 SCAR=11.8
156 61 my ($class) = @_;
157 61 my $target = caller;
158  
159 61 ▶ my $trait =
Branch: 1 branch
BranchTrueFalseTotal
'Role::Tiny'->can('is_role') && 'Role::Tiny'->is_role($target) ? : 28 33 61
Condition100%3/3
"Role::Tiny"->can("is_role") and "Role::Tiny"->is_role($target)
!ll&&!rl&&r
42928
MC/DC100%2/2
"Role::Tiny"->can("is_role") and "Role::Tiny"->is_role($target)
"Role::Tiny"->can("is_role") "Role::Tiny"->is_role($target)
Truth table100%3/3
A and B
A= "Role::Tiny"->can("is_role")B= "Role::Tiny"->is_role($target)
ABresult
0X0
100
111
160 Role::Tiny->can('is_role')
161 && Role::Tiny->is_role($target)
162 ? 'MooseX::AttributeFilter::Trait::Attribute::Role'
163 : 'MooseX::AttributeFilter::Trait::Attribute';
164  
165 61 $filterClasses{$target} = 1;
166 80 ▶ install_modifier $target, 'around', 'has', sub {
Subroutine __ANON__: called CC=3 SCAR=13.8
167 80 my $orig = shift;
168 80 my ( $attr, %opts ) = @_;
169 80 ▶ return $orig->( $attr, %opts ) unless $opts{filter};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"filter"} 21 59 80
170 59 ▶ $opts{moosify} ||= [];
Condition50%1/2
$opts{"moosify"} ||= []
l!l
059
MC/DC0%0/1
$opts{"moosify"} ||= []
$opts{"moosify"}
Truth table50%1/2
A
A= $opts{"moosify"}
Aresult
11
00
171 59 ▶ push @{ $opts{moosify} }, sub {
Subroutine __ANON__: not called CC=2 SCAR=15.7
172 0 my ($spec) = @_;
173 require # hide from CPANTS
174 0 MooseX::AttributeFilter;
175 0 ▶ $spec->{traits} ||= [];
Condition0%0/2
$spec->{"traits"} ||= []
l!l
00
MC/DC0%0/1
$spec->{"traits"} ||= []
$spec->{"traits"}
Truth table0%0/2
A
A= $spec->{"traits"}
Aresult
11
00
176 0 $spec->{bypass_filter_method_check} = 1;
177 0 push @{ $spec->{traits} }, $trait;
178 59 };
179 59 $orig->( $attr, %opts );
180 61 };
181 }
182  
183 3 1;
184  
185 __END__
↑ Summary