| 1 | package Form::Sensible::Field::Select; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use Form::Sensible::DelegateConnection; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 16 | extends 'Form::Sensible::Field'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | ## provides a select field - potentially with multiple selections | ||||||||||||||||||||||||||||
| 10 | ## this could be a dropdown box or a radio-select group | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | has 'options' => ( | ||||||||||||||||||||||||||||
| 15 | is => 'rw', | ||||||||||||||||||||||||||||
| 16 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 17 | required => 1, | ||||||||||||||||||||||||||||
| 18 | 2 | ▶ | default => sub { return [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 16 | lazy => 1, | |||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | has 'options_delegate' => ( | ||||||||||||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||||||||||
| 24 | isa => 'Form::Sensible::DelegateConnection', | ||||||||||||||||||||||||||||
| 25 | required => 1, | ||||||||||||||||||||||||||||
| 26 | 3 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | 3 | my $obj = $self; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 3 | ▶ | return FSConnector( sub { return $obj->options } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||||||||
| 32 | 16 | lazy => 1, | |||||||||||||||||||||||||||
| 33 | coerce => 1, | ||||||||||||||||||||||||||||
| 34 | # additional options | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | has 'values_ok_delegate' => ( | ||||||||||||||||||||||||||||
| 38 | is => 'rw', | ||||||||||||||||||||||||||||
| 39 | isa => 'Form::Sensible::DelegateConnection', | ||||||||||||||||||||||||||||
| 40 | required => 1, | ||||||||||||||||||||||||||||
| 41 | 2 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 43 | 2 | my $obj = $self; | |||||||||||||||||||||||||||
| 44 | 3 | ▶ | return FSConnector( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 45 | 3 | my $caller = shift; | |||||||||||||||||||||||||||
| 46 | 3 | my $values = shift; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | my @errors; | |||||||||||||||||||||||||||
| 49 | 3 | foreach my $value (@{$values}) { | |||||||||||||||||||||||||||
| 50 | 5 | my $valid = 0; | |||||||||||||||||||||||||||
| 51 | 5 | foreach my $option (@{$caller->get_options}) { | |||||||||||||||||||||||||||
| 52 | 27 | ▶ | if ($value eq $option->{'value'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 4 | $valid = 1; | |||||||||||||||||||||||||||
| 54 | 4 | last; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 5 | ▶ | if (!$valid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | if (exists($caller->validation->{'invalid_message'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | push @errors, $caller->validation->{'invalid_message'}; | |||||||||||||||||||||||||||
| 60 | } else { | ||||||||||||||||||||||||||||
| 61 | 1 | push @errors, "_FIELDNAME_ was set to an invalid value"; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 3 | return @errors; | |||||||||||||||||||||||||||
| 66 | 2 | }); | |||||||||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||||||||||
| 68 | 16 | lazy => 1, | |||||||||||||||||||||||||||
| 69 | coerce => 1, | ||||||||||||||||||||||||||||
| 70 | # additional options | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | ## Select has special handling, it's value is ALWAYS returned as an array, so we have to | ||||||||||||||||||||||||||||
| 74 | ## provide our own default 'value_delegate' to negate the default value_delegate behavior of | ||||||||||||||||||||||||||||
| 75 | ## eating arrayrefs when accepts_multiple is false. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub get_additional_configuration { | ||||||||||||||||||||||||||
|
Subroutine
get_additional_configuration: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | return { map { $_ => $self->$_ } qw/accepts_multiple options/ }; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | sub set_selection { | ||||||||||||||||||||||||||
|
Subroutine
set_selection: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 2 | my ($self) = shift; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | $self->value( [ ] ); | |||||||||||||||||||||||||||
| 88 | 2 | return $self->add_selection(@_); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 8 | ▶ | sub add_selection { | ||||||||||||||||||||||||||
|
Subroutine
add_selection: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 8 | my ($self) = shift; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 8 | ▶ | if (!$self->accepts_multiple) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 2 | return $self->value(@_); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 6 | ▶ | if (!defined($self->value()) || ref($self->value()) ne 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $self->value or ref $self->value ne "ARRAY"
MC/DC50%1/2
not defined $self->value or ref $self->value ne "ARRAY"
not defined $self->value
ref $self->value ne "ARRAY"
Truth table66%2/3
not(A) or B
A= not defined $self->valueB= ref $self->value ne "ARRAY"
| |||||||||||||||||||||||||||||
| 99 | 1 | $self->value( [ ] ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 6 | my %selection = map { $_ => 1 } @{$self->value()}; | |||||||||||||||||||||||||||
| 103 | 6 | my $providedoptions = [ @_ ]; | |||||||||||||||||||||||||||
| 104 | 6 | ▶ | if ($#_ == 0 && ref($_[0]) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$#_ == 0 and ref $_[0] eq "ARRAY"
MC/DC100%2/2
$#_ == 0 and ref $_[0] eq "ARRAY"
$#_ == 0
ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= $#_ == 0B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 105 | 1 | $providedoptions = $_[0]; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 6 | foreach my $item (@{$providedoptions}) { | |||||||||||||||||||||||||||
| 109 | 10 | ▶ | if (!exists($selection{$item})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 7 | push @{$self->value()}, $item; | |||||||||||||||||||||||||||
| 111 | 7 | $selection{$item} = 1; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 6 | return $self->value(); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 9 | ▶ | sub add_option { | ||||||||||||||||||||||||||
|
Subroutine
add_option: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 9 | my ($self, $value, $display_name) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 9 | push @{$self->options}, { name => $display_name, value => $value }; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 13 | ▶ | sub get_options { | ||||||||||||||||||||||||||
|
Subroutine
get_options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 13 | my ($self) = shift; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 13 | $self->options_delegate->($self, @_); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 3 | ▶ | around 'validate' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 130 | 3 | my $orig = shift; | |||||||||||||||||||||||||||
| 131 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 3 | my $values; | |||||||||||||||||||||||||||
| 134 | 3 | ▶ | if (ref($self->value) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 1 | $values = $self->value; | |||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||
| 137 | 2 | $values = [ $self->value ]; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 3 | my @errors; | |||||||||||||||||||||||||||
| 140 | 16 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 141 | #warn Dumper($values); | ||||||||||||||||||||||||||||
| 142 | #warn Dumper($self->options); | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 3 | push @errors, $self->$orig(@_); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 3 | push @errors, $self->values_ok_delegate->($self, $values); | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 3 | return @errors; | |||||||||||||||||||||||||||
| 149 | 16 | }; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 54 | ▶ | around 'value' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 152 | 54 | my $orig = shift; | |||||||||||||||||||||||||||
| 153 | 54 | my $self = shift; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 54 | ▶ | if ($#_ == -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 48 | return $self->$orig(); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 6 | my $values; | |||||||||||||||||||||||||||
| 160 | 6 | ▶ | if (ref($_[0]) ne 'ARRAY') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | { | ||||||||||||||||||||||||||||
| 162 | 3 | $values = [ ]; | |||||||||||||||||||||||||||
| 163 | 3 | push @{$values}, @_; | |||||||||||||||||||||||||||
| 164 | } else { | ||||||||||||||||||||||||||||
| 165 | 3 | $values = $_[0]; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 6 | return $self->$orig($values); | |||||||||||||||||||||||||||
| 168 | 16 | }; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 16 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 171 | 16 | 1; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | __END__ | ||||||||||||||||||||||||||||