Show
Ignore:
Timestamp:
06/03/08 22:52:28 (5 years ago)
Author:
tomyhero
Message:

lang/perl/FormValidator-LazyWay?: refactoring Rule.pm

Location:
lang/perl/FormValidator-LazyWay/branches/rebuild
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/FormValidator-LazyWay/branches/rebuild/lib/FormValidator/LazyWay/Rule.pm

    r13091 r13141  
    5656    foreach my $field ( keys %{ $config->{constraints}{$type} } ) { 
    5757        my $validations = $config->{constraints}{$type}{$field}; 
    58  
     58        $constraints->{$field} = []; 
    5959        for my $validation ( @{$validations} ) { 
    6060 
     
    7474            if ( $alias->{$package} ) { 
    7575                $package = $alias->{$package}; 
     76                ( $label  ) = $package =~ m/^FormValidator::LazyWay::Rule::(.+)/; 
     77                $label = '+' . $package unless $label; 
     78                $label = $label . '::' . $method ; 
    7679            } 
    7780            else { 
    78                 my $prefix = 'dfv'; 
    79                 if ( $package =~ s/^\+// ) { 
    80                     $prefix = $self->{prefix} || 'dfv'; 
    81                 } 
    82                 else { 
    83                     $package = __PACKAGE__ . '::' . $package; 
    84                 } 
    85  
     81                $package = __PACKAGE__ . '::' . $package; 
    8682            } 
    8783 
     
    9086            $self->{args}{$type}{$field}{$label} = $args if keys %{$args}; 
    9187 
    92             $constraints->{$field}{$sub}{method} = sub { 
     88            my $method_ref = sub { 
    9389                my $item = shift; 
    9490                no strict; 
     
    9692                return $result; 
    9793            }; 
    98  
    99             $constraints->{$field}{$sub}{label} = $label; 
     94            push ( @{$constraints->{$field}} , { label => $label , method => $method_ref } ); 
    10095 
    10196        } 
     
    105100    return $constraints; 
    106101} 
     102 
    1071031; 
    108104 
     
    113109=head1 DESCRIPTION 
    114110 
    115 Base Rules. 
     111検証モジュールを読み込み、検証ルールハッシュを作成します。 
    116112 
    117113=head1 METHODS 
  • lang/perl/FormValidator-LazyWay/branches/rebuild/t/rule.t

    r13091 r13141  
    88use lib File::Spec->catfile( $FindBin::Bin, 'lib' ); 
    99 
    10 plan tests => 1 * blocks; 
     10plan tests => 14 * blocks; 
    1111 
    1212run { 
     
    1414    my $config = Load( $block->yaml ); 
    1515    my $rule   = FormValidator::LazyWay::Rule->new( config => $config ); 
    16     cmp_deeply( $rule->constraints , $block->result_constraints ) ; 
     16 
     17    is( $rule->constraints->{strict}{oppai}[0]{label} , $block->strict_oppai0 ); 
     18    is( $rule->constraints->{strict}{oppai}[1]{label} , $block->strict_oppai1 ); 
     19    is( $rule->constraints->{strict}{email}[0]{label} , $block->strict_email ); 
     20    is( $rule->constraints->{strict}{email_mx}[0]{label} , $block->strict_email_mx ); 
     21    is( $rule->constraints->{loose}{email}[0]{label} , $block->loose_email ); 
     22    is( $rule->constraints->{loose}{email_mx}[0]{label} , $block->loose_email_mx ); 
     23 
     24    is ( ref $rule->constraints->{strict}{oppai}[0]{method} , 'CODE' ); 
     25    is ( ref $rule->constraints->{strict}{oppai}[1]{method} , 'CODE' ); 
     26    is ( ref $rule->constraints->{strict}{email}[0]{method} , 'CODE' ); 
     27    is ( ref $rule->constraints->{strict}{email_mx}[0]{method} , 'CODE' ); 
     28    is ( ref $rule->constraints->{loose}{email}[0]{method} , 'CODE' ); 
     29    is ( ref $rule->constraints->{loose}{email_mx}[0]{method} , 'CODE' ); 
     30 
     31    is( $rule->args->{strict}{email_mx}{'Email::email'}{'-mxcheck'} , 1 ); 
     32    is( $rule->args->{loose}{email_mx}{'Email::email_loose'}{'-mxcheck'} , 1 ); 
    1733} 
    1834 
     
    2642  strict : 
    2743    oppai : 
     44      - Email::email  
    2845      - +MyRule::Oppai::name 
    2946    email : 
    30       - Email::email : 
     47      - Email::email  
    3148    email_mx : 
    3249      - Email::email : 
     
    3552  loose : 
    3653    email : 
    37       - Email::email_loose : 
     54      - Email::email_loose  
    3855    email_mx : 
    3956      - Email::email_loose : 
    4057          args: 
    4158            -mxcheck: 1 
    42 --- result_constraints  eval 
    43 { 
    44     'strict' => { 
    45         'email' => { 
    46             'FormValidator::LazyWay::Rule::Email::email' => { 
    47                 'label'  => 'Email::email', 
    48                 'method' => sub {"DUMMY"} 
    49             } 
    50         }, 
    51         'oppai' => { 
    52             'MyRule::Oppai::name' => { 
    53                 'label'  => '+MyRule::Oppai::name', 
    54                 'method' => sub {"DUMMY"} 
    55             } 
    56         }, 
    57         'email_mx' => { 
    58             'FormValidator::LazyWay::Rule::Email::email' => { 
    59                 'label'  => 'Email::email', 
    60                 'method' => sub {"DUMMY"} 
    61             } 
    62         } 
    63     }, 
    64     'loose' => { 
    65         'email' => { 
    66             'FormValidator::LazyWay::Rule::Email::email_loose' => { 
    67                 'label'  => 'Email::email_loose', 
    68                 'method' => sub {"DUMMY"} 
    69             } 
    70         }, 
    71         'email_mx' => { 
    72             'FormValidator::LazyWay::Rule::Email::email_loose' => { 
    73                 'label'  => 'Email::email_loose', 
    74                 'method' => sub {"DUMMY"} 
    75             } 
    76         } 
    77     } 
    78 } 
     59--- strict_email chomp 
     60Email::email 
     61--- strict_oppai0 chomp 
     62Email::email 
     63--- strict_oppai1 chomp 
     64+MyRule::Oppai::name 
     65--- strict_email_mx chomp 
     66Email::email 
     67--- loose_email chomp 
     68Email::email_loose 
     69--- loose_email_mx chomp 
     70Email::email_loose 
    7971=== alialias 
    8072--- yaml 
     
    8577  strict : 
    8678    oppai : 
     79      - Email::email  
    8780      - oppai::name 
    8881    email : 
     
    9992          args: 
    10093            -mxcheck: 1 
    101 --- result_constraints  eval 
    102 { 
    103     'strict' => { 
    104         'email' => { 
    105             'FormValidator::LazyWay::Rule::Email::email' => { 
    106                 'label'  => 'Email::email', 
    107                 'method' => sub {"DUMMY"} 
    108             } 
    109         }, 
    110         'oppai' => { 
    111             'MyRule::Oppai::name' => { 
    112                 'label'  => '+MyRule::Oppai::name', 
    113                 'method' => sub {"DUMMY"} 
    114             } 
    115         }, 
    116         'email_mx' => { 
    117             'FormValidator::LazyWay::Rule::Email::email' => { 
    118                 'label'  => 'Email::email', 
    119                 'method' => sub {"DUMMY"} 
    120             } 
    121         } 
    122     }, 
    123     'loose' => { 
    124         'email' => { 
    125             'FormValidator::LazyWay::Rule::Email::email_loose' => { 
    126                 'label'  => 'Email::email_loose', 
    127                 'method' => sub {"DUMMY"} 
    128             } 
    129         }, 
    130         'email_mx' => { 
    131             'FormValidator::LazyWay::Rule::Email::email_loose' => { 
    132                 'label'  => 'Email::email_loose', 
    133                 'method' => sub {"DUMMY"} 
    134             } 
    135         } 
    136     } 
    137 } 
     94--- strict_email chomp 
     95Email::email 
     96--- strict_oppai0 chomp 
     97Email::email 
     98--- strict_oppai1 chomp 
     99+MyRule::Oppai::name 
     100--- strict_email_mx chomp 
     101Email::email 
     102--- loose_email chomp 
     103Email::email_loose 
     104--- loose_email_mx chomp 
     105Email::email_loose