blib/lib/MooX/AttributeFilter.pm

filter: Statementstmt 93.2% 96 / 103 Branchbran 87.5% 21 / 24 Conditioncond 80.0% 20 / 25 MC/DCmcdc 73.3% 11 / 15 Subroutinesub 94.1% 16 / 17 Podpod n/a totaltotal 89.1% 164 / 184 CC 22 SCAR 13.5
CC
22
Cov
90%
__ANON__
8.0
__ANON__
4.8
__ANON__
4.2
CRAP 3.8
↑ Summary build_lib/__MAF/Test.pm »
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.003000'; # TRIAL
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 159 ▶sub _generate_filter_source {
Subroutine _generate_filter_source: called CC=3 SCAR=11.0
19 159 my $this = shift;
20 159 my ( $me, $name, $spec, $source ) = @_;
21  
22 159 ▶ if ( $spec->{filter} && $spec->{filter_sub} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"filter"} and $spec->{"filter_sub"}) 84 75 159
Condition100%3/3
$spec->{"filter"} and $spec->{"filter_sub"}
!ll&&!rl&&r
73284
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 84 $this->{captures}{ '$filter_for_' . $name } = \$spec->{filter_sub};
24 $source =
25 $this->_generate_call_code( $name, 'filter', "${me}, ${source}",
26 84 $spec->{filter_sub} );
27 }
28  
29 159 return $source;
30 }
31  
32 install_modifier "Method::Generate::Accessor", 'around', '_generate_core_set',
33 238 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
34 238 my $orig = shift;
35 238 my $this = shift;
36 238 my ( $me, $name, $spec, $source ) = @_;
37  
38 238 ▶ unless ( $spec->{".filter_dont_generate"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec->{".filter_dont_generate"}) 0 238 238
39 0 $source = _generate_filter_source( $this, $me, $name, $spec, $source );
40 }
41  
42 238 return $orig->( $this, $me, $name, $spec, $source );
43 3 };
44  
45 218 ▶install_modifier "Method::Generate::Accessor", 'around', 'is_simple_set', sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
46 218 my $orig = shift;
47 218 my $this = shift;
48 218 my ( $name, $spec ) = @_;
49 218 ▶ return $orig->( $this, @_ ) && !( $spec->{filter} && $spec->{filter_sub} );
Condition100%6/6
&$orig($this, @_) && !($spec->{'filter'} && $spec->{'filter_sub'})
!ll&&!rl&&r
1214264
$spec->{'filter'} && $spec->{'filter_sub'}
!ll&&!rl&&r
613142
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 155 ▶ '_generate_populate_set', sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
76 155 my $orig = shift;
77 155 my $this = shift;
78 155 my ( $me, $name, $spec, $source, $test, $init_arg ) = @_;
79 155 local $spec->{".filter_dont_generate"} = 1;
80  
81 155 ▶ if ( !$this->has_eager_default( $name, $spec ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->has_eager_default($name, $spec)) 149 6 155
82 149 $source = _generate_filter_source( $this, $me, $name, $spec, $source );
83 }
84  
85 155 return $orig->( $this, $me, $name, $spec, $source, $test, $init_arg );
86 3 };
87  
88 79 ▶install_modifier "Method::Generate::Accessor", 'around', '_generate_set', sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
89 79 my $orig = shift;
90 79 my $this = shift;
91 79 my ( $name, $spec ) = @_;
92 79 local $spec->{".filter_dont_generate"} = 1;
93  
94 79 my $rc = $orig->( $this, @_ );
95  
96 79 ▶ return $rc unless $spec->{filter} && $spec->{filter_sub};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"filter"} and $spec->{"filter_sub"} 0 79 79
Condition33%1/3
$spec->{"filter"} and $spec->{"filter_sub"}
!ll&&!rl&&r
0079
MC/DC0%0/2
$spec->{"filter"} and $spec->{"filter_sub"}
$spec->{"filter"} $spec->{"filter_sub"}
Truth table33%1/3
A and B
A= $spec->{"filter"}B= $spec->{"filter_sub"}
ABresult
0X0
100
111
97  
98 79 my $capName = '$filter_for_' . $name;
99  
100 # Call to the filter was generated already.
101 79 ▶ unless ( $this->{captures}{$capName} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"captures"}{$capName}) 79 0 79
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 79 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 79 . " )";
114 }
115  
116 79 return $rc;
117 3 };
118  
119 install_modifier "Method::Generate::Accessor", 'around', 'generate_method',
120 145 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
121 145 my $orig = shift;
122 145 my $this = shift;
123 145 my ( $into, $name, $spec, $quote_opts ) = @_;
124  
125 145 ▶ if ( $filterClasses{$into} && $spec->{filter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filterClasses{$into} and $spec->{"filter"}) 81 64 145
Condition100%3/3
$filterClasses{$into} and $spec->{"filter"}
!ll&&!rl&&r
65881
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 # unless $spec->{is} =~ /^(rwp?|ro)$/;
129  
130 81 my $filterSub;
131 81 ▶ if ( $spec->{filter} eq 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{'filter'} eq 1) { } 24 57 81
132 24 $filterSub = "_filter_${name}";
133 }
134 else {
135 57 $filterSub = $spec->{filter};
136 }
137  
138 # $spec->{filter} = 1;
139  
140 81 ▶ croak "Attribute '$name' filter option has invalid value"
Branch: 1 branch
BranchTrueFalseTotal
if ref $filterSub and ref $filterSub ne "CODE" 1 80 81
Condition100%3/3
ref $filterSub and ref $filterSub ne "CODE"
!ll&&!rl&&r
53271
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 80 ▶ my $filterCode = ref($filterSub) ? $filterSub : $into->can($filterSub);
Branch: 1 branch
BranchTrueFalseTotal
ref $filterSub ? : 27 53 80
144  
145 80 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $filterCode 1 79 80
146 "No filter method '$filterSub' defined for $into attribute '$name'"
147 unless $filterCode;
148  
149 79 $spec->{filter_sub} = $filterCode;
150 }
151  
152 143 return $orig->( $this, @_ );
153 3 };
154  
155 86 ▶sub import {
Subroutine import: called CC=3 SCAR=11.8
156 86 my ($class) = @_;
157 86 my $target = caller;
158  
159 86 ▶ my $trait =
Branch: 1 branch
BranchTrueFalseTotal
'Role::Tiny'->can('is_role') && 'Role::Tiny'->is_role($target) ? : 41 45 86
Condition100%3/3
"Role::Tiny"->can("is_role") and "Role::Tiny"->is_role($target)
!ll&&!rl&&r
34241
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 86 $filterClasses{$target} = 1;
166 139 ▶ install_modifier $target, 'around', 'has', sub {
Subroutine __ANON__: called CC=3 SCAR=13.8
167 139 my $orig = shift;
168 139 my ( $attr, %opts ) = @_;
169 139 ▶ return $orig->( $attr, %opts ) unless $opts{filter};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"filter"} 59 80 139
170 80 ▶ $opts{moosify} ||= [];
Condition50%1/2
$opts{"moosify"} ||= []
l!l
080
MC/DC0%0/1
$opts{"moosify"} ||= []
$opts{"moosify"}
Truth table50%1/2
A
A= $opts{"moosify"}
Aresult
11
00
171 80 ▶ 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 80 };
179 80 $orig->( $attr, %opts );
180 86 };
181 }
182  
183 3 1;
184  
185 __END__
↑ Summary build_lib/__MAF/Test.pm »