lib/HTML/FormFu/Deflator/CompoundSplit.pm

filter: Statementstmt 100% 43 / 43 Branchbran 91.6% 11 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 93.9% 62 / 66 SCAR 24.0
CC
11
Cov
97%
deflator
11.0
BEGIN
1.0
BEGIN
1.0
CRAP 11.0
« lib/HTML/FormFu/Constraint/Email.pm ↑ Summary lib/HTML/FormFu/Role/CustomRoles.pm »
1 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Deflator::CompoundSplit;
4 7 $HTML::FormFu::Deflator::CompoundSplit::VERSION = '2.06';
5 # ABSTRACT: CompoundSplit deflator
6  
7 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 extends 'HTML::FormFu::Deflator';
10  
11 7 ▶use HTML::FormFu::Constants qw( $EMPTY_STR $SPACE );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 has split => ( is => 'rw', traits => ['Chained'] );
14 7 has join => ( is => 'rw', traits => ['Chained'] );
15 7 has field_order => ( is => 'rw', traits => ['Chained'] );
16  
17 sub deflator {
18 11 ▶ my ( $self, $value ) = @_;
Subroutine deflator: called CC=11 SCAR=24.0
Pod undocumented
19  
20 11 ▶ return if !defined $value || $value eq $EMPTY_STR;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq $HTML::FormFu::Deflator::CompoundSplit::EMPTY_STR 1 10 11
Condition66%2/3
not defined $value or $value eq $HTML::FormFu::Deflator::CompoundSplit::EMPTY_STR
l!l&&r!l&&!r
1010
MC/DC50%1/2
not defined $value or $value eq $HTML::FormFu::Deflator::CompoundSplit::EMPTY_STR
not defined $value $value eq $HTML::FormFu::Deflator::CompoundSplit::EMPTY_STR
Truth table66%2/3
not(A) or B
A= not defined $valueB= $value eq $HTML::FormFu::Deflator::CompoundSplit::EMPTY_STR
ABresult
1X1
011
000
21  
22 10 my ( $multi, @fields ) = @{ $self->parent->get_fields };
23  
24 10 ▶ my $split
Branch: 1 branch
BranchTrueFalseTotal
defined $self->split ? : 1 9 10
25 = defined $self->split
26 ? $self->split
27 : qr/$SPACE+/;
28  
29 10 ▶ my $join
Branch: 1 branch
BranchTrueFalseTotal
defined $self->join ? : 0 10 10
30 = defined $self->join
31 ? $self->join
32 : $SPACE;
33  
34 10 my @parts = CORE::split $split, $value;
35  
36 10 ▶ if ( my $order = $self->field_order ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $order = $self->field_order) 1 9 10
37 1 my @new_order;
38  
39 FIELD:
40 1 for my $i (@$order) {
41 2 for my $field (@fields) {
42 3 ▶ if ( $field->name eq $i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->name eq $i) 2 1 3
43 2 push @new_order, $field;
44 2 next FIELD;
45 }
46 }
47 }
48  
49 1 @fields = @new_order;
50 }
51  
52 10 my %value;
53  
54 10 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 ▶ if ( $i == $#fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#fields) { } 10 11 21
60 10 my $default = CORE::join $join, @parts[ $i .. $#parts ];
61  
62 10 $fields[$i]->default($default);
63  
64 10 $value{ $fields[$i]->name } = $default;
65 }
66 else {
67 11 $fields[$i]->default( $parts[$i] );
68  
69 11 $value{ $fields[$i]->name } = $parts[$i];
70 }
71 }
72  
73 10 return \%value;
74 }
75  
76 7 __PACKAGE__->meta->make_immutable;
77  
78 7 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.06
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 spliting 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
« lib/HTML/FormFu/Constraint/Email.pm ↑ Summary lib/HTML/FormFu/Role/CustomRoles.pm »