| 1 | package FormValidator::LazyWay::Rule; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | use UNIVERSAL::require; |
|---|
| 6 | use Moose; |
|---|
| 7 | |
|---|
| 8 | has alias => ( is => 'ro' , isa => 'HashRef' ); |
|---|
| 9 | has args => ( is => 'ro' , isa => 'HashRef' ); |
|---|
| 10 | has config => ( is => 'ro' , isa => 'HashRef' ); |
|---|
| 11 | has constraints => ( is => 'ro' , isa => 'HashRef' ); |
|---|
| 12 | |
|---|
| 13 | sub BUILD { |
|---|
| 14 | my $self = shift; |
|---|
| 15 | $self->_load_rules(); |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | sub _load_rules { |
|---|
| 19 | my $self = shift; |
|---|
| 20 | my $rules = $self->{config}{rules}; |
|---|
| 21 | |
|---|
| 22 | for my $rule ( @{$rules} ) { |
|---|
| 23 | my $module = $rule; |
|---|
| 24 | |
|---|
| 25 | my @data = split( '=', $rule ); |
|---|
| 26 | my $alias; |
|---|
| 27 | if ( scalar @data == 1 ) { |
|---|
| 28 | $module = $rule; |
|---|
| 29 | } |
|---|
| 30 | else { |
|---|
| 31 | $alias = $data[0]; |
|---|
| 32 | $module = $data[1]; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | unless ( $module =~ s/^\+// ) { |
|---|
| 36 | $module = join( '::', __PACKAGE__, $module ); |
|---|
| 37 | } |
|---|
| 38 | $self->{alias}{$alias} = $module if $alias; |
|---|
| 39 | |
|---|
| 40 | $module->require or die $@; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | foreach my $key ( keys %{ $self->config->{constraints} } ) { |
|---|
| 44 | $self->{constraints}{$key} |
|---|
| 45 | = $self->_make_constraints( $key ); |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | sub _make_constraints { |
|---|
| 50 | my $self = shift; |
|---|
| 51 | my $type = shift; |
|---|
| 52 | my $alias = $self->alias; |
|---|
| 53 | my $constraints = {}; |
|---|
| 54 | my $config = $self->config; |
|---|
| 55 | |
|---|
| 56 | foreach my $field ( keys %{ $config->{constraints}{$type} } ) { |
|---|
| 57 | my $validations = $config->{constraints}{$type}{$field}; |
|---|
| 58 | |
|---|
| 59 | for my $validation ( @{$validations} ) { |
|---|
| 60 | |
|---|
| 61 | my $label; |
|---|
| 62 | my $args = {}; |
|---|
| 63 | if ( ref $validation eq 'HASH' ) { |
|---|
| 64 | ( $label, $args ) = each %{$validation}; |
|---|
| 65 | $args = $args->{args}; |
|---|
| 66 | } |
|---|
| 67 | else { |
|---|
| 68 | $label = $validation; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | my $package = substr( $label, 0, rindex( $label, '::' ) ); |
|---|
| 72 | my $method = substr( $label, rindex( $label, '::' ) + 2 ); |
|---|
| 73 | |
|---|
| 74 | if ( $alias->{$package} ) { |
|---|
| 75 | $package = $alias->{$package}; |
|---|
| 76 | } |
|---|
| 77 | else { |
|---|
| 78 | my $prefix = 'dfv'; |
|---|
| 79 | if ( $package =~ s/^\+// ) { |
|---|
| 80 | $prefix = $self->{prefix} || 'dfv'; |
|---|
| 81 | } |
|---|
| 82 | else { |
|---|
| 83 | $package = __PACKAGE__ . '::' . $package; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | my $sub = $package . '::' . $method; |
|---|
| 89 | |
|---|
| 90 | $self->{args}{$type}{$field}{$label} = $args if keys %{$args}; |
|---|
| 91 | |
|---|
| 92 | $constraints->{$field}{$sub}{method} = sub { |
|---|
| 93 | my $item = shift; |
|---|
| 94 | no strict; |
|---|
| 95 | my $result = $sub->( $item, $args ); |
|---|
| 96 | return $result; |
|---|
| 97 | }; |
|---|
| 98 | |
|---|
| 99 | $constraints->{$field}{$sub}{label} = $label; |
|---|
| 100 | |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | return $constraints; |
|---|
| 106 | } |
|---|
| 107 | 1; |
|---|
| 108 | |
|---|
| 109 | =head1 NAME |
|---|
| 110 | |
|---|
| 111 | FormValidator::LazyWay::Rule - FormValidator::LazyWay Validation Rule module. |
|---|
| 112 | |
|---|
| 113 | =head1 DESCRIPTION |
|---|
| 114 | |
|---|
| 115 | Base Rules. |
|---|
| 116 | |
|---|
| 117 | =head1 METHODS |
|---|
| 118 | |
|---|
| 119 | =head2 new |
|---|
| 120 | |
|---|
| 121 | =head2 alias |
|---|
| 122 | |
|---|
| 123 | =head2 args |
|---|
| 124 | |
|---|
| 125 | =head2 constraints |
|---|
| 126 | |
|---|
| 127 | =head1 AUTHOR |
|---|
| 128 | |
|---|
| 129 | Tomohiro Teranishi <tomohiro.teranishi@gmail.com> |
|---|
| 130 | |
|---|
| 131 | =cut |
|---|