| 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
| |||||||||||||||
| 17 | 32 | ▶ | if(ref($self->attributes) eq 'ARRAY') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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 | ||||||||||||||