Valiant/Filter/Each.pm

filter: Statementstmt 95.8% 23 / 24 Branchbran 50.0% 3 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 83.7% 31 / 37 CC 5 SCAR 10.4
CC
5
Cov
87%
generate_attributes
4.6
filter
2.0
BEGIN
1.0
CRAP 2.8
« Valiant/Validations.pm ↑ Summary Valiant/HTML/Util/View.pm »
1 package Valiant::Filter::Each;
2  
3 11 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Valiant::Util 'throw_exception', 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 with 'Valiant::Filter';
8 11 requires 'filter_each';
9  
10 11 has attributes => (is=>'ro', predicate=>'has_attributes');
11 11 has model => (is=>'ro', required=>1);
12  
13  
14 33 ▶sub generate_attributes {
Subroutine generate_attributes: called CC=4 SCAR=15.2
Pod undocumented
15 33 my ($self, $class, $attrs) = @_;
16 33 ▶ return keys %$attrs unless $self->has_attributes; # Just filter everything when this is used with 'filters_with'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_attributes 1 32 33
17 32 ▶ if(ref($self->attributes) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->attributes eq 'ARRAY') { } 32 0 32
elsif (ref $self->attributes eq 'CODE') { } 0 0 0
18 32 return @{ $self->attributes };
19 } elsif(ref($self->attributes) eq 'CODE') {
20 0 return $self->attributes->($class, $attrs);
21 }
22 }
23  
24 33 ▶sub filter {
Subroutine filter: called CC=2 SCAR=6.9
Pod undocumented
25 33 my ($self, $class, $attrs) = @_;
26 33 foreach my $attribute ($self->generate_attributes($class, $attrs)) {
27 36 $attrs->{$attribute} = $self->filter_each($class, $attrs, $attribute);
28 }
29 33 return $attrs;
30 }
31  
32 11 1;
33  
34 =head1 NAME
35  
36 Valiant::Filter::Each - A Role to create custom validators
37  
38 =head1 SYNOPSIS
39  
40 package Valiant::Filter::With;
41  
42 use Moo;
43  
44 with 'Valiant::Filter::Each';
45  
46 has cb => (is=>'ro', required=>1);
47  
48 sub normalize_shortcut {
49 my ($class, $arg) = @_;
50 if((ref($arg)||'') eq 'CODE') {
51 return +{
52 cb => $arg,
53 };
54 }
55 }
56  
57 sub filter_each {
58 my ($self, $class, $attrs, $attribute_name) = @_;
59 return $self->cb->($class, $attrs, $attribute_name);
60 }
61  
62 1;
63  
64 =head1 DESCRIPTION
65  
66 Use this role when you with to create a custom filter that will be run
67 on your class attributes. Please note
68 that you can also use the 'with' validator (L<Valiant::Filter::With>)
69 for simple custom filter needs. Its best to use this role when you
70 want custom filters that is going to be shared across several classes
71 so the effort pays off in reuse.
72  
73 Your class must provide the method C<filter_each>, which will be called
74 once for each attribute in the validation.
75  
76 =head1 SEE ALSO
77
78 L<Valiant>, L<Valiant::Filter>.
79  
80 =head1 AUTHOR
81
82 See L<Valiant>
83
84 =head1 COPYRIGHT & LICENSE
85
86 See L<Valiant>
87  
88 =cut
« Valiant/Validations.pm ↑ Summary Valiant/HTML/Util/View.pm »