File Coverage

blib/lib/Syccess/Validator/Code.pm
Criterion Covered Total %
statement 15 15 100.0
branch 3 4 75.0
condition 2 6 33.3
subroutine 4 4 100.0
pod 0 1 0.0
total 24 30 80.0


line stmt bran cond sub pod time code
1             package Syccess::Validator::Code;
2             BEGIN {
3 2     2   3145 $Syccess::Validator::Code::AUTHORITY = 'cpan:GETTY';
4             }
5             # ABSTRACT: A validator to check a value through a simple coderef
6             $Syccess::Validator::Code::VERSION = '0.103';
7 2     2   15 use Moo;
  2         4  
  2         13  
8              
9             with qw(
10             Syccess::Validator
11             );
12              
13             has message => (
14             is => 'lazy',
15             );
16              
17             sub _build_message {
18 1     1   395 return 'Your value for %s is not valid.';
19             }
20              
21             sub validate {
22 6     6 0 11 my ( $self, %params ) = @_;
23 6         18 my $name = $self->syccess_field->name;
24 6 50 33     44 return if !exists($params{$name})
      33        
25             || !defined($params{$name})
26             || $params{$name} eq '';
27 6         8 my $value = $params{$name};
28 6         8 my $code = $self->arg;
29 6         7 my @return;
30 6         8 for ($value) {
31 6         18 push @return, $code->($self,%params);
32             }
33 6 100       36 return map { !defined $_ ? $self->message : $_ } @return;
  4         53  
34             }
35              
36             1;
37              
38             __END__
39              
40             =pod
41              
42             =head1 NAME
43              
44             Syccess::Validator::Code - A validator to check a value through a simple coderef
45              
46             =head1 VERSION
47              
48             version 0.103
49              
50             =head1 SYNOPSIS
51              
52             Syccess->new(
53             fields => [
54             foo => [ code => sub { $_ > 3 ? () : ('You are WRONG!') } ],
55             bar => [ code => {
56             arg => sub { $_ > 5 ? () : (undef) },
57             message => 'You have 5 seconds to comply.'
58             } ],
59             ],
60             );
61              
62             =head1 DESCRIPTION
63              
64             This validator allows checking against a CodeRef. The CodeRef will be getting
65             all parameters on B<@_> as Hash, and the specific parameter value for to check
66             against will be in B<$_>, so the coderef can decide which way he want to check.
67              
68             The CodeRef should give back nothing (not even B<undef>) if its a success. Else
69             if should give back B<undef> to release the error message given on L</message>
70             or the default error message B<'Your value for %s is not valid.'>. Alternative
71             it can also give back a string which will be used as B<message> for the error.
72              
73             =head1 ATTRIBUTES
74              
75             =head2 message
76              
77             This contains the error message or the format for the error message
78             generation. See L<Syccess::Error/validator_message>.
79              
80             =encoding utf8
81              
82             =head1 SUPPORT
83              
84             IRC
85              
86             Join #sycontent on irc.perl.org. Highlight Getty for fast reaction :).
87              
88             Repository
89              
90             http://github.com/SyContent/Syccess
91             Pull request and additional contributors are welcome
92              
93             Issue Tracker
94              
95             http://github.com/SyContent/Syccess/issues
96              
97             =cut
98              
99             =head1 AUTHOR
100              
101             Torsten Raudssus <torsten@raudss.us>
102              
103             =head1 COPYRIGHT AND LICENSE
104              
105             This software is copyright (c) 2014 by Torsten Raudssus.
106              
107             This is free software; you can redistribute it and/or modify it under
108             the same terms as the Perl 5 programming language system itself.
109              
110             =cut