File Coverage

blib/lib/Validation/Class/Directive/Toggle.pm
Criterion Covered Total %
statement 30 30 100.0
branch 12 18 66.6
condition 3 4 75.0
subroutine 7 7 100.0
pod 0 3 0.0
total 52 62 83.8


line stmt bran cond sub pod time code
1             # ABSTRACT: Toggle Directive for Validation Class Field Definitions
2              
3             package Validation::Class::Directive::Toggle;
4              
5 109     109   52899 use strict;
  109         282  
  109         3177  
6 109     109   587 use warnings;
  109         269  
  109         2800  
7              
8 109     109   584 use base 'Validation::Class::Directive';
  109         253  
  109         10848  
9              
10 109     109   808 use Validation::Class::Util;
  109         271  
  109         642  
11              
12             our $VERSION = '7.900059'; # VERSION
13              
14              
15             has 'mixin' => 0;
16             has 'field' => 1;
17             has 'multi' => 0;
18              
19             sub before_validation {
20              
21 32     32 0 99 my ($self, $proto, $field, $param) = @_;
22              
23 32 50       97 if (defined $field->{toggle}) {
24              
25 32   100     95 my $stash = $proto->stash->{'directive.toggle'} ||= {};
26              
27             # to be restored after validation
28              
29             $stash->{$field->{name}}->{'required'} =
30 32 100       192 defined $field->{required} ? $field->{required} == 0 ? 0 : 1 : 0;
    50          
31              
32 32 100       268 $field->{required} = 1 if ($field->{toggle} =~ /^(\+|1)$/);
33 32 100       154 $field->{required} = 0 if ($field->{toggle} =~ /^(\-|0)$/);
34              
35             }
36              
37 32         89 return $self;
38              
39             }
40              
41             sub after_validation {
42              
43 32     32 0 128 my ($self, $proto, $field, $param) = @_;
44              
45 32 50       125 if (defined $field->{toggle}) {
46              
47 32   50     107 my $stash = $proto->stash->{'directive.toggle'} ||= {};
48              
49 32 50       171 if (defined $stash->{$field->{name}}->{'required'}) {
50              
51             # restore field state from stash after validation
52              
53 32         100 $field->{required} = $stash->{$field->{name}}->{'required'};
54              
55 32         116 delete $stash->{$field->{name}};
56              
57             }
58              
59             }
60              
61 32 50       113 delete $field->{toggle} if exists $field->{toggle};
62              
63 32         88 return $self;
64              
65             }
66              
67             sub normalize {
68              
69 1009     1009 0 2506 my ($self, $proto, $field, $param) = @_;
70              
71             # on normalize, always remove the toggle directive
72              
73 1009 50       2644 delete $field->{toggle} if exists $field->{toggle};
74              
75 1009         2440 return $self;
76              
77             }
78              
79             1;
80              
81             __END__