| 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
Condition100%3/3
$spec->{"filter"} and $spec->{"filter_sub"}
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"}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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'})
$spec->{'filter'} && $spec->{'filter_sub'}
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'}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$spec->{"filter"} and $spec->{"filter_sub"}
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"}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$filterClasses{$into} and $spec->{"filter"}
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"}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
ref $filterSub and ref $filterSub ne "CODE"
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"
| |||||||||||||||||||||||||||||||||||
| 141 | if ref($filterSub) && ref($filterSub) ne 'CODE'; | ||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||
| 143 | 80 | ▶ | my $filterCode = ref($filterSub) ? $filterSub : $into->can($filterSub); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||
| 145 | 80 | ▶ | croak | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
"Role::Tiny"->can("is_role") and "Role::Tiny"->is_role($target)
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)
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 170 | 80 | ▶ | $opts{moosify} ||= []; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"moosify"} ||= []
MC/DC0%0/1
$opts{"moosify"} ||= []
$opts{"moosify"}
Truth table50%1/2
A
A= $opts{"moosify"}
| |||||||||||||||||||||||||||||||||||
| 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"} ||= []
MC/DC0%0/1
$spec->{"traits"} ||= []
$spec->{"traits"}
Truth table0%0/2
A
A= $spec->{"traits"}
| |||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||