File Coverage

blib/lib/Syccess/Field.pm
Criterion Covered Total %
statement 58 65 89.2
branch 13 24 54.1
condition n/a
subroutine 7 7 100.0
pod 1 2 50.0
total 79 98 80.6


line stmt bran cond sub pod time code
1             package Syccess::Field;
2             our $AUTHORITY = 'cpan:GETTY';
3             # ABSTRACT: Syccess field
4             $Syccess::Field::VERSION = '0.104';
5 9     9   3544 use Moo;
  9         9  
  9         44  
6 9     9   1919 use Module::Runtime qw( use_module );
  9         11  
  9         42  
7 9     9   4083 use Module::Load::Conditional qw( can_load );
  9         179678  
  9         5318  
8              
9             with qw(
10             MooX::Traits
11             );
12              
13             has syccess => (
14             is => 'ro',
15             required => 1,
16             weak_ref => 1,
17             );
18              
19             has name => (
20             is => 'ro',
21             required => 1,
22             );
23              
24             has label => (
25             is => 'lazy',
26             init_arg => undef,
27             );
28              
29             sub _build_label {
30 18     18   84 my ( $self ) = @_;
31 18 50       78 if (ref $self->validators_list eq 'HASH') {
32             return $self->validators_list->{label}
33 0 0       0 if defined $self->validators_list->{label};
34             } else {
35 18         20 my @validators_list = @{$self->validators_list};
  18         40  
36 18         39 while (@validators_list) {
37 21         28 my ( $key, $arg ) = splice(@validators_list,0,2);
38 21 100       67 return $arg if $key eq 'label';
39             }
40             }
41 15         25 my $name = $self->name;
42 15         14 $name =~ s/_([a-z])/ \U$1/g;
43 15         53 return ucfirst($name);
44             }
45              
46             has validators_args => (
47             is => 'ro',
48             predicate => 1,
49             );
50              
51             has validators_list => (
52             is => 'ro',
53             required => 1,
54             init_arg => 'validators',
55             );
56              
57             has validators => (
58             is => 'lazy',
59             init_arg => undef,
60             );
61              
62             sub _build_validators {
63 19     19   88 my ( $self ) = @_;
64             my %validators_args = $self->has_validators_args
65 19 50       61 ? (%{$self->validators_args}) : ();
  0         0  
66 19         15 my @validators;
67             my @validators_list = ref $self->validators_list eq 'HASH'
68 0         0 ? ( map { $_, $self->validators_list->{$_} }
69 0         0 sort { $a cmp $b }
70 0         0 keys %{$self->validators_list} )
71 19 50       62 : ( @{$self->validators_list} );
  19         43  
72 19         57 while (@validators_list) {
73 22         904 my ( $key, $arg ) = splice(@validators_list,0,2);
74 22 100       51 next if $key eq 'label';
75 19         14 my %args;
76 19 100       56 if (ref $arg eq 'HASH') {
77 10         10 %args = %{$arg};
  10         25  
78             } else {
79 9         14 $args{arg} = $arg;
80             }
81 19         28 $args{syccess_field} = $self;
82 19         34 push @validators, $self->load_class_by_key($key)->new(
83             %validators_args, %args
84             );
85             }
86 19         2709 return [ @validators ];
87             }
88              
89             has load_class_cache => (
90             is => 'ro',
91             init_arg => undef,
92             default => sub {{}},
93             );
94              
95             sub load_class_by_key {
96 19     19 0 26 my ( $self, $key ) = @_;
97 19 50       60 return $self->load_class_cache->{$key} if defined $self->load_class_cache->{$key};
98 19         17 my $class;
99 19 50       46 if ($key =~ m/::/) {
100 0 0       0 if (can_load( modules => { $key, 0 } )) {
101 0         0 $class = $key;
102             }
103             } else {
104 19         17 my $module = $key;
105 19         28 $module =~ s/_([a-z])/\U$1/g;
106 19         36 $module = ucfirst($module);
107 19         14 my @namespaces = @{$self->syccess->validator_namespaces};
  19         310  
108 19         70 for my $namespace (@namespaces) {
109 19         37 my $can_class = $namespace.'::'.$module;
110 19 50       60 if (can_load( modules => { $can_class, 0 } )) {
111 19         875 $class = $can_class;
112 19         32 last;
113             }
114             }
115             }
116 19 50       40 die __PACKAGE__." can't load validator for ".$key unless $class;
117 19         57 return $self->load_class_cache->{$key} = use_module($class);
118             }
119              
120             sub validate {
121 43     43 1 68 my ( $self, %params ) = @_;
122 43         36 my @validators = @{$self->validators};
  43         596  
123 43         113 my @messages;
124 43         48 for my $validator (@validators) {
125 43         119 push @messages, $validator->validate(%params);
126             }
127 43         152 return @messages;
128             }
129              
130             1;
131              
132             __END__
133              
134             =pod
135              
136             =head1 NAME
137              
138             Syccess::Field - Syccess field
139              
140             =head1 VERSION
141              
142             version 0.104
143              
144             =head1 DESCRIPTION
145              
146             This class will be used to gather all the validator objects for a specific
147             field of your L<Syccess> definition.
148              
149             =head1 ATTRIBUTES
150              
151             =head2 name
152              
153             This attribute is your name for the field given on the L<Syccess> definition.
154              
155             =head2 label
156              
157             If not set via the validators (See L<Syccess/Label Concept>), this will be
158             calculated out of the L</name>, which was given on the L<Syccess/fields>
159             definition.
160              
161             =head2 validators
162              
163             Contains all the L<Syccess::Validator> object that are generated by the
164             fields definition on the creation of the L<Syccess> object.
165              
166             =head1 METHODS
167              
168             =head2 validate
169              
170             This function is called by L<Syccess/validate>, and will by itself then call
171             I<validate> for all the validators of the field and gather the error messages
172             and give them back in return.
173              
174             =encoding utf8
175              
176             =head1 SUPPORT
177              
178             IRC
179              
180             Join irc.perl.org and msg Getty
181              
182             Repository
183              
184             http://github.com/Getty/p5-syccess
185             Pull request and additional contributors are welcome
186              
187             Issue Tracker
188              
189             http://github.com/Getty/p5-syccess/issues
190              
191             =head1 AUTHOR
192              
193             Torsten Raudssus <torsten@raudss.us>
194              
195             =head1 COPYRIGHT AND LICENSE
196              
197             This software is copyright (c) 2017 by Torsten Raudssus.
198              
199             This is free software; you can redistribute it and/or modify it under
200             the same terms as the Perl 5 programming language system itself.
201              
202             =cut