[Bps-public-commit] RT-Condition-Complex branch, master, updated. 9ef5ef11e3f1d82f331e8f3941913a537c75672b

Ruslan Zakirov ruz at bestpractical.com
Mon Nov 9 21:26:55 EST 2009


The branch, master has been updated
       via  9ef5ef11e3f1d82f331e8f3941913a537c75672b (commit)
       via  36032f8b5b0d0a886ef32a30fb1c14d1a2eb8ecf (commit)
       via  4909e160411b4c7e62766b6ea44e08e52b4f82e7 (commit)
       via  59a036d1fed1b51ba1f377a22f77f2b01175f5d1 (commit)
      from  d2d7a80f30ede2a56e3e20ae2a83239f6439e883 (commit)

Summary of changes:
 .gitignore                  |    1 +
 lib/RT/Condition/Complex.pm |  144 +++++++++++++++++++++++++++++++++----------
 2 files changed, 113 insertions(+), 32 deletions(-)

- Log -----------------------------------------------------------------
commit 59a036d1fed1b51ba1f377a22f77f2b01175f5d1
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Fri Nov 6 00:37:36 2009 +0700

    gitignore

diff --git a/.gitignore b/.gitignore
index cf9e15e..fcd100d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@ Makefile
 blib/
 pm_to_blib
 META.yml
+MANIFEST.bak

commit 4909e160411b4c7e62766b6ea44e08e52b4f82e7
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Mon Nov 9 13:57:33 2009 +0300

    Describe method with localization

diff --git a/lib/RT/Condition/Complex.pm b/lib/RT/Condition/Complex.pm
index 7cabdb8..28299f4 100644
--- a/lib/RT/Condition/Complex.pm
+++ b/lib/RT/Condition/Complex.pm
@@ -238,6 +238,78 @@ sub SolveCondition {
     );
 }
 
+sub DescribeTree {
+    my $self = shift;
+    my $tree = shift;
+
+    my $res = '';
+    $parser->walk(
+        $tree,
+        {
+            open_paren  => sub { $res .= '(' },
+            close_paren => sub { $res .= ')' },
+            operator    => sub { $res .= ' '. $_[1]->loc($_[0]) .' ' },
+            operand     => sub { 
+                my $cond = shift;
+                my $self = shift;
+                my $str = '';
+                if ( $cond->{'op'} ) {
+                    my $qv = $cond->{'rhs'};
+                    if ( $cond->{'op'} eq '=' ) {
+                        $str = $self->loc('[_1] is equal to [_2]', $cond->{'lhs'}, $qv);
+                    }
+                    elsif ( $cond->{'op'} eq '!=' ) {
+                        $str = $self->loc('[_1] is not equal to [_2]', $cond->{'lhs'}, $qv);
+                    }
+                    elsif ( $cond->{'op'} eq '>' ) {
+                        $str = $self->loc('[_1] is greater than [_2]', $cond->{'lhs'}, $qv);
+                    }
+                    elsif ( $cond->{'op'} eq '>=' ) {
+                        $str = $self->loc('[_1] is equal or greater than [_2]', $cond->{'lhs'}, $qv);
+                    }
+                    elsif ( $cond->{'op'} eq '<' ) {
+                        $str = $self->loc('[_1] is smaller than [_2]', $cond->{'lhs'}, $qv);
+                    }
+                    elsif ( $cond->{'op'} eq '<=' ) {
+                        $str = $self->loc('[_1] is smaller or greater than [_2]', $cond->{'lhs'}, $qv);
+                    }
+                    elsif ( $cond->{'op'} eq 'contains' ) {
+                        $str = $self->loc('[_1] contains [_2]', $cond->{'lhs'}, $qv);
+                    }
+                    elsif ( $cond->{'op'} eq 'not contains' ) {
+                        $str = $self->loc("[_1] doesn't contain [_2]", $cond->{'lhs'}, $qv);
+                    }
+                    elsif ( $cond->{'op'} eq 'starts with' ) {
+                        $str = $self->loc('[_1] starts with [_2]', $cond->{'lhs'}, $qv);
+                    }
+                    elsif ( $cond->{'op'} eq 'not starts with' ) {
+                        $str = $self->loc("[_1] doesn't start with [_2]", $cond->{'lhs'}, $qv);
+                    }
+                    elsif ( $cond->{'op'} eq 'ends with' ) {
+                        $str = $self->loc('[_1] ends with [_2]', $cond->{'lhs'}, $qv);
+                    }
+                    elsif ( $cond->{'op'} eq 'not ends with' ) {
+                        $str = $self->loc("[_1] doesn't end with [_2]", $cond->{'lhs'}, $qv);
+                    }
+                    elsif ( $cond->{'op'} eq 'is null' ) {
+                        $str = $self->loc('[_1] is empty', $cond->{'lhs'});
+                    }
+                    elsif ( $cond->{'op'} eq 'is not null' ) {
+                        $str = $self->loc('[_1] is not empty', $cond->{'lhs'});
+                    }
+                    else {
+                        $str = $self->loc("[_1] $cond->{op} [_2]", $cond->{'lhs'}, $qv);
+                    }
+                } else {
+                }
+                $res .= $str;
+            },
+        },
+        $self
+    );
+    return $res;
+}
+
 sub ParseCode {
     my $self = shift;
 

commit 36032f8b5b0d0a886ef32a30fb1c14d1a2eb8ecf
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Mon Nov 9 14:00:43 2009 +0300

    move more things into Solve method and make code more abstract

diff --git a/lib/RT/Condition/Complex.pm b/lib/RT/Condition/Complex.pm
index 28299f4..9487865 100644
--- a/lib/RT/Condition/Complex.pm
+++ b/lib/RT/Condition/Complex.pm
@@ -170,54 +170,62 @@ my %op_handler = (
 
 sub IsApplicable {
     my $self = shift;
-    my ($tree, @errors) = $self->ParseCode;
-    unless ( $tree ) {
-        $RT::Logger->error(
-            "Couldn't parse complex condition, errors:\n"
-            . join("\n", map "\t* $_", @errors)
-            . "\nCODE:\n"
-            . $self->ScripObj->CustomIsApplicableCode
-        );
-        return 0;
-    }
-    return $self->Solve( $tree );
+
+    return ($self->Solve(
+        ''     => $self->TransactionObj,
+        Ticket => $self->TicketObj,
+        @_,
+    ))[0];
 }
 
 my $solver = sub {
     my $cond = shift;
-    my $self = $_[0];
+    my $self = shift;
+    my $res;
     if ( $cond->{'op'} ) {
-        return $self->SolveCondition(
-            Field       => $cond->{'lhs'},
-            Operator    => $cond->{'op'},
-            Value       => $self->GetValue( $cond->{'rhs'}, @_ ),
-            Transaction => $_[1],
-            Ticket      => $_[2],
+        $res = $self->SolveCondition(
+            Field     => $cond->{'lhs'},
+            Operator  => $cond->{'op'},
+            Value     => $self->GetValue( $cond->{'rhs'}, @_ ),
+            Arguments => \@_,
         );
     }
     elsif ( $cond->{'module'} ) {
         my $module = 'RT::Condition::'. $cond->{'module'};
         eval "require $module;1" || die "Require of $module failed.\n$@\n";
+        my %rest = @_;
         my $obj = $module->new (
-            TransactionObj => $_[1],
-            TicketObj      => $_[2],
+            TransactionObj => $rest{'Transaction'},
+            TicketObj      => $rest{'Ticket'},
             Argument       => $cond->{'argument'},
             CurrentUser    => $RT::SystemUser,
         );
-        return $obj->IsApplicable;
+        $res = $obj->IsApplicable;
     } else {
         die "Boo";
     }
+    return undef unless $res;
+    return $res;
 };
 
 sub Solve {
     my $self = shift;
-    my $tree = shift;
+    my %args = @_%2? (Tree => @_) : (@_);
 
-    my $txn = $self->TransactionObj;
-    my $ticket = $self->TicketObj;
+    my ($tree, @errors) = $self->ParseCode( $args{'Tree'} );
+    unless ( $tree ) {
+        $RT::Logger->error(
+            "Couldn't parse complex condition, errors:\n"
+            . join("\n", map "\t* $_", @errors)
+            . "\nCODE:\n" . $args{'Tree'}
+        );
+        return 0;
+    }
+
+    my $solution = $parser->partial_solve( $tree, $solver, $self, %args );
+    return $solution unless ref $solution;
 
-    return $parser->solve( $tree, $solver, $self, $txn, $ticket );
+    return (0, $solution, $self->DescribeTree( $solution ));
 }
 
 sub SolveCondition {
@@ -227,13 +235,10 @@ sub SolveCondition {
     my $op_handler = $self->OpHandler( $args{'Operator'} );
     my $value = $args{'Value'};
     my $checker = sub { return $op_handler->( $_[0], $value ) };
-    
+
     return RT::Extension::ColumnMap->Check(
-        String => $args{'Field'},
-        Objects => {
-            '' => $args{'Transaction'},
-            'Ticket' => $args{'Ticket'},
-        },
+        String  => $args{'Field'},
+        Objects => { @{ $args{'Arguments'} } },
         Checker => $checker,
     );
 }

commit 9ef5ef11e3f1d82f331e8f3941913a537c75672b
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Mon Nov 9 14:01:38 2009 +0300

    ParseCode now takes text of the condition as argument

diff --git a/lib/RT/Condition/Complex.pm b/lib/RT/Condition/Complex.pm
index 9487865..e33191c 100644
--- a/lib/RT/Condition/Complex.pm
+++ b/lib/RT/Condition/Complex.pm
@@ -317,8 +317,11 @@ sub DescribeTree {
 
 sub ParseCode {
     my $self = shift;
+    my $code = shift;
+    return $code if ref $code;
 
-    my $code = $self->ScripObj->CustomIsApplicableCode;
+    $code = $self->ScripObj->CustomIsApplicableCode
+        unless defined $code;
 
     my @errors = ();
     my $res = $parser->as_array(

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list