File Coverage

lib/Dancer/Plugin/DataTransposeValidator/Validator.pm
Criterion Covered Total %
statement 50 50 100.0
branch 12 14 85.7
condition 7 10 70.0
subroutine 8 8 100.0
pod 1 1 100.0
total 78 83 93.9


line stmt bran cond sub pod time code
1             package Dancer::Plugin::DataTransposeValidator::Validator;
2              
3 1     1   642 use Data::Transpose::Validator;
  1         67982  
  1         44  
4 1     1   8 use File::Spec;
  1         2  
  1         22  
5 1     1   4 use Moo;
  1         2  
  1         5  
6 1     1   250 use namespace::clean;
  1         2  
  1         23  
7              
8             =head1 NAME
9              
10             Dancer::Plugin::DataTransposeValidator::Validator - validator class for
11             Dancer::Plugin::DataTransposeValidator
12              
13             =head1 METHODS
14              
15             =head2 additional_args
16              
17             Any additional arguments passed in to validator are passed as arguments
18             to L if it contains a code reference.
19              
20             =cut
21              
22             has additional_args => (
23             is => 'ro',
24             isa => sub {
25             die "params must be as array reference" unless ref( $_[0] ) eq 'ARRAY';
26             },
27             default => sub { [] },
28             );
29              
30             =head2 appdir
31              
32             Dancer's appdir. Required.
33              
34             =cut
35              
36             has appdir => (
37             is => 'ro',
38             isa => sub {
39             die "appdir must be a valid string"
40             unless ( defined $_[0] && $_[0] =~ /\S/ );
41             die "appdir must be a valid directory" unless -d $_[0];
42             },
43             required => 1,
44             );
45              
46             =head2 css_error_class
47              
48             Returns the value supplied via L or 'has-error'.
49              
50             =cut
51              
52             has css_error_class => (
53             is => 'lazy',
54             isa => sub {
55             die "rules_file must be a valid string"
56             unless ( defined $_[0] && $_[0] =~ /\S/ );
57             },
58             );
59              
60             sub _build_css_error_class {
61 6     6   790 my $self = shift;
62             return
63             defined $self->plugin_setting->{css_error_class}
64             ? $self->plugin_setting->{css_error_class}
65 6 50       158 : 'has-error';
66             }
67              
68             =head2 params
69              
70             Hash reference of params to validate. Required.
71              
72             =cut
73              
74             has params => (
75             is => 'ro',
76             isa => sub {
77             die "params must be a hash reference" unless ref( $_[0] ) eq 'HASH';
78             },
79             required => 1,
80             );
81              
82             =head2 plugin_setting
83              
84             plugin_setting hash reference. Required.
85              
86             =cut
87              
88             has plugin_setting => (
89             is => 'ro',
90             isa => sub {
91             die "plugin_setting must be a hash reference"
92             unless ref( $_[0] ) eq 'HASH';
93             },
94             required => 1,
95             );
96              
97             =head2 rules_dir
98              
99             rules_dir setting from L or 'validation' if that is undef
100              
101             =cut
102              
103             has rules_dir => (
104             is => 'lazy',
105             isa => sub {
106             die "rules directory does not exist: $_[0]" unless -d $_[0];
107             },
108             );
109              
110             sub _build_rules_dir {
111 9     9   681 my $self = shift;
112             my $dir =
113             defined $self->plugin_setting->{rules_dir}
114             ? $self->plugin_setting->{rules_dir}
115 9 100       75 : 'validation';
116 9         320 return File::Spec->catdir( $self->appdir, $dir );
117             }
118              
119             =head2 rules_file
120              
121             The name of the rules file. Required.
122              
123             =cut
124              
125             has rules_file => (
126             is => 'ro',
127             isa => sub {
128             die "rules_file must be a valid string"
129             unless ( defined $_[0] && $_[0] =~ /\S/ );
130             },
131             required => 1,
132             );
133              
134             =head2 rules
135              
136             The rules produced via eval of L
137              
138             =cut
139              
140             has rules => (
141             is => 'lazy',
142             isa => sub {
143             die "plugin_setting must be a hash reference"
144             unless ref( $_[0] ) eq 'HASH';
145             },
146             );
147              
148             sub _build_rules {
149 9     9   622 my $self = shift;
150 9         213 my $path = File::Spec->catfile( $self->rules_dir, $self->rules_file );
151 8 50       3410 my $rules = do $path or die "bad rules file: $path - $! $@";
152 8 100       258 if ( ref($rules) eq 'CODE' ) {
153 2         5 return $rules->( @{ $self->additional_args } );
  2         13  
154             }
155 6         222 return $rules;
156             }
157              
158             =head2 transpose
159              
160             Uses Data::Transpose::Validator to transpose and validate L.
161              
162             =cut
163              
164             sub transpose {
165 9     9 1 144 my $self = shift;
166              
167 9         215 my $rules = $self->rules;
168              
169 8   50     111 my $options = $rules->{options} || {};
170 8   50     30 my $prepare = $rules->{prepare} || {};
171              
172 8         270 my $dtv = Data::Transpose::Validator->new(%$options);
173 8         12105 $dtv->prepare(%$prepare);
174              
175 8         156273 my $params = $self->params;
176              
177 8         75 my $clean = $dtv->transpose($params);
178 8         46238 my $ret;
179              
180 8 100       36 if ($clean) {
181 2         9 $ret->{valid} = 1;
182 2         5 $ret->{values} = $clean;
183             }
184             else {
185 6         20 $ret->{valid} = 0;
186 6         29 $ret->{values} = $dtv->transposed_data;
187              
188 6         34 my $errors_hash = $self->plugin_setting->{errors_hash};
189              
190 6         30 my $v_hash = $dtv->errors_hash;
191 6         431 while ( my ( $key, $value ) = each %$v_hash ) {
192              
193 11         256 $ret->{css}->{$key} = $self->css_error_class;
194              
195 11         130 my @errors = map { $_->{value} } @{$value};
  20         52  
  11         24  
196              
197 11 100 100     81 if ( $errors_hash && $errors_hash eq 'joined' ) {
    100 66        
198 2         15 $ret->{errors}->{$key} = join( ". ", @errors );
199             }
200             elsif ( $errors_hash && $errors_hash eq 'arrayref' ) {
201 2         14 $ret->{errors}->{$key} = \@errors;
202             }
203             else {
204 7         53 $ret->{errors}->{$key} = $errors[0];
205             }
206             }
207             }
208 8         222 return $ret;
209             }
210              
211             1;