File Coverage

blib/lib/Method/ParamValidator/Key/Field.pm
Criterion Covered Total %
statement 29 32 90.6
branch 9 14 64.2
condition 4 6 66.6
subroutine 9 9 100.0
pod 0 3 0.0
total 51 64 79.6


line stmt bran cond sub pod time code
1             package Method::ParamValidator::Key::Field;
2              
3             $Method::ParamValidator::Key::Field::VERSION = '0.14';
4             $Method::ParamValidator::Key::Field::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Method::ParamValidator::Key::Field - Represents 'parameter key field' for Method::ParamValidator.
9              
10             =head1 VERSION
11              
12             Version 0.14
13              
14             =cut
15              
16 6     6   112 use 5.006;
  6         21  
17 6     6   31 use Data::Dumper;
  6         12  
  6         218  
18              
19 6     6   2722 use Moo;
  6         59310  
  6         32  
20 6     6   9696 use namespace::autoclean;
  6         67727  
  6         26  
21              
22 6     6   3237 use Types::Standard qw(:all);
  6         374095  
  6         61  
23 6     6   245556 use Method::ParamValidator::Key::Field::DataType qw(:all);
  6         15  
  6         68  
24              
25             has 'name' => (is => 'ro', isa => Str, required => 1);
26             has 'format' => (is => 'ro', isa => Str, default => sub { 's' });
27             has 'check' => (is => 'rw', isa => CodeRef, predicate => 1);
28             has 'source' => (is => 'ro', isa => HashRef[Str], predicate => 1);
29             has 'message' => (is => 'ro', isa => Str);
30             has 'multi' => (is => 'ro');
31              
32 10   33 10 0 52 sub str { !(defined $_[0] && $_[0] =~ /^\d+$/) };
33 8 50   8 0 99 sub int { (defined $_[0] && $_[0] =~ /^\d+$/) };
34              
35             =head1 DESCRIPTION
36              
37             B.
38              
39             =cut
40              
41             sub valid {
42 23     23 0 46 my ($self, $value) = @_;
43              
44 23 100       48 if ($self->has_check) {
45 1         23 return $self->check->($value);
46             }
47             else {
48 22 100 100     78 if ($self->has_source && (keys %{$self->source})) {
  19 100       72  
    50          
49 4         9 my $separator = $self->multi;
50 4 50       8 if (defined $separator) {
51             # Check each against the source.
52 0         0 foreach (split /\Q$separator\E/, $value) {
53 0 0       0 return 0 unless exists $self->source->{uc($_)};
54             }
55 0         0 return 1;
56             }
57 4         28 return exists $self->source->{uc($value)};
58             }
59             elsif ($self->format eq 's') {
60 10         21 &str($value);
61             }
62             elsif ($self->format eq 'd') {
63 8         17 &int($value);
64             }
65             }
66             }
67              
68             =head1 AUTHOR
69              
70             Mohammad S Anwar, C<< >>
71              
72             =head1 REPOSITORY
73              
74             L
75              
76             =head1 BUGS
77              
78             Please report any bugs or feature requests to C,
79             or through the web interface at L.
80             I will be notified and then you'll automatically be notified of progress on your
81             bug as I make changes.
82              
83             =head1 SUPPORT
84              
85             You can find documentation for this module with the perldoc command.
86              
87             perldoc Method::ParamValidator::Key::Field
88              
89             You can also look for information at:
90              
91             =over 4
92              
93             =item * RT: CPAN's request tracker (report bugs here)
94              
95             L
96              
97             =item * AnnoCPAN: Annotated CPAN documentation
98              
99             L
100              
101             =item * CPAN Ratings
102              
103             L
104              
105             =item * Search CPAN
106              
107             L
108              
109             =back
110              
111             =head1 LICENSE AND COPYRIGHT
112              
113             Copyright (C) 2015 Mohammad S Anwar.
114              
115             This program is free software; you can redistribute it and / or modify it under
116             the terms of the the Artistic License (2.0). You may obtain a copy of the full
117             license at:
118              
119             L
120              
121             Any use, modification, and distribution of the Standard or Modified Versions is
122             governed by this Artistic License.By using, modifying or distributing the Package,
123             you accept this license. Do not use, modify, or distribute the Package, if you do
124             not accept this license.
125              
126             If your Modified Version has been derived from a Modified Version made by someone
127             other than you,you are nevertheless required to ensure that your Modified Version
128             complies with the requirements of this license.
129              
130             This license does not grant you the right to use any trademark, service mark,
131             tradename, or logo of the Copyright Holder.
132              
133             This license includes the non-exclusive, worldwide, free-of-charge patent license
134             to make, have made, use, offer to sell, sell, import and otherwise transfer the
135             Package with respect to any patent claims licensable by the Copyright Holder that
136             are necessarily infringed by the Package. If you institute patent litigation
137             (including a cross-claim or counterclaim) against any party alleging that the
138             Package constitutes direct or contributory patent infringement,then this Artistic
139             License to you shall terminate on the date that such litigation is filed.
140              
141             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
142             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
143             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
144             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
145             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
146             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
147             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
148              
149             =cut
150              
151             1; # End of Method::ParamValidator::Key::Field