| 1 | package Valiant::Filter::With; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 3 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use Valiant::Util 'throw_exception'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 3 | with 'Valiant::Filter::Each'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 3 | has cb => (is=>'ro', required=>1); | |||||||||||||||||||
| 9 | 3 | has opts => (is=>'ro', predicate=>'has_opts'); | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 4 | ▶ | sub normalize_shortcut { | ||||||||||||||||||
|
Subroutine
normalize_shortcut: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||||||||||
| 12 | 4 | my ($class, $arg) = @_; | |||||||||||||||||||
| 13 | 4 | ▶ | if((ref($arg)||'') eq 'CODE') { | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $arg || ''
MC/DC0%0/1
ref $arg || ''
ref $arg
Truth table50%1/2
A
A= ref $arg
| |||||||||||||||||||||
| 14 | return +{ | ||||||||||||||||||||
| 15 | 3 | cb => $arg, | |||||||||||||||||||
| 16 | }; | ||||||||||||||||||||
| 17 | } | ||||||||||||||||||||
| 18 | 1 | ▶ | if((ref($arg)||'') eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $arg || ''
MC/DC0%0/1
ref $arg || ''
ref $arg
Truth table50%1/2
A
A= ref $arg
| |||||||||||||||||||||
| 19 | return +{ | ||||||||||||||||||||
| 20 | 1 | cb => $arg->[0], | |||||||||||||||||||
| 21 | opts => $arg->[1], | ||||||||||||||||||||
| 22 | }; | ||||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 7 | ▶ | sub filter_each { | ||||||||||||||||||
|
Subroutine
filter_each: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 28 | 7 | my ($self, $class, $attrs, $attribute_name) = @_; | |||||||||||||||||||
| 29 | 7 | my @args = ($class, $attrs, $attribute_name); | |||||||||||||||||||
| 30 | 7 | ▶ | push @args, $self->opts if $self->has_opts; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | 7 | return $self->cb->(@args); | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 3 | 1; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head1 NAME | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | Valiant::Validator::With - Filter using a coderef and options opts | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =head1 SYNOPSIS | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | package Local::Test::User; | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | use Moo; | ||||||||||||||||||||
| 45 | use Valiant::Filters; | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | has 'name' => (is=>'ro', required=>1); | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | filters name => ( | ||||||||||||||||||||
| 50 | with => { | ||||||||||||||||||||
| 51 | cb => sub { | ||||||||||||||||||||
| 52 | my ($class, $attrs, $name, $opts) = @_; | ||||||||||||||||||||
| 53 | return $attrs->{$name}.$opts->{a}; | ||||||||||||||||||||
| 54 | }, | ||||||||||||||||||||
| 55 | opts => +{ a=>'foo' }, | ||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||
| 57 | with => sub { | ||||||||||||||||||||
| 58 | my ($class, $attrs, $name) = @_; | ||||||||||||||||||||
| 59 | return $attrs->{$name}.'bar'; | ||||||||||||||||||||
| 60 | }, | ||||||||||||||||||||
| 61 | with => [sub { | ||||||||||||||||||||
| 62 | my ($class, $attrs, $name, $opts) = @_; | ||||||||||||||||||||
| 63 | return $attrs->{$name}.$opts; | ||||||||||||||||||||
| 64 | }, 'baz'], | ||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | my $user = Local::Test::User->new(name=>'john'); | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | print $user->name; # 'johnfoobarbaz' | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =head1 DESCRIPTIONi | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | this filter allows you to make a custom subroutine reference into a filter, with | ||||||||||||||||||||
| 74 | options options for parameterization. You can use this when you have a very | ||||||||||||||||||||
| 75 | special filter need but don't feel like writing a custom filter by subclassing | ||||||||||||||||||||
| 76 | L<Valiant::Filter::Each>. | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =head1 Passing parameters to $opts | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | You can pass parameters to the C<$opts> hashref using the C<opts> argument: | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | my $filter = sub { | ||||||||||||||||||||
| 83 | my ($self, $class, $attrs, $attribute_name, $opts) = @_; | ||||||||||||||||||||
| 84 | my $old_value = $attrs->{$attribute_name}; | ||||||||||||||||||||
| 85 | # ... | ||||||||||||||||||||
| 86 | return $new_value | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | }; | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | filters my_attribute => ( | ||||||||||||||||||||
| 91 | with => { | ||||||||||||||||||||
| 92 | cb => $filter, | ||||||||||||||||||||
| 93 | opts => {arg => 2000}, | ||||||||||||||||||||
| 94 | }, | ||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | You might find this useful in creating more parametered callbacks. However at this point | ||||||||||||||||||||
| 99 | you might wish to consider just writing a custom filter class. | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =head1 SHORTCUT FORM | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | This filter supports the follow shortcut forms: | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | validates attribute => ( with => sub { my $self = shift; ... }, ... ); | ||||||||||||||||||||
| 106 | validates attribute => ( with => [\&method, [1,2,3]], ... ); | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | Which is the same as: | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | validates attribute => ( | ||||||||||||||||||||
| 111 | with => { | ||||||||||||||||||||
| 112 | cb => sub { ... }, | ||||||||||||||||||||
| 113 | }, | ||||||||||||||||||||
| 114 | ... | ||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | validates attribute => ( | ||||||||||||||||||||
| 118 | with => { | ||||||||||||||||||||
| 119 | cb => \&method, | ||||||||||||||||||||
| 120 | opts => [1,2,3], | ||||||||||||||||||||
| 121 | }, | ||||||||||||||||||||
| 122 | ... | ||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head1 SEE ALSO | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | L<Valiant>, L<Valiant::Filter>, L<Valiant::Filter::Each>. | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =head1 AUTHOR | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | See L<Valiant> | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | See L<Valiant> | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||