| 1 | 32 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::Role::Constraint::Others; | ||||||||||||||||||||||||||||
| 4 | 32 | $HTML::FormFu::Role::Constraint::Others::VERSION = '2.09'; | |||||||||||||||||||||||||||
| 5 | # ABSTRACT: Base class for constraints needing others() method | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 32 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 32 | ▶ | use HTML::FormFu::Util qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | DEBUG_CONSTRAINTS_OTHERS | ||||||||||||||||||||||||||||
| 11 | debug | ||||||||||||||||||||||||||||
| 12 | 32 | ); | |||||||||||||||||||||||||||
| 13 | 32 | ▶ | use Clone (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 32 | ▶ | use List::Util 1.33 qw( any none ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 32 | has others => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||
| 17 | 32 | has other_siblings => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||
| 18 | 32 | has attach_errors_to => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||
| 19 | 32 | has attach_errors_to_base => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||
| 20 | 32 | has attach_errors_to_others => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 87 | ▶ | sub pre_process { | ||||||||||||||||||||||||||
|
Subroutine
pre_process: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 87 | my ($self) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 87 | ▶ | if ( $self->other_siblings ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 33 | 11 | $block = $parent; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 11 | ▶ | last if grep { $_ ne $field } @{ $block->get_fields }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 11 | my @names; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 11 | for my $sibling ( @{ $block->get_fields } ) { | |||||||||||||||||||||||||||
| 41 | 36 | ▶ | next if $sibling == $field; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 25 | push @names, $sibling->nested_name; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 11 | $self->others( [@names] ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 4 | ▶ | after repeatable_repeat => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.4
| |||||||||||||||||||||||||||||
| 51 | 4 | my ( $self, $repeatable, $new_block ) = @_; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | 6 | ▶ | DEBUG_CONSTRAINTS_OTHERS && debug( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 93 | 4 | ▶ | DEBUG_CONSTRAINTS_OTHERS && debug( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 32 | }; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 101 | ▶ | sub mk_errors { | ||||||||||||||||||||||||||
|
Subroutine
mk_errors: called CC=29 SCAR=34.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 101 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 101 | my $pass = $args->{pass}; | |||||||||||||||||||||||||||
| 114 | 101 | ▶ | my @failed = $args->{failed} ? @{ $args->{failed} } : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 101 | ▶ | my @names = $args->{names} ? @{ $args->{names} } : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 101 | ▶ | my $force = $self->force_errors || $self->parent->force_errors; | ||||||||||||||||||||||||||
|
Condition100%3/3
$self->force_errors || $self->parent->force_errors
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
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 101 | ▶ | DEBUG_CONSTRAINTS_OTHERS && debug( PASS => $pass ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 101 | ▶ | DEBUG_CONSTRAINTS_OTHERS && debug( NAMES => \@names ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 101 | ▶ | DEBUG_CONSTRAINTS_OTHERS && debug( 'FAILED NAMES' => \@failed ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 101 | ▶ | DEBUG_CONSTRAINTS_OTHERS && debug( FORCE => $force ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 101 | ▶ | if ( $pass && !$force ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$pass and not $force
MC/DC100%2/2
$pass and not $force
$pass
not $force
Truth table100%3/3
A and not(B)
A= $passB= not $force
| |||||||||||||||||||||||||||||
| 125 | 56 | ▶ | DEBUG_CONSTRAINTS_OTHERS | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 136 | 1 | push @can_error, @{ $self->attach_errors_to }; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | ▶ | if ( !$pass ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | ? @{ $self->others } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | : $self->others; | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 2 | ▶ | if ( !$pass ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | push @has_error, ref $self->others | ||||||||||||||||||||||||||||
| 156 | 0 | ▶ | ? @{ $self->others } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | : $self->others; | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||
| 161 | 31 | push @can_error, @names; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 31 | ▶ | if ( !$pass ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 21 | push @has_error, @failed; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 45 | ▶ | DEBUG_CONSTRAINTS_OTHERS && debug( 'CAN ERROR' => \@can_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 45 | ▶ | DEBUG_CONSTRAINTS_OTHERS && debug( 'HAS ERROR' => \@has_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$force or grep {$name eq $_;} @has_error
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
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 68 | ▶ | DEBUG_CONSTRAINTS_OTHERS && debug( 'CREATING ERROR' => $name ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 68 | ▶ | my $field = $self->form->get_field( { nested_name => $name } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 187 | 31 | ▶ | DEBUG_CONSTRAINTS_OTHERS | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 4 | ▶ | around clone => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 200 | 4 | my ( $orig, $self, $args ) = @_; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 4 | my $clone = $self->$orig($args); | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 4 | ▶ | if ( ref $self->others ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 2 | $clone->others( Clone::clone( $self->others ) ); | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 4 | return $clone; | |||||||||||||||||||||||||||
| 209 | 32 | }; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 32 | 1; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | __END__ | ||||||||||||||||||||||||||||