File Coverage

blib/lib/Form/Factory/Result/Gathered.pm
Criterion Covered Total %
statement 49 55 89.0
branch 6 6 100.0
condition n/a
subroutine 18 21 85.7
pod 13 13 100.0
total 86 95 90.5


line stmt bran cond sub pod time code
1             package Form::Factory::Result::Gathered;
2             $Form::Factory::Result::Gathered::VERSION = '0.022';
3 1     1   5 use Moose;
  1         1  
  1         6  
4              
5 1     1   4742 use Carp ();
  1         2  
  1         17  
6 1     1   3 use Scalar::Util qw( blessed refaddr );
  1         1  
  1         56  
7 1     1   4 use List::MoreUtils qw( all any );
  1         2  
  1         7  
8              
9             with qw( Form::Factory::Result );
10              
11             # ABSTRACT: A group of results
12              
13              
14             has _results => (
15             is => 'ro',
16             isa => 'HashRef[Form::Factory::Result]',
17             required => 1,
18             default => sub { {} },
19             );
20              
21              
22             sub results {
23 150     150 1 165 my $self = shift;
24 150         133 return values %{ $self->_results };
  150         4703  
25             }
26              
27              
28             sub gather_results {
29 48     48 1 143 my ($self, @results) = @_;
30              
31 48         1742 my $results = $self->_results;
32 48         115 for my $result (@results) {
33 223 100       565 Carp::croak('you attempted to pass a result itself to gather_results(), but you cannot gather results recursively')
34             if refaddr $result == refaddr $self;
35              
36 222         302 my $addr = refaddr $result;
37 222         588 $results->{$addr} = $result;
38             }
39             }
40              
41              
42             sub clear_state {
43 28     28 1 38 my $self = shift;
44 28         82 $_->clear_state for $self->results;
45             }
46              
47              
48             sub clear_results {
49 28     28 1 37 my $self = shift;
50 28         30 %{ $self->_results } = ();
  28         800  
51             }
52              
53              
54             sub clear_messages {
55 28     28 1 41 my $self = shift;
56 28         79 $_->clear_messages for $self->results;
57             }
58              
59              
60             sub clear_messages_for_field {
61 0     0 1 0 my $self = shift;
62 0         0 my $field = shift;
63              
64 0         0 $_->clear_messages_for_field($field) for $self->results;
65             }
66              
67              
68             sub clear_all {
69 28     28 1 40 my $self = shift;
70 28         95 $self->clear_state;
71 28         113 $self->clear_messages;
72 28         87 $self->clear_results;
73             }
74              
75              
76             sub is_valid {
77 42     42 1 397 my $self = shift;
78 42 100   155   266 return all { not $_->is_validated or $_->is_valid } $self->results;
  155         5123  
79             }
80              
81              
82             sub is_validated {
83 6     6 1 23 my $self = shift;
84 6     9   25 return any { $_->is_validated } $self->results;
  9         274  
85             }
86              
87              
88             sub is_success {
89 2     2 1 20 my $self = shift;
90 2 100   14   11 return all { not $_->is_outcome_known or $_->is_success } $self->results;
  14         404  
91             }
92              
93              
94             sub is_outcome_known {
95 0     0 1 0 my $self = shift;
96 0     0   0 return any { $_->is_outcome_known } $self->results;
  0         0  
97             }
98              
99              
100             sub messages {
101 23     23 1 41 my $self = shift;
102 23         59 return [ map { @{ $_->messages } } $self->results ];
  96         81  
  96         2751  
103             }
104              
105              
106             # Dumb merge
107             sub content {
108 21     21 1 284 my $self = shift;
109 21         46 return { map { %{ $_->content } } $self->results };
  26         24  
  26         792  
110             }
111              
112             __PACKAGE__->meta->make_immutable;
113              
114             __END__
115              
116             =pod
117              
118             =encoding UTF-8
119              
120             =head1 NAME
121              
122             Form::Factory::Result::Gathered - A group of results
123              
124             =head1 VERSION
125              
126             version 0.022
127              
128             =head1 SYNOPSIS
129              
130             my $result = Form::Factory::Result::Gathered->new;
131             $result->gather_results($other_result1, $other_result2, $other_result3);
132              
133             my @child_results = $result->results;
134              
135             $result->clear_messages;
136             $result->clear_messages_for_field('foo');
137             $result->clear_results;
138             $result->clear_all;
139              
140             my $validated = $result->is_validated;
141             my $valid = $result->is_valid;
142              
143             my $has_outcome = $result->is_outcome_known;
144             my $success = $result->is_success;
145              
146             my $messages = $result->messages;
147             my $content = $result->content;
148              
149             =head1 DESCRIPTION
150              
151             This is a collection of results. The results are grouped and collected together in a way that makes sense to the Form::Factory API.
152              
153             =head1 METHODS
154              
155             =head2 results
156              
157             my @results = $self->results;
158              
159             Returns a list of the results that have been gathered.
160              
161             =head2 gather_results
162              
163             $result->gather_results(@results);
164              
165             Given one or more result objects, it adds them to the list of results already gathered. These are placed in a set such that no result is added more than once. If a result object was already added, it will not be added again.
166              
167             =head2 clear_state
168              
169             Clears the state of all gathered results. It just calls C<clear_state> recursively on all results.
170              
171             =head2 clear_results
172              
173             Clears the list of results. L</results> will return an empty list after this is called.
174              
175             =head2 clear_messages
176              
177             Calls the C<clear_messages> method on all results that have been gathered. This will clear messages for all the associated results.
178              
179             =head2 clear_messages_for_field
180              
181             $result->clear_messagesw_for_field($field);
182              
183             Calls the C<clear_messages_for_field> method on all results that have been gathered.
184              
185             =head2 clear_all
186              
187             Clears all messages on the gathered results (via L</clear_message>) and then clears all the results (via L</clear_results>).
188              
189             =head2 is_valid
190              
191             Tests each result for validity. This will return true if every result returns false for C<is_validated> or returns true for C<is_valid>.
192              
193             =head2 is_validated
194              
195             Returns true if any result returns true for C<is_validated>.
196              
197             =head2 is_success
198              
199             Tests each result for success. This will return true if every result returns false for C<is_outcome_known> or true for C<is_success>.
200              
201             =head2 is_outcome_known
202              
203             Returns true if any result returns true for C<is_outcome_known>.
204              
205             =head2 messages
206              
207             Returns a reference to an array of messages. This includes all messages from the gathered results.
208              
209             =head2 content
210              
211             Performs a shallow merge of all the return value of each result's C<content> method and returns that.
212              
213             =head1 AUTHOR
214              
215             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
216              
217             =head1 COPYRIGHT AND LICENSE
218              
219             This software is copyright (c) 2015 by Qubling Software LLC.
220              
221             This is free software; you can redistribute it and/or modify it under
222             the same terms as the Perl 5 programming language system itself.
223              
224             =cut