File Coverage

blib/lib/Data/Transpose/Validator/Subrefs.pm
Criterion Covered Total %
statement 25 25 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 1 2 50.0
total 37 38 97.3


line stmt bran cond sub pod time code
1             package Data::Transpose::Validator::Subrefs;
2 12     12   13535 use strict;
  12         11  
  12         291  
3 12     12   34 use warnings;
  12         20  
  12         221  
4 12     12   4751 use Moo;
  12         98210  
  12         94  
5             extends 'Data::Transpose::Validator::Base';
6 12     12   17532 use MooX::Types::MooseLike::Base qw(:all);
  12         48431  
  12         3249  
7 12     12   4462 use namespace::clean;
  12         75866  
  12         109  
8              
9             =head1 NAME
10              
11             Data::Transpose::Validator::Subrefs Validator using custom subroutines
12              
13             sub custom_sub {
14             my $field = shift;
15             return $field
16             if $field =~ m/\w/;
17             return (undef, "Not a \\w");
18             }
19            
20             my $vcr = Data::Transpose::Validator::Subrefs->new( \&custom_sub );
21            
22             ok($vcr->is_valid("H!"), "Hi! is valid");
23             ok(!$vcr->is_valid("!"), "! is not");
24             is($vcr->error, "Not a \\w", "error displayed correctly");
25            
26              
27             =cut
28              
29             =head2 new(\&subroutine)
30              
31             The constructor accepts only one argument, a reference to a
32             subroutine. The class will provide the variable to validate as the
33             first and only argument. The subroutine is expected to return a
34             true value on success, or a false value on failure.
35              
36             To set a custom error, the subroutine in case of error should return 2
37             elements, where the first should be undefined (see the example above).
38              
39              
40             =cut
41              
42             has call => (is => 'rw', isa => CodeRef, required => 1);
43              
44             =head2 call
45              
46             Accessor to the subroutine
47              
48             =head2 is_valid($what)
49              
50             The call to the validator.
51              
52             =cut
53              
54              
55             sub is_valid {
56 8     8 1 101 my ($self, $arg) = @_;
57 8         29 $self->reset_errors;
58 8         366 my ($result, $error) = $self->call->($arg);
59 8 100       1222 if ($error) {
60 2         8 $self->error($error);
61 2         7 return undef;
62             } else {
63 6         24 return $result;
64             }
65             }
66              
67             sub BUILDARGS {
68             # straight from the manual
69 7     7 0 4376 my ($class, @args) = @_;
70 7 100       29 unshift @args, 'call' if @args % 2 == 1;
71 7         110 return { @args };
72             };
73              
74              
75             1; # the last famous words
76