| 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 | 108 |  |  | 108 |  | 68852 | use strict; | 
|  | 108 |  |  |  |  | 198 |  | 
|  | 108 |  |  |  |  | 2762 |  | 
| 6 | 108 |  |  | 108 |  | 526 | use warnings; | 
|  | 108 |  |  |  |  | 195 |  | 
|  | 108 |  |  |  |  | 2784 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 108 |  |  | 108 |  | 595 | use base 'Validation::Class::Directive'; | 
|  | 108 |  |  |  |  | 176 |  | 
|  | 108 |  |  |  |  | 7818 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 108 |  |  | 108 |  | 571 | use Validation::Class::Util; | 
|  | 108 |  |  |  |  | 238 |  | 
|  | 108 |  |  |  |  | 691 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our $VERSION = '7.900057'; # 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 | 78 | my ($self, $proto, $field, $param) = @_; | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 32 | 50 |  |  |  | 103 | if (defined $field->{toggle}) { | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 32 |  | 100 |  |  | 123 | my $stash = $proto->stash->{'directive.toggle'} ||= {}; | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | # to be restored after validation | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | $stash->{$field->{name}}->{'required'} = | 
| 30 | 32 | 100 |  |  |  | 209 | defined $field->{required} ? $field->{required} == 0 ? 0 : 1 : 0; | 
|  |  | 50 |  |  |  |  |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 32 | 100 |  |  |  | 577 | $field->{required} = 1 if ($field->{toggle} =~ /^(\+|1)$/); | 
| 33 | 32 | 100 |  |  |  | 138 | $field->{required} = 0 if ($field->{toggle} =~ /^(\-|0)$/); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 32 |  |  |  |  | 117 | return $self; | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub after_validation { | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 32 |  |  | 32 | 0 | 72 | my ($self, $proto, $field, $param) = @_; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 32 | 50 |  |  |  | 108 | if (defined $field->{toggle}) { | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 32 |  | 50 |  |  | 123 | my $stash = $proto->stash->{'directive.toggle'} ||= {}; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 32 | 50 |  |  |  | 132 | if (defined $stash->{$field->{name}}->{'required'}) { | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | # restore field state from stash after validation | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 32 |  |  |  |  | 78 | $field->{required} = $stash->{$field->{name}}->{'required'}; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 32 |  |  |  |  | 104 | delete $stash->{$field->{name}}; | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 32 | 50 |  |  |  | 103 | delete $field->{toggle} if exists $field->{toggle}; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 32 |  |  |  |  | 111 | return $self; | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub normalize { | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 1002 |  |  | 1002 | 0 | 1842 | my ($self, $proto, $field, $param) = @_; | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | # on normalize, always remove the toggle directive | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 1002 | 50 |  |  |  | 2758 | delete $field->{toggle} if exists $field->{toggle}; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 1002 |  |  |  |  | 2848 | return $self; | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | 1; | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | __END__ |