Show
Ignore:
Timestamp:
04/25/08 10:18:14 (5 years ago)
Author:
tokuhirom
Message:

added function condition

Location:
lang/perl/HTTPx-Dispatcher/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/HTTPx-Dispatcher/trunk/lib/HTTPx/Dispatcher/Rule.pm

    r10390 r10394  
    7474    my ($self, $req) = @_; 
    7575 
    76     $self->condition_check_method($req); 
     76    $self->condition_check_method($req) && $self->condition_check_function($req); 
    7777} 
    7878 
    7979sub condition_check_method { 
    8080    my ($self, $req) = @_; 
     81    croak "request required" unless blessed $req; 
    8182 
    8283    my $method = $self->conditions->{method}; 
     
    9293} 
    9394 
     95sub condition_check_function { 
     96    my ($self, $req) = @_; 
     97    croak "request required" unless blessed $req; 
     98 
     99    my $function = $self->conditions->{function}; 
     100    return 1 unless $function; 
     101 
     102    local $_ = $req; 
     103    if ( $function->( $req ) ) { 
     104        return 1; 
     105    } else { 
     106        return 0; 
     107    } 
     108} 
     109 
    941101; 
    95111 
  • lang/perl/HTTPx-Dispatcher/trunk/t/01_simple.t

    r10390 r10394  
    153153controller: user 
    154154 
     155=== function condition(1) 
     156--- input 
     157src: |+ 
     158    connect 'edit' => { 
     159        conditions => { 
     160            function => sub { $_->method =~ /get/i }, 
     161        }, 
     162        controller => 'user', 
     163        action => 'get_root', 
     164    }; 
     165    connect 'edit' => { 
     166        conditions => { 
     167            function => sub { $_->method =~ /post/i }, 
     168        }, 
     169        controller => 'user', 
     170        action => 'post_root', 
     171    }; 
     172uri: /edit 
     173method: POST 
     174--- expected 
     175action: post_root 
     176controller: user 
     177 
     178=== function condition(2) 
     179--- input 
     180src: |+ 
     181    connect 'edit' => { 
     182        conditions => { 
     183            function => sub { $_->method =~ /get/i }, 
     184        }, 
     185        controller => 'user', 
     186        action => 'get_root', 
     187    }; 
     188    connect 'edit' => { 
     189        conditions => { 
     190            function => sub { $_->method =~ /post/i }, 
     191        }, 
     192        controller => 'user', 
     193        action => 'post_root', 
     194    }; 
     195uri: /edit 
     196method: GET 
     197--- expected 
     198action: get_root 
     199controller: user 
     200