Valiant/Filters.pm

filter: Statementstmt 84.0% 74 / 88 Branchbran 43.7% 7 / 16 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 15 / 18 Podpod 100% 2 / 2 totaltotal 75.3% 101 / 134 CC 16 SCAR 12.0
CC
16
Cov
76%
import
8.3
_add_metadata
4.3
__ANON__
2.5
CRAP 3.3
« Valiant/Proxy/Array.pm ↑ Summary Valiant/Proxy/Object.pm »
1 package Valiant::Filters;
2  
3 12 ▶use Class::Method::Modifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Valiant::Util 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Moo::_Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 require Moo::Role;
9 7 require Sub::Util;
10  
11 7 our @DEFAULT_ROLES = (qw(Valiant::Filterable));
12 7 our @DEFAULT_EXPORTS = (qw(filters filters_with));
13 7 our %Meta_Data = ();
14  
15 11 ▶sub default_roles { @DEFAULT_ROLES }
Subroutine default_roles: called CC=1 SCAR=0.0
Pod documented
16 22 ▶sub default_exports { @DEFAULT_EXPORTS }
Subroutine default_exports: called CC=1 SCAR=0.0
Pod documented
17  
18 11 ▶sub import {
Subroutine import: called CC=7 SCAR=21.2
19 11 my $class = shift;
20 11 my $target = caller;
21  
22 11 ▶ unless (Moo::Role->is_role($target)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Moo::Role"->is_role($target)) 11 0 11
23 11 my $orig = $target->can('with');
24 0 ▶ Moo::_Utils::_install_tracked($target, 'with', sub {
Subroutine __ANON__: not called CC=2 SCAR=9.2
Subroutine with: called CC=2 SCAR=9.2
25 0 ▶ unless ($target->can('filters_metadata')) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($target->can("filters_metadata")) 0 0 0
unless ($target->can("filters_metadata")) 0 12 12
26 0 $Meta_Data{$target}{'filters'} = \my @data;
27 0 ▶ my $method = Sub::Util::set_subname "${target}::filters_metadata" => sub { @data };
Subroutine __ANON__: not called CC=1 SCAR=2.6
28 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 0 *{"${target}::filters_metadata"} = $method;
30 }
31 0 &$orig;
32 11 });
33 }
34  
35 11 foreach my $default_role ($class->default_roles) {
36 11 ▶ next if Role::Tiny::does_role($target, $default_role);
Branch: 1 branch
BranchTrueFalseTotal
if Role::Tiny::does_role($target, $default_role) 0 11 11
37 11 debug 1, "Applying role '$default_role' to '$target'";
38 11 Moo::Role->apply_roles_to_package($target, $default_role);
39 11 foreach my $export ($class->default_exports) {
40 22 debug 2, "Adding method '__${export}_for_exporter' as alias for '$export'";
41 22 Moo::_Utils::_install_tracked($target, "__${export}_for_exporter", \&{"${target}::${export}"});
42 }
43 }
44  
45 my %cb = map {
46 11 $_ => $target->can("__${_}_for_exporter");
47 } $class->default_exports;
48  
49 11 foreach my $exported_method (keys %cb) {
50 12 ▶ my $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=7.9
Subroutine filters: called CC=2 SCAR=7.9
51 12 ▶ if(Scalar::Util::blessed($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0])) { } 0 12 12
52 0 return $cb{$exported_method}->(@_);
53 } else {
54 0 return $cb{$exported_method}->($target, @_);
55 }
56 22 };
57 22 Moo::_Utils::_install_tracked($target, $exported_method, $sub);
58 }
59  
60 35 ▶ Class::Method::Modifiers::install_modifier $target, 'around', 'has', sub {
Subroutine __ANON__: called CC=2 SCAR=7.5
61 35 my $orig = shift;
62 35 my ($attr, %opts) = @_;
63  
64 35 my $method = \&{"${target}::filters"};
65
66 35 ▶ if(my $validates = delete $opts{filters}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $validates = delete $opts{"filters"}) 0 35 35
67 0 debug 1, "Found filter in attribute '$attr'";
68 0 $method->($attr, @$validates);
69 }
70
71 35 return $orig->($attr, %opts);
72 11 ▶ } if $target->can('has');
Branch: 1 branch
BranchTrueFalseTotal
if $target->can("has") 11 0 11
73 }
74  
75 28 ▶sub _add_metadata {
Subroutine _add_metadata: called CC=4 SCAR=14.5
76 28 my ($target, $type, @add) = @_;
77 28 ▶ my $store = $Meta_Data{$target}{$type} ||= do {
Condition66%2/3
$Meta_Data{$target}{$type} ||= do { my @data; if ("Moo::Role"->is_role($target) or $target->can("${type}_metadata")) { $target->can("around")->("${type}_metadata", sub { my($orig, $self) = (shift(), shift()); $self->$orig(@_), @data; } ); } else { require Sub::Util; my $method = Sub::Util::set_subname("${target}::${type}_metadata", sub { @data; } ); *{"${target}::${type}_metadata";} = $method; } ; \@data }
l!l&&r!l&&!r
17110
MC/DC0%0/2
$Meta_Data{$target}{$type} ||= do { my @data; if ("Moo::Role"->is_role($target) or $target->can("${type}_metadata")) { $target->can("around")->("${type}_metadata", sub { my($orig, $self) = (shift(), shift()); $self->$orig(@_), @data; } ); } else { require Sub::Util; my $method = Sub::Util::set_subname("${target}::${type}_metadata", sub { @data; } ); *{"${target}::${type}_metadata";} = $method; } ; \@data }
$Meta_Data{$target}{$type} do { my @data; if ("Moo::Role"->is_role($target) or $target->can("${type}_metadata")) { $target->can("around")->("${type}_metadata", sub { my($orig, $self) = (shift(), shift()); $self->$orig(@_), @data; } ); } else { require Sub::Util; my $method = Sub::Util::set_subname("${target}::${type}_metadata", sub { @data; } ); *{"${target}::${type}_metadata";} = $method; } ; \@data }
Truth table66%2/3
A ||= B
A= $Meta_Data{$target}{$type}B= do { my @data; if ("Moo::Role"->is_role($target) or $target->can("${type}_metadata")) { $target->can("around")->("${type}_metadata", sub { my($orig, $self) = (shift(), shift()); $self->$orig(@_), @data; } ); } else { require Sub::Util; my $method = Sub::Util::set_subname("${target}::${type}_metadata", sub { @data; } ); *{"${target}::${type}_metadata";} = $method; } ; \@data }
ABresult
1X1
011
000
78 11 my @data;
79 11 ▶ if (Moo::Role->is_role($target) or $target->can("${type}_metadata")) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Moo::Role'->is_role($target) or $target->can("${type}_metadata")) { } 0 11 11
Condition33%1/3
"Moo::Role"->is_role($target) or $target->can("${type}_metadata")
l!l&&r!l&&!r
0011
MC/DC0%0/2
"Moo::Role"->is_role($target) or $target->can("${type}_metadata")
"Moo::Role"->is_role($target) $target->can("${type}_metadata")
Truth table33%1/3
A or B
A= "Moo::Role"->is_role($target)B= $target->can("${type}_metadata")
ABresult
1X1
011
000
80 0 ▶ $target->can('around')->("${type}_metadata", sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 my ($orig, $self) = (shift, shift);
82 0 ($self->$orig(@_), @data);
83 0 });
84 } else {
85 11 require Sub::Util;
86 11 ▶ my $method = Sub::Util::set_subname "${target}::${type}_metadata" => sub { @data };
Subroutine __ANON__: called CC=1 SCAR=0.0
87 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 11 *{"${target}::${type}_metadata"} = $method;
89 }
90 11 \@data;
91 };
92 28 push @$store, @add;
93 28 return;
94 }
95  
96 7 1;
97  
98 =head1 NAME
99  
100 Valiant::Filters - Adds a filter DSL and API to your Moo/se classes
101  
102 =head1 SYNOPSIS
103  
104 package Local::Person;
105  
106 use Moo;
107 use Valiant::Filters;
108  
109 has name => (is => 'ro');
110 has age => (is => 'ro');
111  
112 filters name => (
113 truncate => { max_length=>25 }
114 );
115  
116 filters_with 'Trim';
117  
118 Filters on specific attributes can be added to the C<has> clause if you prefer:
119  
120 package Local::Person;
121  
122 use Moo;
123 use Valiant::Filters;
124  
125 has name => (is => 'ro', filters => [ truncate=>25 ] );
126 has age => (is => 'ro');
127  
128 filters_with 'Trim';
129  
130 Using filters on objects:
131  
132 my $person = Local::Person->new(
133 name => ' Ja ',
134 age => 300,
135 );
136  
137 print $person->name; # "Ja" not " Ja ";
138  
139 See L<Valiant> for overall overview and L<Valiant::Filterable> for additional API
140 level documentation.
141  
142 =head1 DESCRIPTION
143  
144 Using this package will apply the L<Valiant::Filterable> role to your current class
145 as well as import several class methods from that role. It also wraps the C<has>
146 imported method so that you can add attribute filters as arguments to C<has> if
147 you find that approach to be neater than calling C<filter>.
148  
149 You can override several class methods of this package if you need to create your
150 own custom subclass.
151  
152 =head1 IMPORTS
153  
154 The following subroutines are imported from L<Valiant::Filterable>
155  
156 =head2 filters_with
157  
158 Accepts the name of a custom filter or a reference to a function, followed by a list
159 of arguments.
160  
161 filters_with sub {
162 my ($class, $attrs, $opts) = @_;
163 return $attrs;
164 };
165  
166 filters_with 'SpecialFilters', arg1=>'foo', arg2=>'bar';
167  
168 See C<filters_with> in L<Valiant::Filterable> for more.
169  
170 =head2 filters
171  
172 Create filters on an object's attributes. Accepts the name of an attribute (or an
173 arrayref of names) followed by a list of filters and global options. Filters can
174 be a subroutine reference or the name of a Filter class.
175  
176 filters name => sub {
177 my ($class, $attrs, $attribute_name) = @_;
178 return $attrs->{$attribute_name}; # return the filtered value
179 };
180  
181 filters ['name', 'title'] => (
182 trim => 1,
183 );
184  
185 See C<filters> in L<Valiant::Filterable> for more.
186  
187 =head1 METHODS
188  
189 The following class methods are available for subclasses
190  
191 =head2 default_roles
192  
193 Roles that are applied when using this class. Default is L<Valiant::Filterable>. If
194 you are subclassing and wish to apply more roles, or if you've made your own version
195 of L<Valiant::Filterable> you can override this method.
196  
197 =head2 default_exports
198  
199 Methods that are automatically exported into the calling package.
200  
201 =head1 SEE ALSO
202
203 L<Valiant>, L<Valiant::Filterable>
204  
205 =head1 AUTHOR
206
207 See L<Valiant>
208  
209 =head1 COPYRIGHT & LICENSE
210
211 See L<Valiant>
212  
213 =cut
« Valiant/Proxy/Array.pm ↑ Summary Valiant/Proxy/Object.pm »