File Coverage

blib/lib/Data/Transpose/Validator/Set.pm
Criterion Covered Total %
statement 43 45 95.5
branch 16 18 88.8
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 70 74 94.5


line stmt bran cond sub pod time code
1             package Data::Transpose::Validator::Set;
2 2     2   1526 use strict;
  2         4  
  2         57  
3 2     2   11 use warnings;
  2         4  
  2         62  
4 2     2   10 use Scalar::Util qw/looks_like_number/;
  2         3  
  2         129  
5 2     2   10 use Moo;
  2         4  
  2         13  
6             extends 'Data::Transpose::Validator::Base';
7 2     2   606 use MooX::Types::MooseLike::Base qw(:all);
  2         4  
  2         831  
8 2     2   11 use namespace::clean;
  2         4  
  2         16  
9              
10             =head1 NAME
11              
12             Data::Transpose::Validator::Set - Validate a string inside a set of values
13              
14             =head1 METHODS
15              
16             =head2 new(list => \@list, multiple => 1)
17              
18             Constructor to set the list in which the value to be validated must be
19             present.
20              
21             =cut
22              
23             has list => (is => 'rw',
24             isa => ArrayRef,
25             required => 1,
26             );
27              
28             has multiple => (is => 'rw',
29             isa => Bool,
30             default => sub { 0 });
31              
32              
33             =head2 is_valid($value, [ $value, $value, ... ] )
34              
35             The validator. Returns a true value if the value (or the values)
36             passed are all present in the set. Multiple values validate only if
37             the C option is set in the constructor. It also accept an
38             arrayref as single argument, if the C option is set.
39              
40             =cut
41              
42              
43             sub is_valid {
44 27     27 1 5417 my ($self, @args) = @_;
45 27         93 $self->reset_errors;
46 27         1160 my @input;
47 27 100       71 if (@args == 1) {
    50          
48 21         33 my $arg = shift @args;
49 21 100       62 if (ref($arg) eq 'ARRAY') {
    50          
50 4 100       64 if ($self->multiple) {
51 3         22 push @input, @$arg
52             }
53             else {
54 1         573 $self->error([nomulti => "No multiple values are allowed"])
55             }
56             }
57             elsif (ref($arg) ne '') {
58 0         0 die "Bad argument\n";
59             }
60             else {
61 17         34 push @input, $arg;
62             }
63             }
64             elsif (@args > 1) {
65 6 100       123 if ($self->multiple) {
66 5         35 push @input, @args;
67             } else {
68 1         14 $self->error([nomulti => "No multiple values are allowed"]);
69             }
70             }
71             else {
72 0         0 $self->error([noinput => "No value passed"]);
73             }
74 27 100       92 return undef if $self->error;
75 25         66 return $self->_check_set(@input);
76              
77             }
78              
79             sub _check_set {
80 25     25   54 my ($self, @input) = @_;
81 25         56 my %list = $self->list_as_hash;
82 25         57 foreach my $val (@input) {
83             $self->error(["missinginset", "No match in the allowed values"])
84 35 100       140 unless exists $list{$val};
85             }
86 25 100       82 $self->error ? return 0 : return 1;
87             }
88              
89              
90             =head1 INTERNAL METHODS
91              
92             =head2 multiple
93              
94             Accessor to the C option
95              
96             =head2 list
97              
98             Accessor to the C option
99              
100             =head2 list_as_hash
101              
102             Accessor to the list of values, as an hash.
103              
104             =cut
105              
106             sub list_as_hash {
107 25     25 1 32 my $self = shift;
108 25         31 my %list = map { $_ => 1 } @{$self->list};
  73         1407  
  25         454  
109 25         113 return %list;
110             }
111              
112              
113             1;