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