File Coverage

blib/lib/Dancer2/Plugin/DataTransposeValidator/Validator.pm
Criterion Covered Total %
statement 20 20 100.0
branch 4 4 100.0
condition n/a
subroutine 6 6 100.0
pod 1 2 50.0
total 31 32 96.8


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::DataTransposeValidator::Validator;
2              
3 2     2   86369 use Moo;
  2         11181  
  2         15  
4 2     2   2208 use Dancer2::Core::Types qw(Bool Dict Enum HashRef InstanceOf Maybe Str Undef);
  2         145185  
  2         20  
5 2     2   6273 use Data::Transpose::Validator;
  2         146068  
  2         78  
6 2     2   20 use namespace::clean;
  2         5  
  2         14  
7              
8             =head1 NAME
9              
10             Dancer2::Plugin::DataTransposeValidator::Validator
11              
12             =head1 VERSION
13              
14             Version 0.200
15              
16             =cut
17              
18             our $VERSION = '0.200';
19              
20             =head1 CONSTRUCTOR ARGS
21              
22             The following constructor args are all required.
23              
24             =head2 params
25              
26             A hash reference of parameters to be validated.
27              
28             =cut
29              
30             has params => (
31             is => 'ro',
32             isa => HashRef,
33             required => 1,
34             );
35              
36             =head2 rules
37              
38             A hash reference with the following keys to be used by
39             L:
40              
41             =over
42              
43             =item * options
44              
45             The L constructor arguments.
46              
47             =item * prepare
48              
49             The arguments for L.
50              
51             =back
52              
53             =cut
54              
55             has rules => (
56             is => 'ro',
57             isa => Dict [ options => HashRef, prepare => HashRef ],
58             required => 1,
59             );
60              
61             =head2 css_error_class
62              
63             The css error class. See
64             L.
65              
66             =cut
67              
68             has css_error_class => (
69             is => 'ro',
70             isa => Str,
71             required => 1,
72             );
73              
74             =head2 errors_hash
75              
76             Configuration which determines how L are returned. See
77             L.
78              
79             =cut
80              
81             has errors_hash => (
82             is => 'ro',
83             isa => Enum [qw/arrayref joined/] | Undef,
84             required => 1,
85             );
86              
87             =head1 ATTRIBUTES
88              
89             =cut
90              
91             has _dtv => (
92             is => 'ro',
93             isa => InstanceOf ['Data::Transpose::Validator'],
94             lazy => 1,
95             default => sub {
96             my $self = shift;
97             my $dtv =
98             Data::Transpose::Validator->new( %{ $self->rules->{options} } );
99             $dtv->prepare( %{ $self->rules->{prepare} } );
100             return $dtv;
101             },
102             );
103              
104             has _clean => (
105             is => 'ro',
106             isa => Maybe [HashRef],
107             lazy => 1,
108             default => sub {
109             my $self = shift;
110             return $self->_dtv->transpose( $self->params );
111             },
112             );
113              
114             =head2 valid
115              
116             Returns a boolean value indicating whether L are valid.
117              
118             =cut
119              
120             has valid => (
121             is => 'ro',
122             isa => Bool,
123             lazy => 1,
124             default => sub {
125             my $self = shift;
126             return $self->_clean ? 1 : 0;
127             },
128             );
129              
130             =head2 values
131              
132             Returns a hash reference of transposed values.
133              
134             =cut
135              
136             has values => (
137             is => 'ro',
138             isa => HashRef,
139             lazy => 1,
140             default => sub {
141             my $self = shift;
142             return $self->valid ? $self->_clean : $self->_dtv->transposed_data;
143             },
144             );
145              
146             =head2 css
147              
148             If there are any L, returns a hash reference of field to css class
149             for any fields with errors.
150              
151             =cut
152              
153             has css => (
154             is => 'ro',
155             isa => HashRef,
156             lazy => 1,
157             default => sub {
158             my $self = shift;
159             return {} if $self->valid;
160             my $errors = $self->errors;
161             return +{ map { $_ => $self->css_error_class } keys %$errors };
162             },
163             );
164              
165             =head2 errors
166              
167             If there are any errors, returns a hash reference of field and errors.
168              
169             =cut
170              
171             has errors => (
172             is => 'ro',
173             isa => HashRef,
174             lazy => 1,
175             default => sub {
176             my $self = shift;
177             return {} if $self->valid;
178              
179             my $errors_hash = $self->errors_hash || '';
180             my $ret;
181             my $dtv_errors = $self->_dtv->errors_hash;
182             while ( my ( $key, $value ) = each %$dtv_errors ) {
183             my @errors = map { $_->{value} } @{$value};
184             if ( $errors_hash eq 'joined' ) {
185             $ret->{$key} = join( ". ", @errors );
186             }
187             elsif ( $errors_hash eq 'arrayref' ) {
188             $ret->{$key} = \@errors;
189             }
190             else {
191             $ret->{$key} = $errors[0];
192             }
193             }
194             return $ret;
195             },
196             );
197              
198             # Make sure all attributes are built, for backwards-compat with comsumers
199             # that expect a hashref rather than an object.
200             sub BUILD {
201 17     17 0 49596 my $self = shift;
202 17         427 $self->values;
203 17 100       78411 unless ( $self->valid ) {
204 13         392 $self->css;
205 13         553 $self->errors;
206             }
207             }
208              
209             =head2 TO_JSON
210              
211             Returns a hash reference of L and L, and if L is
212             false also adds L and L.
213              
214             If L serializer has C set to a true value, then
215             this method is called automatically on object before serialization.
216              
217             =cut
218              
219             sub TO_JSON {
220 13     13 1 34516 my $self = shift;
221             return +{
222 13 100       263 map { $_ => $self->$_ }
  46         1035  
223             $self->valid ? (qw/valid values/) : (qw/css errors valid values/)
224             };
225             }
226              
227             1;