| 1 | package Valiant::Filter::ToArray; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use Valiant::Util 'throw_exception'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | with 'Valiant::Filter::Each'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | has split_on => (is=>'ro', predicate=>'has_split_on'); | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | sub normalize_shortcut { | ||||||||||||||||||
|
Subroutine
normalize_shortcut: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 11 | 1 | my ($class, $arg) = @_; | |||||||||||||||||||
| 12 | 1 | return +{ }; | |||||||||||||||||||
| 13 | } | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 3 | ▶ | sub filter_each { | ||||||||||||||||||
|
Subroutine
filter_each: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 16 | 3 | my ($self, $class, $attrs, $attribute_name) = @_; | |||||||||||||||||||
| 17 | 3 | my $value = $attrs->{$attribute_name}; | |||||||||||||||||||
| 18 | 3 | ▶ | return unless defined $value; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 3 | ▶ | if($self->has_split_on) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | 1 | return [split($self->split_on, $value) ]; | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | 2 | ▶ | return (ref($value)||'') eq 'ARRAY' ? $value : [$value]; | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $value || ''
MC/DC100%1/1
ref $value || ''
ref $value
Truth table100%2/2
A
A= ref $value
| |||||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 1 | 1; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | =head1 NAME | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | Valiant::Filter::ToArray - Force the value into an arrayref if its not one already | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | package Local::Test; | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | use Moo; | ||||||||||||||||||||
| 37 | use Valiant::Filters; | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | has 'string' => (is=>'ro', required=>1); | ||||||||||||||||||||
| 40 | has 'array' => (is=>'ro', required=>1); | ||||||||||||||||||||
| 41 | has 'split' => (is=>'ro', required=>1); | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | filters ['string', 'array'] => (to_array => 1); | ||||||||||||||||||||
| 44 | filters split => (to_array => +{ split_on => ',' } ); | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | my $object = Local::Test->new( | ||||||||||||||||||||
| 47 | string => 'foo', | ||||||||||||||||||||
| 48 | array => ['bar', 'baz'], | ||||||||||||||||||||
| 49 | split = '123', | ||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | $object->string; # ['foo'] | ||||||||||||||||||||
| 53 | $object->array; # ['bar', 'baz'] | ||||||||||||||||||||
| 54 | $object->split: # [1, 2, 3] | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =head1 DESCRIPTION | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | Force any scalar values to arrayref. Basically normalize on an arrayref. Allows you | ||||||||||||||||||||
| 59 | to specific a split pattern or just make the string into an arrayref | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | This filter defines the following attributes | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =head2 split_on | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | Optional. A pattern used via C<split> to split a string into an arrayref. If not | ||||||||||||||||||||
| 68 | present just use the string as is to make an arrayref. | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | =head1 SEE ALSO | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | L<Valiant>, L<Valiant::Filter>, L<Valiant::Validator::Filter>. | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =head1 AUTHOR | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | See L<Valiant> | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | See L<Valiant> | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||