lib/HTML/FormFu/Role/Constraint/Others.pm

filter: Statementstmt 96.5% 111 / 115 Branchbran 71.4% 50 / 70 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 87.6% 185 / 211 SCAR 38.4
CC
43
Cov
88%
mk_errors
32.0
__ANON__
9.4
pre_process
6.0
CRAP 46.5
« lib/HTML/FormFu/Role/Element/FieldMethods.pm ↑ Summary lib/HTML/FormFu/Role/FormAndElementMethods.pm »
1 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Role::Constraint::Others;
4 26 $HTML::FormFu::Role::Constraint::Others::VERSION = '2.06';
5 # ABSTRACT: Base class for constraints needing others() method
6  
7 26 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 26 use HTML::FormFu::Util qw(
10 DEBUG_CONSTRAINTS_OTHERS
11 debug
12 26 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 26 ▶use List::Util 1.33 qw( any none );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 26 has others => ( is => 'rw', traits => ['Chained'] );
17 26 has other_siblings => ( is => 'rw', traits => ['Chained'] );
18 26 has attach_errors_to => ( is => 'rw', traits => ['Chained'] );
19 26 has attach_errors_to_base => ( is => 'rw', traits => ['Chained'] );
20 26 has attach_errors_to_others => ( is => 'rw', traits => ['Chained'] );
21  
22 sub pre_process {
23 87 ▶ my ($self) = @_;
Subroutine pre_process: called CC=6 SCAR=18.0
Pod undocumented
24  
25 87 ▶ if ( $self->other_siblings ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->other_siblings) 11 76 87
26  
27 11 my $field = $self->field;
28 11 my $block = $field;
29  
30 # find the nearest parent that contains any field other than
31 # the one this constraint is attached to
32 11 ▶ while ( defined( my $parent = $block->parent ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $parent = $block->parent)) 11 0 11
33 11 $block = $parent;
34  
35 11 ▶ last if grep { $_ ne $field } @{ $block->get_fields };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ ne $field;} @{$block->get_fields;} 11 0 11
36 }
37  
38 11 my @names;
39  
40 11 for my $sibling ( @{ $block->get_fields } ) {
41 36 ▶ next if $sibling == $field;
Branch: 1 branch
BranchTrueFalseTotal
if $sibling == $field 11 25 36
42  
43 25 push @names, $sibling->nested_name;
44 }
45  
46 11 $self->others( [@names] );
47 }
48 }
49  
50 after repeatable_repeat => sub {
51 4 ▶ my ( $self, $repeatable, $new_block ) = @_;
Subroutine __ANON__: called CC=9 SCAR=22.4
52  
53 4 my $block_fields = $new_block->get_fields;
54  
55 # rename any 'others' fields
56 {
57 4 my $others = $self->others;
58 4 ▶ if ( !ref $others ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $others) 2 2 4
59 2 $others = [$others];
60 }
61 4 my @new_others;
62  
63 4 for my $name (@$others) {
64 6 my $field = $repeatable->get_field_with_original_name( $name,
65 $block_fields );
66  
67 6 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) { } 6 0 6
68 6 ▶ DEBUG_CONSTRAINTS_OTHERS && debug(
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug(sprintf("Repeatable renaming constraint 'other' '%s' to '%s'", $name, $field->nested_name)) 0 6 6
69 sprintf
70 "Repeatable renaming constraint 'other' '%s' to '%s'",
71 $name, $field->nested_name,
72 );
73  
74 6 push @new_others, $field->nested_name;
75 }
76 else {
77 0 push @new_others, $name;
78 }
79 }
80  
81 4 $self->others( \@new_others );
82 }
83  
84 # rename any 'attach_errors_to' fields
85 4 ▶ if ( my $others = $self->attach_errors_to ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $others = $self->attach_errors_to) 2 2 4
86 2 my @new_others;
87  
88 2 for my $name (@$others) {
89 4 my $field = $repeatable->get_field_with_original_name( $name,
90 $block_fields );
91  
92 4 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) { } 4 0 4
93 4 ▶ DEBUG_CONSTRAINTS_OTHERS && debug(
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug(sprintf("Repeatable renaming constraint 'attach_errors_to' '%s' to '%s'", $name, $field->nested_name)) 0 4 4
94 sprintf
95 "Repeatable renaming constraint 'attach_errors_to' '%s' to '%s'",
96 $name, $field->nested_name,
97 );
98  
99 4 push @new_others, $field->nested_name;
100 }
101 else {
102 0 push @new_others, $name;
103 }
104 }
105  
106 2 $self->attach_errors_to( \@new_others );
107 }
108 26 };
109  
110 sub mk_errors {
111 101 ▶ my ( $self, $args ) = @_;
Subroutine mk_errors: called CC=29 SCAR=34.6
Pod undocumented
112  
113 101 my $pass = $args->{pass};
114 101 ▶ my @failed = $args->{failed} ? @{ $args->{failed} } : ();
Branch: 1 branch
BranchTrueFalseTotal
$args->{'failed'} ? : 84 17 101
115 101 ▶ my @names = $args->{names} ? @{ $args->{names} } : ();
Branch: 1 branch
BranchTrueFalseTotal
$args->{'names'} ? : 84 17 101
116  
117 101 ▶ my $force = $self->force_errors || $self->parent->force_errors;
Condition100%3/3
$self->force_errors || $self->parent->force_errors
l!l&&r!l&&!r
12584
MC/DC100%2/2
$self->force_errors || $self->parent->force_errors
$self->force_errors $self->parent->force_errors
Truth table100%3/3
A || B
A= $self->force_errorsB= $self->parent->force_errors
ABresult
1X1
011
000
118  
119 101 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( PASS => $pass );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("PASS", $pass) 0 101 101
120 101 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( NAMES => \@names );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("NAMES", \@names) 0 101 101
121 101 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( 'FAILED NAMES' => \@failed );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("FAILED NAMES", \@failed) 0 101 101
122 101 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( FORCE => $force );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("FORCE", $force) 0 101 101
123  
124 101 ▶ if ( $pass && !$force ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass and not $force) 56 45 101
Condition100%3/3
$pass and not $force
!ll&&!rl&&r
331256
MC/DC100%2/2
$pass and not $force
$pass not $force
Truth table100%3/3
A and not(B)
A= $passB= not $force
ABresult
0X0
100
111
125 56 ▶ DEBUG_CONSTRAINTS_OTHERS
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("constraint passed, or force_errors is false - returning no errors") 0 56 56
126 && debug(
127 'constraint passed, or force_errors is false - returning no errors'
128 );
129 56 return;
130 }
131  
132 45 my @can_error;
133 my @has_error;
134  
135 45 ▶ if ( $self->attach_errors_to ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->attach_errors_to) { } 1 44 45
elsif ($self->attach_errors_to_base) { } 11 33 44
elsif ($self->attach_errors_to_others) { } 2 31 33
136 1 push @can_error, @{ $self->attach_errors_to };
137  
138 1 ▶ if ( !$pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 1 0 1
139 1 push @has_error, @{ $self->attach_errors_to };
140 }
141 }
142 elsif ( $self->attach_errors_to_base ) {
143 11 push @can_error, $self->nested_name;
144  
145 11 ▶ if ( !$pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 9 2 11
146 9 push @has_error, $self->nested_name;
147 }
148 }
149 elsif ( $self->attach_errors_to_others ) {
150 push @can_error, ref $self->others
151 2 ▶ ? @{ $self->others }
Branch: 1 branch
BranchTrueFalseTotal
ref $self->others ? : 0 2 2
152 : $self->others;
153  
154 2 ▶ if ( !$pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 2 0 2
155 push @has_error, ref $self->others
156 2 ▶ ? @{ $self->others }
Branch: 1 branch
BranchTrueFalseTotal
ref $self->others ? : 0 2 2
157 : $self->others;
158 }
159 }
160 else {
161 31 push @can_error, @names;
162  
163 31 ▶ if ( !$pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 21 10 31
164 21 push @has_error, @failed;
165 }
166 }
167  
168 45 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( 'CAN ERROR' => \@can_error );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("CAN ERROR", \@can_error) 0 45 45
169 45 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( 'HAS ERROR' => \@has_error );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("HAS ERROR", \@has_error) 0 45 45
170  
171 45 my @errors;
172  
173 45 for my $name (@can_error) {
174  
175 98 ▶ next unless $force || grep { $name eq $_ } @has_error;
Branch: 1 branch
BranchTrueFalseTotal
unless $force or grep {$name eq $_;} @has_error 30 68 98
Condition100%3/3
$force or grep {$name eq $_;} @has_error
l!l&&r!l&&!r
363230
MC/DC100%2/2
$force or grep {$name eq $_;} @has_error
$force grep {$name eq $_;} @has_error
Truth table100%3/3
A or B
A= $forceB= grep {$name eq $_;} @has_error
ABresult
1X1
011
000
176  
177 68 ▶ DEBUG_CONSTRAINTS_OTHERS && debug( 'CREATING ERROR' => $name );
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("CREATING ERROR", $name) 0 68 68
178  
179 68 ▶ my $field = $self->form->get_field( { nested_name => $name } )
Branch: 1 branch
BranchTrueFalseTotal
my $field = $self->form->get_field({"nested_name", $name}) or die "others() field not found: '${name}'" 0 68 68
180 or die "others() field not found: '$name'";
181  
182 68 my $error = $self->mk_error;
183  
184 68 $error->parent($field);
185  
186 68 ▶ if ( !grep { $name eq $_ } @has_error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$name eq $_;} @has_error) 31 37 68
187 31 ▶ DEBUG_CONSTRAINTS_OTHERS
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS_OTHERS() and debug("setting '${name}' error forced(1)") 0 31 31
188 && debug("setting '$name' error forced(1)");
189  
190 31 $error->forced(1);
191 }
192  
193 68 push @errors, $error;
194 }
195  
196 45 return @errors;
197 }
198  
199 around clone => sub {
200 4 ▶ my ( $orig, $self, $args ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
201  
202 4 my $clone = $self->$orig($args);
203  
204 4 ▶ if ( ref $self->others ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->others) 2 2 4
205 2 $clone->others( Clone::clone( $self->others ) );
206 }
207  
208 4 return $clone;
209 26 };
210  
211 26 1;
212  
213 __END__
214  
215 =pod
216  
217 =encoding UTF-8
218  
219 =head1 NAME
220  
221 HTML::FormFu::Role::Constraint::Others - Base class for constraints needing others() method
222  
223 =head1 VERSION
224  
225 version 2.06
226  
227 =head1 METHODS
228  
229 =head2 others
230  
231 Arguments: \@nested_names
232  
233 =head2 other_siblings
234  
235 Arguments: $bool
236  
237 If true, the L</others> list will be automatically generated from the
238 C<nested_name> of all fields which are considered siblings of the field the
239 constraint is attached to.
240  
241 Sibling are found by searching up through the field's parental hierarchy for
242 the first block containing any other field. All fields attached at any depth
243 to this block are considered siblings.
244  
245 =head2 attach_errors_to_base
246  
247 If true, any error will cause the error message to be associated with the
248 field the constraint is attached to.
249  
250 Can be use in conjunction with L</attach_errors_to_others>.
251  
252 Is ignored if L</attach_errors_to> is set.
253  
254 =head2 attach_errors_to_others
255  
256 If true, any error will cause the error message to be associated with every
257 field named in L</others>.
258  
259 Can be use in conjunction with L</attach_errors_to_base>.
260  
261 Is ignored if L</attach_errors_to> is set.
262  
263 =head2 attach_errors_to
264  
265 Arguments: \@field_names
266  
267 Any error will cause the error message to be associated with every field
268 named in L</attach_errors_to>.
269  
270 Overrides L</attach_errors_to_base> and L</attach_errors_to_others>.
271  
272 =head1 SEE ALSO
273  
274 Is a sub-class of, and inherits methods from L<HTML::FormFu::Constraint>
275  
276 L<HTML::FormFu>
277  
278 =head1 AUTHOR
279  
280 Carl Franks C<cfranks@cpan.org>
281  
282 =head1 LICENSE
283  
284 This library is free software, you can redistribute it and/or modify it under
285 the same terms as Perl itself.
286  
287 =head1 AUTHOR
288  
289 Carl Franks <cpan@fireartist.com>
290  
291 =head1 COPYRIGHT AND LICENSE
292  
293 This software is copyright (c) 2018 by Carl Franks.
294  
295 This is free software; you can redistribute it and/or modify it under
296 the same terms as the Perl 5 programming language system itself.
297  
298 =cut
« lib/HTML/FormFu/Role/Element/FieldMethods.pm ↑ Summary lib/HTML/FormFu/Role/FormAndElementMethods.pm »