| 1 | package HTML::FormHandler::Result::Role; | ||||||||||
| 2 | # ABSTRACT: role with common code for form & field results | ||||||||||
| 3 | 149 | $HTML::FormHandler::Result::Role::VERSION = '0.410002'; | |||||||||
| 4 | 149 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | |||||||||||
| 7 | 149 | has 'name' => ( isa => 'Str', is => 'rw', required => 1 ); | |||||||||
| 8 | |||||||||||
| 9 | # do we need 'accessor' ? | ||||||||||
| 10 | 149 | has 'parent' => ( is => 'rw', weak_ref => 1 ); | |||||||||
| 11 | |||||||||||
| 12 | 149 | has 'input' => ( | |||||||||
| 13 | is => 'ro', | ||||||||||
| 14 | clearer => '_clear_input', | ||||||||||
| 15 | writer => '_set_input', | ||||||||||
| 16 | predicate => 'has_input', | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | has '_results' => ( | ||||||||||
| 20 | traits => ['Array'], | ||||||||||
| 21 | isa => 'ArrayRef[HTML::FormHandler::Field::Result]', | ||||||||||
| 22 | is => 'rw', | ||||||||||
| 23 | 3311 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 149 | handles => { | |||||||||
| 25 | results => 'elements', | ||||||||||
| 26 | add_result => 'push', | ||||||||||
| 27 | num_results => 'count', | ||||||||||
| 28 | has_results => 'count', | ||||||||||
| 29 | clear_results => 'clear', | ||||||||||
| 30 | find_result_index => 'first_index', | ||||||||||
| 31 | set_result_at_index => 'set', | ||||||||||
| 32 | _pop_result => 'pop', | ||||||||||
| 33 | } | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | has 'error_results' => ( | ||||||||||
| 37 | traits => ['Array'], | ||||||||||
| 38 | isa => 'ArrayRef', # for HFH::Result and HFH::Field::Result | ||||||||||
| 39 | is => 'rw', | ||||||||||
| 40 | 3311 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 149 | handles => { | |||||||||
| 42 | has_error_results => 'count', | ||||||||||
| 43 | num_error_results => 'count', | ||||||||||
| 44 | clear_error_results => 'clear', | ||||||||||
| 45 | add_error_result => 'push', | ||||||||||
| 46 | all_error_results => 'elements', | ||||||||||
| 47 | } | ||||||||||
| 48 | ); | ||||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | sub errors_by_id { | ||||||||
|
Subroutine
errors_by_id: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 51 | 0 | my $self = shift; | |||||||||
| 52 | 0 | my %errors; | |||||||||
| 53 | 0 | $errors{$_->field_def->id} = [$_->all_errors] for $self->all_error_results; | |||||||||
| 54 | 0 | return \%errors; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 1 | ▶ | sub errors_by_name { | ||||||||
|
Subroutine
errors_by_name: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 58 | 1 | my $self = shift; | |||||||||
| 59 | 1 | my %errors; | |||||||||
| 60 | 1 | $errors{$_->field_def->html_name} = [$_->all_errors] for $self->all_error_results; | |||||||||
| 61 | 1 | return \%errors; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | has 'errors' => ( | ||||||||||
| 65 | traits => ['Array'], | ||||||||||
| 66 | is => 'rw', | ||||||||||
| 67 | isa => 'ArrayRef[Str]', | ||||||||||
| 68 | 3311 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 69 | 149 | handles => { | |||||||||
| 70 | all_errors => 'elements', | ||||||||||
| 71 | _push_errors => 'push', | ||||||||||
| 72 | num_errors => 'count', | ||||||||||
| 73 | has_errors => 'count', | ||||||||||
| 74 | clear_errors => 'clear', | ||||||||||
| 75 | } | ||||||||||
| 76 | ); | ||||||||||
| 77 | |||||||||||
| 78 | has 'warnings' => ( | ||||||||||
| 79 | traits => ['Array'], | ||||||||||
| 80 | is => 'rw', | ||||||||||
| 81 | isa => 'ArrayRef[Str]', | ||||||||||
| 82 | 3311 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 83 | 149 | handles => { | |||||||||
| 84 | all_warnings => 'elements', | ||||||||||
| 85 | add_warning => 'push', | ||||||||||
| 86 | num_warnings => 'count', | ||||||||||
| 87 | has_warnings => 'count', | ||||||||||
| 88 | clear_warnings => 'clear', | ||||||||||
| 89 | } | ||||||||||
| 90 | ); | ||||||||||
| 91 | |||||||||||
| 92 | 9 | ▶ | sub validated { !$_[0]->has_error_results && $_[0]->has_input } | ||||||||
|
Subroutine
validated: called CC=2 SCAR=7.6
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 93 | 0 | ▶ | sub is_valid { shift->validated } | ||||||||
|
Subroutine
is_valid: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 94 | |||||||||||
| 95 | # this ought to be named 'result' for consistency, | ||||||||||
| 96 | # but the result objects are named 'result'. | ||||||||||
| 97 | # also providing 'field' method for compatibility | ||||||||||
| 98 | 251 | ▶ | sub get_result { | ||||||||
|
Subroutine
get_result: called CC=7 SCAR=20.4
Pod
undocumented
| |||||||||||
| 99 | 251 | my ( $self, $name, $die ) = @_; | |||||||||
| 100 | |||||||||||
| 101 | 251 | my $index; | |||||||||
| 102 | # if this is a full_name for a compound field | ||||||||||
| 103 | # walk through the fields to get to it | ||||||||||
| 104 | 251 | ▶ | if ( $name =~ /\./ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 5 | my @names = split /\./, $name; | |||||||||
| 106 | 5 | my $result = $self; | |||||||||
| 107 | 5 | foreach my $rname (@names) { | |||||||||
| 108 | 10 | $result = $result->get_result($rname); | |||||||||
| 109 | 10 | ▶ | return unless $result | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | } | ||||||||||
| 111 | 5 | return $result; | |||||||||
| 112 | } | ||||||||||
| 113 | else # not a compound name | ||||||||||
| 114 | { | ||||||||||
| 115 | 246 | for my $result ( $self->results ) { | |||||||||
| 116 | 588 | ▶ | return $result if ( $result->name eq $name ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | } | ||||||||||
| 118 | } | ||||||||||
| 119 | 0 | ▶ | return unless $die; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | die "Field '$name' not found in '$self'"; | |||||||||
| 121 | } | ||||||||||
| 122 | 202 | ▶ | sub field { shift->get_result(@_) } | ||||||||
|
Subroutine
field: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 123 | |||||||||||
| 124 | 149 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 125 | 149 | 1; | |||||||||
| 126 | |||||||||||
| 127 | __END__ | ||||||||||