| 1 | package Valiant::Filter::Flatten; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | with 'Valiant::Filter::Each'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | has pick => (is=>'ro', predicate=>'has_pick'); | |||||||||
| 8 | 1 | has join => (is=>'ro', predicate=>'has_join'); | |||||||||
| 9 | 1 | has sprintf => (is=>'ro', predicate=>'has_sprintf'); | |||||||||
| 10 | 1 | has pattern => (is=>'ro', predicate=>'has_pattern'); | |||||||||
| 11 | |||||||||||
| 12 | 0 | ▶ | sub normalize_shortcut { | ||||||||
|
Subroutine
normalize_shortcut: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 13 | 0 | my ($class, $arg) = @_; | |||||||||
| 14 | 0 | return +{ }; | |||||||||
| 15 | } | ||||||||||
| 16 | |||||||||||
| 17 | 5 | ▶ | sub filter_each { | ||||||||
|
Subroutine
filter_each: called CC=9 SCAR=22.5
Pod
undocumented
| |||||||||||
| 18 | 5 | my ($self, $class, $attrs, $attribute_name) = @_; | |||||||||
| 19 | 5 | my $value = $attrs->{$attribute_name}; | |||||||||
| 20 | 5 | ▶ | return unless defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | |||||||||||
| 22 | 5 | ▶ | if($self->has_pick) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 2 | ▶ | return $value->[0] if $self->pick eq 'first'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 1 | ▶ | return $value->[-1] if $self->pick eq 'last'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 0 | die '"pick" must be either "first" or "last"'; | |||||||||
| 26 | } | ||||||||||
| 27 | 3 | ▶ | if($self->has_join) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 1 | return join($self->join, @$value); | |||||||||
| 29 | } | ||||||||||
| 30 | 2 | ▶ | if($self->has_sprintf) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 1 | return sprintf($self->sprintf, @$value); | |||||||||
| 32 | } | ||||||||||
| 33 | 1 | ▶ | if($self->has_pattern) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 1 | my $pattern = $self->pattern; | |||||||||
| 35 | 1 | ▶ | $pattern =~ s/\{\{([^}]+)\}\}/ defined($value->{$1}) ? $value->{$1}: '' /gex; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 1 | return $pattern; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 0 | die 'Flatten filter must define one of pick, join or sprintf'; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 1 | 1; | |||||||||
| 43 | |||||||||||
| 44 | =head1 NAME | ||||||||||
| 45 | |||||||||||
| 46 | Valiant::Filter::Flatten - Array or Hash ref to string | ||||||||||
| 47 | |||||||||||
| 48 | =head1 SYNOPSIS | ||||||||||
| 49 | |||||||||||
| 50 | package Local::Test; | ||||||||||
| 51 | |||||||||||
| 52 | use Moo; | ||||||||||
| 53 | use Valiant::Filters; | ||||||||||
| 54 | |||||||||||
| 55 | has 'pick_first' => (is=>'ro', required=>1); | ||||||||||
| 56 | has 'pick_last' => (is=>'ro', required=>1); | ||||||||||
| 57 | has 'join' => (is=>'ro', required=>1); | ||||||||||
| 58 | has 'sprintf' => (is=>'ro', required=>1); | ||||||||||
| 59 | has 'pattern' => (is=>'ro', required=>1); | ||||||||||
| 60 | |||||||||||
| 61 | filters pick_first => (flatten => +{pick => 'first'}); | ||||||||||
| 62 | filters pick_last => (flatten => +{pick => 'last'}); | ||||||||||
| 63 | filters join => (flatten => +{join => ','}); | ||||||||||
| 64 | filters sprintf => (flatten => +{sprintf => '%s-%s-%s'}); | ||||||||||
| 65 | filters pattern => (flatten => +{pattern => 'hi {{a}} there {{b}}'}); | ||||||||||
| 66 | |||||||||||
| 67 | my $object = Local::Test->new( | ||||||||||
| 68 | pick_first => [1,2,3], | ||||||||||
| 69 | pick_last => [1,2,3], | ||||||||||
| 70 | join => [1,2,3], | ||||||||||
| 71 | sprintf => [1,2,3], | ||||||||||
| 72 | pattern => +{ a=>'now', b=>'john' }, | ||||||||||
| 73 | ); | ||||||||||
| 74 | |||||||||||
| 75 | print $object->pick_first; # 1 | ||||||||||
| 76 | print $object->pick_last; # 3 | ||||||||||
| 77 | print $object->join; # '1,2,3' | ||||||||||
| 78 | print $object->sprintf; # '1-2-3' | ||||||||||
| 79 | print $object->pattern; # 'hi now there john' | ||||||||||
| 80 | |||||||||||
| 81 | =head1 DESCRIPTION | ||||||||||
| 82 | |||||||||||
| 83 | Given an arrayref for a value, flatten to a string in various ways | ||||||||||
| 84 | |||||||||||
| 85 | =head1 ATTRIBUTES | ||||||||||
| 86 | |||||||||||
| 87 | This filter defines the following attributes | ||||||||||
| 88 | |||||||||||
| 89 | =head2 pick | ||||||||||
| 90 | |||||||||||
| 91 | Value of either 'first' or 'last' which indicates choosing either the first or | ||||||||||
| 92 | last index of the arrayref. | ||||||||||
| 93 | |||||||||||
| 94 | =head2 join | ||||||||||
| 95 | |||||||||||
| 96 | Join the arrayref into a string using the value of 'join' as the deliminator | ||||||||||
| 97 | |||||||||||
| 98 | =head2 sprintf | ||||||||||
| 99 | |||||||||||
| 100 | Use C<sprintf> formatted string to convert an arrayref. | ||||||||||
| 101 | |||||||||||
| 102 | =head2 pattern | ||||||||||
| 103 | |||||||||||
| 104 | Uses a L<Valiant> style pattern string to convert a hashref to a string. Basically | ||||||||||
| 105 | inside the pattern string we look for "{{$key}}" and substitute $key for whatever | ||||||||||
| 106 | matches in the incoming hashref as a key value. If there's no match we replace with | ||||||||||
| 107 | an empty string without raising warnings. | ||||||||||
| 108 | |||||||||||
| 109 | =head1 SEE ALSO | ||||||||||
| 110 | |||||||||||
| 111 | L<Valiant>, L<Valiant::Filter>, L<Valiant::Validator::Filter>. | ||||||||||
| 112 | |||||||||||
| 113 | =head1 AUTHOR | ||||||||||
| 114 | |||||||||||
| 115 | See L<Valiant> | ||||||||||
| 116 | |||||||||||
| 117 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 118 | |||||||||||
| 119 | See L<Valiant> | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||