lib/Form/Sensible/Field/Select.pm

filter: Statementstmt 95.7% 90 / 94 Branchbran 95.0% 19 / 20 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 93.7% 15 / 16 Podpod 100% 5 / 5 totaltotal 94.4% 137 / 145 CC 16 SCAR 14.0
CC
16
Cov
95%
add_selection
8.0
__ANON__
6.0
__ANON__
3.0
CRAP 4.1
« lib/Form/Sensible/Renderer/HTML.pm ↑ Summary lib/Form/Sensible.pm »
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
BranchTrueFalseTotal
if ($value eq $option->{"value"}) 4 23 27
53 4 $valid = 1;
54 4 last;
55 }
56 }
57 5 ▶ if (!$valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid) 1 4 5
58 1 ▶ if (exists($caller->validation->{'invalid_message'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $caller->validation->{'invalid_message'}) { } 0 1 1
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
BranchTrueFalseTotal
unless ($self->accepts_multiple) 2 6 8
95 2 return $self->value(@_);
96 }
97  
98 6 ▶ if (!defined($self->value()) || ref($self->value()) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->value or ref $self->value ne "ARRAY") 1 5 6
Condition66%2/3
not defined $self->value or ref $self->value ne "ARRAY"
l!l&&r!l&&!r
105
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"
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($#_ == 0 and ref $_[0] eq "ARRAY") 1 5 6
Condition100%3/3
$#_ == 0 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
231
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"
ABresult
0X0
100
111
105 1 $providedoptions = $_[0];
106 }
107  
108 6 foreach my $item (@{$providedoptions}) {
109 10 ▶ if (!exists($selection{$item})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $selection{$item}) 7 3 10
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
BranchTrueFalseTotal
if (ref $self->value eq 'ARRAY') { } 1 2 3
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
BranchTrueFalseTotal
if ($#_ == -1) 48 6 54
156 48 return $self->$orig();
157 }
158  
159 6 my $values;
160 6 ▶ if (ref($_[0]) ne 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] ne 'ARRAY') { } 3 3 6
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__
« lib/Form/Sensible/Renderer/HTML.pm ↑ Summary lib/Form/Sensible.pm »