File Coverage

blib/lib/XAS/Service/Profiles.pm
Criterion Covered Total %
statement 18 68 26.4
branch 0 18 0.0
condition 0 20 0.0
subroutine 6 10 60.0
pod 1 1 100.0
total 25 117 21.3


line stmt bran cond sub pod time code
1             package XAS::Service::Profiles;
2              
3             our $VERSION = '0.01';
4              
5 1     1   453 use Hash::Merge;
  1         1736  
  1         41  
6 1     1   531 use Data::FormValidator;
  1         17644  
  1         38  
7 1     1   6 use XAS::Utils ':validation';
  1         1  
  1         7  
8 1     1   135 use XAS::Constants 'HASHREF';
  1         1  
  1         10  
9 1     1   71 use Data::FormValidator::Results;
  1         1  
  1         15  
10 1     1   3 use Badger::Class import => 'class';
  1         1  
  1         5  
11              
12             #use Data::Dumper;
13              
14             # -----------------------------------------------------------------
15             # Overrides - WARNING Will Robinson WARNING here be dragons
16             # -----------------------------------------------------------------
17              
18             class('Data::FormValidator::Results')->methods(
19             _generate_msgs => sub {
20 0     0     my $self = shift;
21 0   0       my $controls = shift || {};
22              
23 0 0 0       if (defined $controls and ref $controls ne 'HASH') {
24              
25 0           die "$0: parameter passed to msgs must be a hash ref";
26              
27             }
28              
29             # Allow msgs to be called more than one to accumulate error messages
30              
31 0   0       $self->{msgs} ||= {};
32 0   0       $self->{profile}{msgs} ||= {};
33 0           $self->{msgs} = { %{ $self->{msgs} }, %$controls };
  0            
34              
35             # Legacy typo support.
36              
37 0           for my $href ($self->{msgs}, $self->{profile}{msgs}) {
38              
39 0 0 0       if ((not defined $href->{invalid_separator}) &&
40             (defined $href->{invalid_seperator})) {
41              
42 0           $href->{invalid_separator} = $href->{invalid_seperator};
43              
44             }
45              
46             }
47              
48             my %profile = (
49             prefix => '',
50             missing => 'Missing',
51             invalid => 'Invalid',
52             invalid_separator => ' ',
53              
54             format => '* %s',
55 0           %{ $self->{msgs} },
56 0           %{ $self->{profile}{msgs} },
  0            
57             );
58              
59 0           my %msgs = ();
60              
61             # Add invalid messages to hash
62             # look at all the constraints, look up their messages (or provide a default)
63             # add field + formatted constraint message to hash
64              
65 0 0         if ($self->has_invalid) {
66              
67 0           my @invalids = $self->invalid;
68              
69 0           foreach my $i (@invalids) {
70              
71             $msgs{$i} = join(
72             $profile{invalid_separator},
73 0   0       $self->_error_msg_fmt($profile{format}, ($profile{constraints}{$i} || $profile{invalid}))
74             );
75              
76             }
77              
78             }
79              
80             # Add missing messages, if any
81              
82 0 0         if ($self->has_missing) {
83              
84 0           my $missing = $self->missing;
85              
86 0           for my $m (@$missing) {
87              
88 0           $msgs{$m} = $self->_error_msg_fmt($profile{format},$profile{missing});
89              
90             }
91              
92             }
93              
94 0           my $msgs_ref = $self->prefix_hash($profile{prefix},\%msgs);
95              
96 0 0         unless ($self->success) {
97              
98 0 0         $msgs_ref->{ $profile{any_errors} } = 1 if defined $profile{any_errors};
99              
100             }
101              
102 0           return $msgs_ref;
103              
104             },
105             _error_msg_fmt => sub {
106 0     0     my $self = shift;
107 0           my $fmt = shift;
108 0           my $msg = shift;
109              
110 0   0       $fmt ||= '* %s';
111              
112 0 0         ($fmt =~ m/%s/) || die 'format must contain %s';
113              
114 0           return sprintf $fmt, $msg;
115              
116             },
117             prefix_hash => sub {
118 0     0     my $self = shift;
119 0           my $pre = shift;
120 0           my $href = shift;
121              
122 0 0 0       die "prefix_hash: need two arguments" unless (defined($pre) && defined($href));
123 0 0         die "prefix_hash: second argument must be a hash ref" unless (ref $href eq 'HASH');
124              
125 0           my %out;
126              
127 0           for (keys %$href) {
128              
129 0           $out{$pre.$_} = $href->{$_};
130              
131             }
132              
133 0           return \%out;
134              
135             }
136             );
137              
138             # -----------------------------------------------------------------
139             # Public Methods
140             # -----------------------------------------------------------------
141              
142             sub new {
143 0     0 1   my $class = shift;
144 0           my @profiles = validate_params(\@_, [
145             { type => HASHREF },
146             ({ optional => 1, type => HASHREF}) x (@_ - 1),
147             ]);
148              
149 0           my $profile = {};
150 0           my $merger = Hash::Merge->new('RIGHT_PRECEDENT');
151              
152 0           foreach my $p (@profiles) {
153              
154 0           $profile = \%{ $merger->merge($profile, $p) };
  0            
155              
156             }
157              
158 0           return Data::FormValidator->new($profile);
159              
160             }
161              
162             # -----------------------------------------------------------------
163             # Private Methods
164             # -----------------------------------------------------------------
165              
166             1;
167              
168             =head1 NAME
169              
170             XAS::Service::Profiles - A class for creating standard validation profiles.
171              
172             =head1 SYNOPSIS
173              
174             use XAS::Service::Profiles;
175             use XAS::Service::Profiles::Search;
176              
177             my $params = {
178             start => 0,
179             limit => 25,
180             sort => qq/[{"field":"server',"direction":"DESC"}]/
181             };
182              
183             my @fields = [qw(id server queue requestor typeofrequest status startdatetime)];
184              
185             my $search = XAS::Service::Profiles::Search->new(\@fields);
186             my $profile = XAS::Service::Profiles->new($search);
187              
188             my $results = $profile->check($params, 'pager');
189              
190             if ($results->has_invalid) {
191              
192             my @invalids = $results->invalid;
193              
194             foreach my $invalid (@invalids) {
195              
196             printf("%s %s\n", $invalid, $results->msgs->{$invalid});
197              
198             }
199              
200             }
201              
202             =head1 DESCRIPTION
203              
204             This module combines multiple validation profiles into one
205             L validator.
206              
207             =head1 METHODS
208              
209             =head2 new($hash, ...)
210              
211             This method initilizes the validator by combining multiple profiles.
212              
213             =over 4
214              
215             =item B<$hash>
216              
217             A hash of validation profiles, there may be more then one. They are combined
218             such that later profiles may overwrite earlier ones.
219              
220             =back
221              
222             =head1 OVERRIDES
223              
224             This module overrides the following methods in L.
225              
226             =head2 _generate_msgs
227              
228             For whatever reason, it wouldn't find error messages for constraints. Not
229             sure why. No bugs reports have been filed about this. But it wouldn't work
230             for me as documented, now it does.
231              
232             =head2 _error_msg_fmt
233              
234             A supporting routine for _generate_msgs(). Allowed it to be referenced
235             from $self.
236              
237             =head2 prefix_hash
238              
239             A supporting routine for _generate_msgs(). Allowed it to be referenced
240             from $self.
241              
242             =head1 SEE ALSO
243              
244             =over 4
245              
246             =item L
247              
248             =item L
249              
250             =item L
251              
252             =back
253              
254             =head1 AUTHOR
255              
256             Kevin L. Esteb, Ekevin@kesteb.usE
257              
258             =head1 COPYRIGHT AND LICENSE
259              
260             Copyright (c) 2012-2016 Kevin L. Esteb
261              
262             This is free software; you can redistribute it and/or modify it under
263             the terms of the Artistic License 2.0. For details, see the full text
264             of the license at http://www.perlfoundation.org/artistic_license_2_0.
265              
266             =cut