File Coverage

blib/lib/Data/Verifier/Results.pm
Criterion Covered Total %
statement 71 71 100.0
branch 28 30 93.3
condition 3 3 100.0
subroutine 19 19 100.0
pod 17 17 100.0
total 138 140 98.5


line stmt bran cond sub pod time code
1             package Data::Verifier::Results;
2             $Data::Verifier::Results::VERSION = '0.61';
3 17     17   55 use Moose;
  17         20  
  17         94  
4 17     17   68824 use MooseX::Storage;
  17         21  
  17         106  
5              
6             with 'MooseX::Storage::Deferred';
7              
8             # ABSTRACT: Results of a Data::Verifier verify
9              
10              
11             has 'fields' => (
12             is => 'rw',
13             isa => 'HashRef',
14             traits => [ 'Hash' ],
15             default => sub { {} },
16             handles => {
17             get_field => 'get',
18             set_field => 'set',
19             has_field => 'exists'
20             }
21             );
22              
23              
24             sub get_original_value {
25 14     14 1 28 my ($self, $key) = @_;
26              
27 14         540 my $f = $self->get_field($key);
28 14 50       30 return undef unless defined($f);
29 14         466 return $f->original_value;
30             }
31              
32              
33             sub get_post_filter_value {
34 46     46 1 570 my ($self, $key) = @_;
35              
36 46         1278 my $f = $self->get_field($key);
37 46 50       90 return undef unless defined($f);
38 46         1038 return $f->post_filter_value;
39             }
40              
41              
42             sub get_value {
43 134     134 1 813 my ($self, $key) = @_;
44              
45 134         4131 my $f = $self->get_field($key);
46 134 100       216 return undef unless defined($f);
47 132         2868 return $f->value;
48             }
49              
50              
51             sub get_values {
52 1     1 1 2 my ($self, @keys) = @_;
53              
54 1         3 return map { $self->get_value($_) } @keys;
  2         3  
55             }
56              
57              
58             sub is_invalid {
59 27     27 1 42 my ($self, $field) = @_;
60              
61 27         1220 my $f = $self->get_field($field);
62              
63 27 100       56 return 0 unless defined($f);
64 24 100       548 return $f->valid ? 0 : 1;
65             }
66              
67              
68             sub is_missing {
69 17     17 1 29 my ($self, $field) = @_;
70              
71 17 100       591 return 0 unless $self->has_field($field);
72              
73 15         444 my $f = $self->get_field($field);
74              
75 15 100       47 return 1 unless defined($f);
76 8         21 return 0;
77             }
78              
79              
80             sub is_valid {
81 20     20 1 39 my ($self, $field) = @_;
82              
83 20         744 my $f = $self->get_field($field);
84              
85 20 100       49 return 0 unless defined($f);
86 15 100       363 return $f->valid ? 1 : 0;
87             }
88              
89              
90             sub is_wrong {
91 4     4 1 7 my ($self, $field) = @_;
92              
93             # return true if it is missing
94 4 100       6 return 1 if $self->is_missing($field);
95             # return 0 if it's not present at all
96 3 100       82 return 0 if !defined($self->get_field($field));
97             # lastly, check that it's valid
98 2 100       4 return 1 if $self->is_invalid($field);
99              
100             # Nope, must be fine
101 1         3 return 0;
102             }
103              
104              
105             sub merge {
106 9     9 1 14 my ($self, $other) = @_;
107              
108 9         8 foreach my $f (keys %{ $other->fields }) {
  9         193  
109 11         301 $self->set_field($f, $other->get_field($f));
110             }
111             }
112              
113              
114             sub invalid_count {
115 115     115 1 114 my ($self) = @_;
116              
117 115         144 return scalar($self->invalids);
118             }
119              
120              
121             sub invalids {
122 115     115 1 94 my ($self) = @_;
123              
124             return grep(
125 171 100       4598 { my $field = $self->get_field($_); defined($field) && !$field->valid; }
  171         3725  
126 115         70 keys %{ $self->fields }
  115         2911  
127             );
128             }
129              
130              
131             sub missing_count {
132 121     121 1 95 my ($self) = @_;
133              
134 121         166 return scalar($self->missings);
135             }
136              
137              
138             sub missings {
139 121     121 1 87 my ($self) = @_;
140              
141             return grep(
142 182         4969 { my $field = $self->get_field($_); !defined($field) }
  182         572  
143 121         99 keys %{ $self->fields }
  121         2799  
144             );
145             }
146              
147              
148             sub success {
149 105     105 1 208 my ($self) = @_;
150              
151 105 100 100     152 if($self->missing_count || $self->invalid_count) {
152 27         124 return 0;
153             }
154              
155 78         355 return 1;
156             }
157              
158              
159             sub valids {
160 15     15 1 14 my ($self) = @_;
161              
162             return grep(
163 23 100       629 { my $field = $self->get_field($_); defined($field) && $field->valid; }
  23         479  
164 15         15 keys %{ $self->fields }
  15         432  
165             );
166             }
167              
168              
169             sub valid_count {
170 13     13 1 20 my ($self) = @_;
171              
172 13         24 return scalar($self->valids);
173             }
174              
175              
176             sub valid_values {
177 2     2 1 2 my ($self) = @_;
178              
179 2         7 return map { $_ => $self->get_value($_) } $self->valids;
  4         8  
180             }
181              
182             __PACKAGE__->meta->make_immutable;
183              
184             1;
185              
186             __END__
187              
188             =pod
189              
190             =head1 NAME
191              
192             Data::Verifier::Results - Results of a Data::Verifier verify
193              
194             =head1 VERSION
195              
196             version 0.61
197              
198             =head1 SYNOPSIS
199              
200             use Data::Verifier;
201              
202             my $dv = Data::Verifier->new(profile => {
203             name => {
204             required => 1,
205             type => 'Str',
206             filters => [ qw(collapse trim) ]
207             },
208             age => {
209             type => 'Int'
210             },
211             sign => {
212             required => 1,
213             type => 'Str'
214             }
215             });
216              
217             my $results = $dv->verify({
218             name => 'Cory', age => 'foobar'
219             });
220              
221             $results->success; # no
222              
223             $results->is_invalid('name'); # no
224             $results->is_invalid('age'); # yes
225              
226             $results->is_missing('name'); # no
227             $results->is_missing('sign'); # yes
228              
229             =head1 SERIALIZATION
230              
231             Data::Verifier uses L<MooseX::Storage::Deferred> to allow quick and easy
232             serialization. So a quick call to C<freeze> will serialize this object into
233             JSON and C<thaw> will inflate it. The only caveat is that we don't serialize
234             the C<value> attribute. Since coercion allows you to make the result any type
235             you want, it can't reliably be serialized. Use original value if you are
236             serializing Result objects and using them to refill forms or something.
237              
238             my $json = $results->freeze({ format => 'JSON' });
239             # ...
240             my $results = Data::Verifier::Results->thaw($json, { format => 'JSON' });
241              
242             =head1 INTERNALS
243              
244             This module has a hashref attribute C<fields>. The keys are the names of the
245             fields from the profile. The corresponding values are are either C<undef> or a
246             L<Data::Verifier::Field> object.
247              
248             The B<only> keys that will be populated in the Result object are those that were
249             listed in the profile. Any arbitrary fields I<will not> be part of the result
250             object, as they were not part of the profile. You should not query the result
251             object for the state of any arbitrary fields. This will not throw any
252             exceptions, but it may not return the results you want if you query for
253             arbitrary field names.
254              
255             =head1 ATTRIBUTES
256              
257             =head2 fields
258              
259             HashRef of fields in this Results object.
260              
261             =head1 METHODS
262              
263             =head2 get_field ($name)
264              
265             Gets the field object, if it exists, for the name provided.
266              
267             =head2 has_field ($name)
268              
269             Returns true if the name in question is part of this result object. This
270             should be true for any field that was in the profile.
271              
272             =head2 set_field ($name)
273              
274             Sets the field object (you shouldn't be doing this directly) for the name
275             provided.
276              
277             =head2 get_original_value ($name)
278              
279             Get the original value for the specified field.
280              
281             =head2 get_post_filter_value ($name)
282              
283             Get the post-filter value for the specified field.
284              
285             =head2 get_value ($name)
286              
287             Returns the value for the specified field. The value may be different from
288             the one originally supplied due to filtering or coercion.
289              
290             =head2 get_values (@names)
291              
292             Same concept as C<get_value> but will return a list of respective values in
293             the same order in which you provide the names.
294              
295             =head2 is_invalid ($name)
296              
297             Returns true if the specific field is invalid.
298              
299             =head2 is_missing ($name)
300              
301             Returns true if the specified field is missing.
302              
303             =head2 is_valid ($name)
304              
305             Returns true if the field is valid.
306              
307             =head2 is_wrong ($name)
308              
309             Returns true if the value was required and is missing or if the value did not
310             pass it's type constraint. This is a one-stop method for determining if the
311             field in question is "wrong".
312              
313             =head2 merge ($other_results_object)
314              
315             Merge an existing Data::Verifier::Results object into this one.
316              
317             =head2 invalid_count
318              
319             Returns the count of invalid fields in this result.
320              
321             =head2 invalids
322              
323             Returns a list of invalid field names.
324              
325             =head2 missing_count
326              
327             Returns the count of missing fields in this result.
328              
329             =head2 missings
330              
331             Returns a list of missing field names.
332              
333             =head2 success
334              
335             Returns true or false based on if the verification's success.
336              
337             =head2 valids
338              
339             Returns a list of keys for which we have valid values.
340              
341             =head2 valid_count
342              
343             Returns the number of valid fields in this Results.
344              
345             =head2 valid_values
346              
347             Returns a hash of valid values in the form of C<name => value>. This is a
348             convenient method for instantiating Moose objects from your verified data.
349              
350             =head1 AUTHOR
351              
352             Cory G Watson <gphat@cpan.org>
353              
354             =head1 COPYRIGHT AND LICENSE
355              
356             This software is copyright (c) 2016 by Cold Hard Code, LLC.
357              
358             This is free software; you can redistribute it and/or modify it under
359             the same terms as the Perl 5 programming language system itself.
360              
361             =cut