File Coverage

blib/lib/HTML/FormFu/Deflator/CompoundSplit.pm
Criterion Covered Total %
statement 36 36 100.0
branch 11 12 91.6
condition 2 3 66.6
subroutine 5 5 100.0
pod 0 1 0.0
total 54 57 94.7


line stmt bran cond sub pod time code
1 7     7   875 use strict;
  7         21  
  7         463  
2              
3             package HTML::FormFu::Deflator::CompoundSplit;
4             $HTML::FormFu::Deflator::CompoundSplit::VERSION = '2.07';
5             # ABSTRACT: CompoundSplit deflator
6              
7 7     7   54 use Moose;
  7         17  
  7         54  
8 7     7   49342 use MooseX::Attribute::Chained;
  7         26  
  7         316  
9             extends 'HTML::FormFu::Deflator';
10              
11 7     7   51 use HTML::FormFu::Constants qw( $EMPTY_STR $SPACE );
  7         19  
  7         3843  
12              
13             has split => ( is => 'rw', traits => ['Chained'] );
14             has join => ( is => 'rw', traits => ['Chained'] );
15             has field_order => ( is => 'rw', traits => ['Chained'] );
16              
17             sub deflator {
18 11     11 0 37 my ( $self, $value ) = @_;
19              
20 11 100 66     97 return if !defined $value || $value eq $EMPTY_STR;
21              
22 10         92 my ( $multi, @fields ) = @{ $self->parent->get_fields };
  10         42  
23              
24 10 100       342 my $split
25             = defined $self->split
26             ? $self->split
27             : qr/$SPACE+/;
28              
29 10 50       506 my $join
30             = defined $self->join
31             ? $self->join
32             : $SPACE;
33              
34 10         147 my @parts = CORE::split $split, $value;
35              
36 10 100       382 if ( my $order = $self->field_order ) {
37 1         2 my @new_order;
38              
39             FIELD:
40 1         2 for my $i (@$order) {
41 2         3 for my $field (@fields) {
42 3 100       9 if ( $field->name eq $i ) {
43 2         3 push @new_order, $field;
44 2         6 next FIELD;
45             }
46             }
47             }
48              
49 1         4 @fields = @new_order;
50             }
51              
52 10         23 my %value;
53              
54 10         44 for my $i ( 0 .. $#fields ) {
55              
56             # if there are more parts than fields, join the extra ones
57             # to make the final field's value
58              
59 21 100       70 if ( $i == $#fields ) {
60 10         51 my $default = CORE::join $join, @parts[ $i .. $#parts ];
61              
62 10         55 $fields[$i]->default($default);
63              
64 10         34 $value{ $fields[$i]->name } = $default;
65             }
66             else {
67 11         56 $fields[$i]->default( $parts[$i] );
68              
69 11         42 $value{ $fields[$i]->name } = $parts[$i];
70             }
71             }
72              
73 10         64 return \%value;
74             }
75              
76             __PACKAGE__->meta->make_immutable;
77              
78             1;
79              
80             __END__
81              
82             =pod
83              
84             =encoding UTF-8
85              
86             =head1 NAME
87              
88             HTML::FormFu::Deflator::CompoundSplit - CompoundSplit deflator
89              
90             =head1 VERSION
91              
92             version 2.07
93              
94             =head1 SYNOPSIS
95              
96             ---
97             element:
98             - type: Multi
99             name: address
100              
101             elements:
102             - name: number
103             - name: street
104              
105             deflator:
106             - type: CompoundSplit
107              
108             # set the default
109              
110             $form->get_field('address')->default( $address );
111              
112             =head1 DESCRIPTION
113              
114             Deflator to allow you to set several field's values at once.
115              
116             For use with a L<HTML::FormFu::Element::Multi> group of fields.
117              
118             A L<default|HTML::FormFu::Role::Element::Field/default> value passed to the
119             L<Multi|HTML::FormFu::Element::Multi> field will be split according to the
120             L</split> setting, and its resulting parts passed to its child elements.
121              
122             =head1 METHODS
123              
124             =head2 split
125              
126             Arguments: $regex
127              
128             Default Value: C<qr/ +/>
129              
130             Regex used to split the default value. Defaults to a regex matching 1 or more
131             space characters.
132              
133             =head2 join
134              
135             Arguments: $string
136              
137             Default Value: C<' '>
138              
139             If splitting the value results in more parts than there are fields, any extra
140             parts are joined again to form the value for the last field. The value of
141             L</join> is used to join these values.
142              
143             Defaults to a single space.
144              
145             For example, if the Multi element contains fields C<number> and C<name>,
146             and is given the value C<10 Downing Street>; when split this results in 3
147             parts: C<10>, C<Downing> and C<Street>. In this case, the 1st part, C<10> is
148             assigned to the first field, and the 2nd and 3rd parts are re-joined with a
149             space to give the single value C<Downing Street>, which is assigned to the
150             2nd field.
151              
152             =head2 field_order
153              
154             Arguments: \@order
155              
156             If the parts from the split value should be assigned to the fields in a
157             different order, you must provide an arrayref containing the names, in the
158             order they should be assigned to.
159              
160             ---
161             element:
162             - type: Multi
163             name: address
164              
165             elements:
166             - name: street
167             - name: number
168              
169             deflator:
170             - type: CompoundSplit
171             field_order:
172             - number
173             - street
174              
175             =head1 AUTHOR
176              
177             Carl Franks
178              
179             =head1 LICENSE
180              
181             This library is free software, you can redistribute it and/or modify it under
182             the same terms as Perl itself.
183              
184             =head1 AUTHOR
185              
186             Carl Franks <cpan@fireartist.com>
187              
188             =head1 COPYRIGHT AND LICENSE
189              
190             This software is copyright (c) 2018 by Carl Franks.
191              
192             This is free software; you can redistribute it and/or modify it under
193             the same terms as the Perl 5 programming language system itself.
194              
195             =cut