Changeset 46

Show
Ignore:
Timestamp:
02/08/06 16:14:17
Author:
miyagawa
Message:

support rule_op like AND, OR, XOR, NAND and NOR. Default is AND

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/lib/Plagger/Plugin.pm

    r7 r46  
    1010        conf => $opt->{config} || {}, 
    1111        rule => $opt->{rule}, 
     12        rule_op => $opt->{rule_op} || 'AND', 
    1213        stash => {}, 
    1314    }, $class; 
     
    2021    if (my $rule = $self->{rule}) { 
    2122        $rule = [ $rule ] if ref($rule) eq 'HASH'; 
    22         $self->{rule} = Plagger::Rule::Compound->new(@$rule); 
     23        my $op = $self->{rule_op}; 
     24        $self->{rule} = Plagger::Rule::Compound->new($op, @$rule); 
    2325    } else { 
    2426        $self->{rule} = Plagger::Rule->new({ module => 'Always' }); 
  • trunk/plagger/lib/Plagger/Rule/Compound.pm

    r36 r46  
    22use strict; 
    33 
     4use List::Util qw(reduce); 
     5 
     6our %Ops = ( 
     7    AND  => [ sub { $_[0] && $_[1] } ], 
     8    OR   => [ sub { $_[0] || $_[1] } ], 
     9    XOR  => [ sub { $_[0] xor $_[1] }, 0 ], 
     10    NAND => [ sub { $_[0] && $_[1] }, 1 ], 
     11    NOR  => [ sub { $_[0] || $_[1] }, 1 ], 
     12); 
     13 
    414sub new { 
    5     my($class, @rules) = @_; 
     15    my($class, $op, @rules) = @_; 
     16    my $ops_sub = $Ops{uc($op)} 
     17        or Plagger->context->error("operator $op not supported"); 
     18 
    619    bless { 
     20        ops_sub => $ops_sub->[0], 
     21        ops_not => $ops_sub->[1], 
    722        rules => [ map Plagger::Rule->new($_), @rules ], 
    823    }, $class; 
     
    1227    my($self, $args) = @_; 
    1328 
    14     my $bool = 1
     29    my @bool
    1530    for my $rule (@{ $self->{rules} }) { 
    16         $bool = 0 unless $rule->dispatch($args); # AND mode 
     31        push @bool, ($rule->dispatch($args) ? 1 : 0); 
    1732    } 
    1833 
     34    my $bool = reduce { $self->{ops_sub}->($a, $b) } @bool; 
     35    $bool = !$bool if $self->{ops_not}; 
    1936    $bool; 
    2037}