File Coverage

blib/lib/Data/Transpose/Validator/Group.pm
Criterion Covered Total %
statement 40 40 100.0
branch 10 10 100.0
condition 3 3 100.0
subroutine 7 7 100.0
pod 1 1 100.0
total 61 61 100.0


line stmt bran cond sub pod time code
1             package Data::Transpose::Validator::Group;
2              
3 11     11   38 use strict;
  11         11  
  11         283  
4 11     11   34 use warnings;
  11         11  
  11         199  
5              
6 11     11   40 use Moo;
  11         11  
  11         47  
7             extends 'Data::Transpose::Validator::Base';
8 11     11   2012 use MooX::Types::MooseLike::Base qw(:all);
  11         13  
  11         2829  
9 11     11   48 use namespace::clean;
  11         26  
  11         55  
10              
11             =head1 NAME
12              
13             Data::Transpose::Validator::Group - Class for grouped field
14              
15             =head1 SYNOPSIS
16              
17             =head1 METHODS
18              
19             =head2 new(name => "name", fields => [$obj1, $obj2, ... ])
20              
21             =cut
22              
23             has fields => (is => 'ro',
24             isa => ArrayRef);
25              
26             has name => (is => 'ro',
27             isa => Str);
28              
29             has equal => (is => 'rw',
30             isa => Bool,
31             default => sub { 1 });
32              
33              
34             =head2 fields
35              
36             Return an arrayref of the objects set in the constructor. This is read only.
37              
38             =head2 name
39              
40             Return the name set in the constructor. This is read only. If you want
41             a sensible default error string, you should set this to something that
42             concatenated with "%s differ" makes sense.
43              
44             E.g. "passwords" will produce such an error: "Passwords differ!";
45              
46             =head2 equal
47              
48             Set to a true value if the check for equality is needed. Defaults to
49             true, and so far it's the only use of this module.
50              
51             =head2 is_valid
52              
53             Returns true if the group validates. If no check were done (because,
54             e.g. you set equal => 0) this method returns true but sets a warning,
55             which you can retrieve with -C.
56              
57             =cut
58              
59              
60             sub is_valid {
61 14     14 1 15 my $self = shift;
62 14         45 $self->reset_errors;
63 14         424 $self->reset_warnings;
64 14         1383 my $valid = 1;
65 14         14 my $checks = 0;
66 14 100       179 if ($self->equal) {
67 11         451 $valid = $self->_check_if_fields_are_equal;
68 11         13 $checks++;
69             }
70 14 100 100     70 if ($valid && !$checks) {
71             # unclear if we should die here or just warn. But the user
72             # could very well not check the warnings.
73 3         13 $self->warnings("No check were done");
74             }
75 14         52 return $valid;
76             }
77              
78              
79             sub _check_if_fields_are_equal {
80 11     11   12 my $self = shift;
81 11         13 my @fields = @{$self->fields};
  11         29  
82 11         10 my $value;
83 11         13 my $equal = 1;
84 11         20 foreach my $f (@fields) {
85             # first run the value is undef, so we can't check
86 22 100       33 if (defined $value) {
87 11 100       167 if ($value ne $f->dtv_value) {
88 6         12 $equal = 0;
89             }
90             }
91             else {
92 11         182 $value = $f->dtv_value;
93             }
94             }
95 11 100       19 unless ($equal) {
96 6         18 my $name = ucfirst($self->name);
97 6         32 $self->error([ not_equal => "$name differ!" ]);
98             }
99 11         17 return $equal;
100             }
101              
102             1;