File Coverage

blib/lib/Form/Factory/Control/SelectMany.pm
Criterion Covered Total %
statement 10 15 66.6
branch n/a
condition n/a
subroutine 4 9 44.4
pod 6 6 100.0
total 20 30 66.6


line stmt bran cond sub pod time code
1             package Form::Factory::Control::SelectMany;
2             $Form::Factory::Control::SelectMany::VERSION = '0.022';
3 1     1   474 use Moose;
  1         2  
  1         6  
4              
5             with qw(
6             Form::Factory::Control
7             Form::Factory::Control::Role::AvailableChoices
8             Form::Factory::Control::Role::Labeled
9             Form::Factory::Control::Role::ListValue
10             );
11              
12 1     1   4664 use List::MoreUtils qw( any );
  1         2  
  1         14  
13              
14             # ABSTRACT: the multi-select control
15              
16              
17             has '+value' => (
18             isa => 'ArrayRef[Str]',
19             );
20              
21             has '+default_value' => (
22             isa => 'ArrayRef[Str]',
23             default => sub { [] },
24             );
25              
26              
27 6     6 1 25 sub current_values { shift->current_value(@_) }
28              
29              
30 0     0 1 0 sub selected_choices { shift->value(@_) }
31              
32 0     0 1 0 sub has_selected_choices { shift->has_value(@_) }
33              
34              
35 0     0 1 0 sub default_selected_choices { shift->default_value(@_) }
36              
37 0     0 1 0 sub has_default_selected_choices { shift->has_default_value(@_) }
38              
39              
40             sub is_choice_selected {
41 5     5 1 8 my ($self, $choice) = @_;
42              
43 5     0   23 return any { $_ eq $choice->value } @{ $self->current_values };
  0         0  
  5         63  
44             }
45              
46              
47             around has_current_value => sub {
48             my $next = shift;
49             my $self = shift;
50             return ($self->has_value || $self->has_default_value)
51             && scalar(@{ $self->current_value }) > 0;
52             };
53              
54             __PACKAGE__->meta->make_immutable;
55              
56             __END__
57              
58             =pod
59              
60             =encoding UTF-8
61              
62             =head1 NAME
63              
64             Form::Factory::Control::SelectMany - the multi-select control
65              
66             =head1 VERSION
67              
68             version 0.022
69              
70             =head1 SYNOPSIS
71              
72             has_control pick_some => (
73             control => 'select_many',
74             options => {
75             label => 'Just select some of these already...",
76             available_choices => [
77             Form::Factory::Control::Choice->new('one');
78             Form::Factory::Control::Choice->new('two');
79             Form::Factory::Control::Choice->new('three');
80             ],
81             default_selected_choices => [ qw( one three ) ],
82             },
83             );
84              
85             =head1 DESCRIPTION
86              
87             A select many can be displayed as a multi-select list box or a list of checkboxes.
88              
89             This control implements L<Form::Factory::Control>, L<Form::Factory::Control::Role::AvailableChoices>, L<Form::Factory::Control::Role::Labeled>, L<Form::Factory::Control::Role::ListValue>.
90              
91             =head1 METHODS
92              
93             =head2 current_values
94              
95             This is a synonym for C<current_value>.
96              
97             =head2 selected_choices
98              
99             This is a synonym for C<value>.
100              
101             =head2 has_selected_choices
102              
103             This is a synonyms for C<has_selected_choices>.
104              
105             =head2 default_selected_choices
106              
107             This is a synonym for C<default_value>.
108              
109             =head2 has_default_selected_choices
110              
111             This is a synonym for C<has_default_selected_choices>.
112              
113             =head2 is_choice_selected
114              
115             for my $choice (@{ $self->available_choices }) {
116             if ($control->is_choice_selected($choice)) {
117             # ...
118             }
119             }
120              
121             This is a helper that is useful while iterating over the available choices in deciding which have been selected.
122              
123             =head2 has_current_value
124              
125             It has a current value if one or more values are selected.
126              
127             =head1 AUTHOR
128              
129             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
130              
131             =head1 COPYRIGHT AND LICENSE
132              
133             This software is copyright (c) 2015 by Qubling Software LLC.
134              
135             This is free software; you can redistribute it and/or modify it under
136             the same terms as the Perl 5 programming language system itself.
137              
138             =cut