File Coverage

blib/lib/FormValidator/Simple/Profile.pm
Criterion Covered Total %
statement 72 75 96.0
branch 11 14 78.5
condition n/a
subroutine 16 16 100.0
pod 0 1 0.0
total 99 106 93.4


line stmt bran cond sub pod time code
1             package FormValidator::Simple::Profile;
2 23     23   26315 use strict;
  23         41  
  23         907  
3 23     23   139 use base qw/FormValidator::Simple::ArrayList/;
  23         42  
  23         15313  
4 23     23   709 use FormValidator::Simple::Exception;
  23         44  
  23         194  
5 23     23   506 use FormValidator::Simple::Iterator;
  23         44  
  23         270  
6              
7             sub _init {
8 56     56   114 my($self, $prof) = @_;
9 56         121 for (my $i = 0; $i <= $#{$prof}; $i += 2) {
  160         1043  
10 104         268 my ($key, $constraints) = ($prof->[$i], $prof->[$i + 1]);
11 104         426 my $record = FormValidator::Simple::Profile::Record->new;
12 104         297 $record->set_keys($key);
13 104         858 $record->set_constraints($constraints);
14 104         904 $self->append($record);
15             }
16             }
17              
18             sub iterator {
19 56     56 0 615 my $self = shift;
20 56         422 return FormValidator::Simple::Profile::Iterator->new($self);
21             }
22              
23             package FormValidator::Simple::Profile::Record;
24 23     23   6153 use base qw/Class::Accessor::Fast/;
  23         46  
  23         2020  
25 23     23   218 use FormValidator::Simple::Exception;
  23         42  
  23         118  
26 23     23   33799 use FormValidator::Simple::Constants;
  23         51  
  23         2061  
27 23     23   16787 use FormValidator::Simple::Constraints;
  23         61  
  23         245  
28 23     23   15613 use FormValidator::Simple::Constraint;
  23         88  
  23         252  
29              
30             __PACKAGE__->mk_accessors(qw/name keys constraints/);
31              
32             sub new {
33 105     105   180 my $class = shift;
34 105         348 my $self = bless { }, $class;
35 105         295 $self->_init(@_);
36 105         607 return $self;
37             }
38              
39             sub _init {
40 105     105   147 my $self = shift;
41 105         340 $self->name( q{ } );
42 105         1046 $self->keys( [] );
43 105         1023 $self->constraints( FormValidator::Simple::Constraints->new );
44             }
45              
46             sub set_keys {
47 108     108   5126 my ($self, $keys) = @_;
48 108 100       258 if (ref $keys) {
49 26 50       77 if (ref $keys eq 'HASH') {
50 26         94 my ($name) = keys %$keys;
51 26         56 my $params = $keys->{$name};
52 26         82 $self->name($name);
53 26 100       162 if(ref $params) {
54 25         84 $self->keys( $params );
55             }
56             else {
57 1         7 $self->keys( [$params] );
58             }
59             }
60             else {
61 0         0 FormValidator::Simple::Exception->throw(
62             qq/set keys of profile as hashref or single scalar./
63             );
64             }
65             }
66             else {
67 82         252 $self->name( $keys );
68 82         594 $self->keys( [$keys] );
69             }
70             }
71              
72             sub set_constraints {
73 109     109   14879 my ($self, $constraints) = @_;
74 109         383 $self->constraints( FormValidator::Simple::Constraints->new );
75 109 100       1143 if (ref $constraints) {
76              
77 108 50       286 if (ref $constraints eq 'ARRAY') {
78              
79             SETTING:
80 108         215 foreach my $setting ( @$constraints ) {
81 163         902 my $const = FormValidator::Simple::Constraint->new($setting);
82 163 100       418 if ($const->name eq 'NOT_BLANK') {
83 44         300 $self->constraints->needs_blank_check( TRUE );
84 44         445 next SETTING;
85             }
86             else {
87 119         741 $self->constraints->append($const);
88             }
89             }
90              
91             }
92             else {
93 0         0 FormValidator::Simple::Exception->throw(
94             qq/set constraints as arrayref or single scalar./
95             );
96             }
97             }
98             else {
99 1         8 my $const = FormValidator::Simple::Constraint->new($constraints);
100 1 50       5 if ($const->name eq 'NOT_BLANK') {
101 0         0 $self->constraints->needs_blank_check( TRUE );
102             }
103             else {
104 1         46 $self->constraints->append($const);
105             }
106             }
107             }
108              
109             package FormValidator::Simple::Profile::Iterator;
110 23     23   24779 use base qw/FormValidator::Simple::Iterator/;
  23         69  
  23         3112  
111              
112             1;
113             __END__