[Rt-commit] r18849 - in rt/3.999/branches/lorzy: lib/RT lib/RT/Lorzy lib/RT/ScripAction t/api

clkao at bestpractical.com clkao at bestpractical.com
Thu Mar 19 08:35:54 EDT 2009


Author: clkao
Date: Thu Mar 19 08:35:53 2009
New Revision: 18849

Modified:
   rt/3.999/branches/lorzy/lib/RT/Bootstrap.pm
   rt/3.999/branches/lorzy/lib/RT/Lorzy.pm
   rt/3.999/branches/lorzy/lib/RT/Lorzy/Dispatcher.pm
   rt/3.999/branches/lorzy/lib/RT/Lorzy/Package/RT.pm
   rt/3.999/branches/lorzy/lib/RT/Rule.pm
   rt/3.999/branches/lorzy/lib/RT/Ruleset.pm
   rt/3.999/branches/lorzy/lib/RT/ScripAction.pm
   rt/3.999/branches/lorzy/lib/RT/ScripAction/SendEmail.pm
   rt/3.999/branches/lorzy/t/api/action-createtickets.t
   rt/3.999/branches/lorzy/t/api/rules-preview.t

Log:
- refactor lorzy::dispatcher into separate factory class to simplify
  the rules integration.
- make sendemail hint via the hint api.


Modified: rt/3.999/branches/lorzy/lib/RT/Bootstrap.pm
==============================================================================
--- rt/3.999/branches/lorzy/lib/RT/Bootstrap.pm	(original)
+++ rt/3.999/branches/lorzy/lib/RT/Bootstrap.pm	Thu Mar 19 08:35:53 2009
@@ -451,6 +451,7 @@
                 $item->{scrip_condition},
                 $item->{scrip_action},
                 $item->{template},
+                $item->{description},
             );
 
             RT::Lorzy::Dispatcher->add_rule( $rule );

Modified: rt/3.999/branches/lorzy/lib/RT/Lorzy.pm
==============================================================================
--- rt/3.999/branches/lorzy/lib/RT/Lorzy.pm	(original)
+++ rt/3.999/branches/lorzy/lib/RT/Lorzy.pm	Thu Mar 19 08:35:53 2009
@@ -4,8 +4,9 @@
 
 use RT::Ruleset;
 use Lorzy::Evaluator;
+use RT::Lorzy::Dispatcher;
 
-RT::Ruleset->add( name => 'Lorzy', rules => ['RT::Lorzy::Dispatcher'] );
+RT::Ruleset->register( RT::Lorzy::Dispatcher->new );
 our $EVAL = Lorzy::Evaluator->new();
 $EVAL->load_package($_) for qw(Str Native);
 $EVAL->load_package('RT', 'RT::Lorzy::Package::RT');
@@ -17,7 +18,7 @@
 }
 
 sub create_scripish {
-    my ( $class, $scrip_condition, $scrip_action, $template, $queue ) = @_;
+    my ( $class, $scrip_condition, $scrip_action, $template, $description, $queue ) = @_;
     my $sigs = { ticket => Lorzy::FunctionArgument->new( name => 'ticket', type => 'RT::Model::Ticket' ),
         transaction => Lorzy::FunctionArgument->new( name => 'transaction', type => 'RT::Model::Transaction' ) };
     my $builder = Lorzy::Builder->new();
@@ -70,23 +71,24 @@
                     name     => $scrip_action,
                     template => $template,
                     ticket => { name => 'Symbol', args => { symbol => 'ticket' } },
-                    callback => { name => 'Symbol', args => { symbol => 'callback' } },
                     transaction => { name => 'Symbol', args => { symbol => 'transaction' } },
                     } } ],
-        signature => {%$sigs,
-                      callback => Lorzy::FunctionArgument->new( name => 'callback', type => 'CODE' ) } );
+        signature => $sigs );
 
-    RT::Lorzy::Rule->new(
+    RT::Lorzy::RuleFactory->make_factory(
         { condition     => $condition,
           collect_hints => $hints,
-          action        => $action } )
+          action        => $action,
+          description   => $description,
+          _stage        => 'transaction_create',
+      } )
 }
 
-package RT::Lorzy::Rule;
+package RT::Lorzy::RuleFactory;
 use base 'Class::Accessor::Fast';
-__PACKAGE__->mk_accessors(qw(condition action collect_hints));
+__PACKAGE__->mk_accessors(qw(description condition action collect_hints _stage));
 
-sub new {
+sub make_factory {
     my $class = shift;
     my $self = $class->SUPER::new(@_);
     if (ref($self->action) eq 'CODE') {
@@ -101,20 +103,48 @@
     return $self;
 }
 
-sub on_condition {
-    my ($self, $ticket_obj, $transaction_obj) = @_;
-    return RT::Lorzy->evaluate( $self->condition, ticket => $ticket_obj, transaction => $transaction_obj);
+sub new {
+    my $self = shift;
+    return RT::Lorzy::Rule->new( @_, factory => $self);
 }
 
+package RT::Lorzy::Rule;
+use base 'RT::Rule';
+use base 'Class::Accessor::Fast';
+
+__PACKAGE__->mk_accessors(qw(factory));
+sub _init {
+    my $self = shift;
+    my %args = @_;
+    $self->SUPER::_init(%args);
+    $self->factory($args{factory});
+}
+
+sub prepare {
+    my ($self, %args) = @_;
+    return RT::Lorzy->evaluate( $self->factory->condition,
+                                ticket => $self->ticket_obj,
+                                transaction => $self->transaction);
+}
+
+sub description { $_[0]->factory->description }
+
 sub hints {
-    my ($self, $ticket_obj, $transaction_obj, $hints) = @_;
-    return unless $self->collect_hints;
-    return RT::Lorzy->evaluate( $self->collect_hints, ticket => $ticket_obj, transaction => $transaction_obj, callback => $hints);
+    my ($self) = @_;
+    return unless $self->factory->collect_hints;
+    my $hints = RT::Lorzy->evaluate( $self->factory->collect_hints,
+                                     ticket => $self->ticket_obj,
+                                     transaction => $self->transaction);
+
+    return { description => $self->description,
+             %$hints };
 }
 
 sub commit {
-    my ($self, $ticket_obj, $transaction_obj) = @_;
-    return RT::Lorzy->evaluate( $self->action, ticket => $ticket_obj, transaction => $transaction_obj);
+    my ($self, %args) = @_;
+    return RT::Lorzy->evaluate( $self->factory->action,
+                                ticket => $self->ticket_obj,
+                                transaction => $self->transaction);
 }
 
 1;

Modified: rt/3.999/branches/lorzy/lib/RT/Lorzy/Dispatcher.pm
==============================================================================
--- rt/3.999/branches/lorzy/lib/RT/Lorzy/Dispatcher.pm	(original)
+++ rt/3.999/branches/lorzy/lib/RT/Lorzy/Dispatcher.pm	Thu Mar 19 08:35:53 2009
@@ -1,8 +1,7 @@
 package RT::Lorzy::Dispatcher;
-use base 'RT::Rule';
 use base 'RT::Ruleset';
 
-my $rules;
+my $rules = [];
 
 sub reset_rules {
     $rules = [];
@@ -13,28 +12,8 @@
     push @$rules, $rule;
 }
 
-sub prepare {
-    my ($self, %args) = @_;
-    for (@$rules) {
-        push @{$self->{prepared}}, $_
-            if $_->on_condition( $self->ticket_obj, $self->transaction );
-    }
-    return scalar @{$self->{prepared}};
-}
-
-sub commit {
-    my ($self, %args) = @_;
-    for ( @{$self->{prepared}} ) {
-        $_->commit( $self->ticket_obj, $self->transaction );
-    }
-}
-
-sub hints {
-    my ($self, $callback) = @_;
-    return unless $self->transaction->object;
-    for ( @{$self->{prepared}} ) {
-        $_->hints( $self->transaction->object, $self->transaction, $callback );
-    }
+sub rules {
+    return $rules;
 }
 
 1;

Modified: rt/3.999/branches/lorzy/lib/RT/Lorzy/Package/RT.pm
==============================================================================
--- rt/3.999/branches/lorzy/lib/RT/Lorzy/Package/RT.pm	(original)
+++ rt/3.999/branches/lorzy/lib/RT/Lorzy/Package/RT.pm	Thu Mar 19 08:35:53 2009
@@ -51,7 +51,6 @@
     signature => {
         'name'     => Lorzy::FunctionArgument->new( name => 'name' ),
         'template' => Lorzy::FunctionArgument->new( name => 'template' ),
-        'callback' => Lorzy::FunctionArgument->new( name => 'callback' ),
         'ticket'   => Lorzy::FunctionArgument->new( name => 'ticket', type => 'RT::Model::Ticket' ),
         'transaction' => Lorzy::FunctionArgument->new( name => 'transaction', type => 'RT::Model::Transaction' ),
     },
@@ -61,7 +60,7 @@
                                   ticket_obj => $args->{ticket},
                                   transaction_obj => $args->{transaction}
                               );
-        $rule->scrip_action_hints(@{$args}{qw(name template callback)});
+        $rule->scrip_action_hints(@{$args}{qw(name template)});
     },
 );
 

Modified: rt/3.999/branches/lorzy/lib/RT/Rule.pm
==============================================================================
--- rt/3.999/branches/lorzy/lib/RT/Rule.pm	(original)
+++ rt/3.999/branches/lorzy/lib/RT/Rule.pm	Thu Mar 19 08:35:53 2009
@@ -86,10 +86,10 @@
 }
 
 sub scrip_action_hints {
-    my ($self, $scrip_action, $template, $callback, %args) = @_;
+    my ($self, $scrip_action, $template, %args) = @_;
     my $action = $self->_get_scrip_action($scrip_action, $template, %args);
     $action->prepare or return;
-    $action->hints($callback);
+    $action->hints();
 }
 
 sub _get_scrip_action {

Modified: rt/3.999/branches/lorzy/lib/RT/Ruleset.pm
==============================================================================
--- rt/3.999/branches/lorzy/lib/RT/Ruleset.pm	(original)
+++ rt/3.999/branches/lorzy/lib/RT/Ruleset.pm	Thu Mar 19 08:35:53 2009
@@ -60,6 +60,7 @@
 sub find_all_rules {
     my ($class, %args) = @_;
     my $current_user = $args{ticket_obj}->current_user;
+
     return [
         grep { $_->prepare }
         map { $_->new(current_user => $current_user, %args) }
@@ -74,12 +75,17 @@
         for @$rules;
 }
 
+sub register {
+    my ($class, $ruleset) = @_;
+    push @RULE_SETS, $ruleset;
+}
+
 sub add {
     my ($class, %args) = @_;
     for (@{$args{rules}}) {
         $_->require or die $UNIVERSAL::require::ERROR;
     }
-    push @RULE_SETS, $class->new(\%args);
+    $class->register($class->new(\%args));
 }
 
 1;

Modified: rt/3.999/branches/lorzy/lib/RT/ScripAction.pm
==============================================================================
--- rt/3.999/branches/lorzy/lib/RT/ScripAction.pm	(original)
+++ rt/3.999/branches/lorzy/lib/RT/ScripAction.pm	Thu Mar 19 08:35:53 2009
@@ -84,7 +84,6 @@
         argument       => undef,
         current_user    => undef,
         scrip_action_obj => undef,
-        hints            => {},
         template_obj    => undef,
         ticket_obj      => undef,
         transaction_obj => undef,
@@ -100,11 +99,10 @@
     $self->{'ticket_obj'}      = $args{'ticket_obj'};
     $self->{'transaction_obj'} = $args{'transaction_obj'};
     $self->{'type'}           = $args{'type'};
-    $self->{'hints'}          = $args{'hints'};
 
 #    Scalar::Util::weaken( $self->{'scrip_action_obj'} );
     Scalar::Util::weaken( $self->{'template_obj'} );
-    Scalar::Util::weaken( $self->{'ticket_obj'} );
+#    Scalar::Util::weaken( $self->{'ticket_obj'} );
     Scalar::Util::weaken( $self->{'transaction'} );
 
 }
@@ -136,12 +134,6 @@
 }
 
 
-sub hints {
-    my $self = shift;
-    return ( $self->{'hints'} );
-}
-
-
 sub scrip_action_obj {
     my $self = shift;
     return ( $self->{'scrip_action_obj'} );
@@ -180,11 +172,10 @@
 }
 
 sub hints {
-    my ($self, $callback) = @_;
-    return;
+    my ($self) = @_;
+    return { class => 'General' };
 }
 
-
 #If this rule applies to this transaction, return true.
 
 sub is_applicable {
@@ -199,7 +190,6 @@
     # We need to clean up all the references that might maybe get
     # oddly circular
     $self->{'scrip_action_obj'} = undef;
-    $self->{'hints'}       = undef;
     $self->{'template_obj'}    = undef;
     $self->{'ticket_obj'} = undef;
     $self->{'transaction_obj'} = undef;

Modified: rt/3.999/branches/lorzy/lib/RT/ScripAction/SendEmail.pm
==============================================================================
--- rt/3.999/branches/lorzy/lib/RT/ScripAction/SendEmail.pm	(original)
+++ rt/3.999/branches/lorzy/lib/RT/ScripAction/SendEmail.pm	Thu Mar 19 08:35:53 2009
@@ -228,6 +228,20 @@
     return $result;
 }
 
+=head2 hints
+
+=cut
+
+sub hints {
+    my ($self) = @_;
+
+    return { class => 'SendEmail',
+             recipients => { to =>  [$self->to],
+                             cc =>  [$self->cc],
+                             bcc => [$self->bcc],
+                         } };
+}
+
 =head2 to
 
 Returns an array of L<Email::Address> objects containing all the To: recipients for this notification

Modified: rt/3.999/branches/lorzy/t/api/action-createtickets.t
==============================================================================
--- rt/3.999/branches/lorzy/t/api/action-createtickets.t	(original)
+++ rt/3.999/branches/lorzy/t/api/action-createtickets.t	Thu Mar 19 08:35:53 2009
@@ -58,7 +58,9 @@
 my $rule = RT::Lorzy->create_scripish(
     'On Transaction',
     'Create Tickets',
-    'Approvals', $q->id);
+    'Approvals',
+    'Create approval tickets',
+     $q->id);
 
 RT::Lorzy::Dispatcher->add_rule( $rule );
 

Modified: rt/3.999/branches/lorzy/t/api/rules-preview.t
==============================================================================
--- rt/3.999/branches/lorzy/t/api/rules-preview.t	(original)
+++ rt/3.999/branches/lorzy/t/api/rules-preview.t	Thu Mar 19 08:35:53 2009
@@ -10,6 +10,7 @@
 $ticket->create(
 	queue => $queue->id,
 	requestor => 'root at localhost',
+	cc => 'moosecc at example.org',
 	subject => 'scrip preview test',
 );
 
@@ -18,6 +19,7 @@
 our $DEBUG=1;
 
 my ( $txn_id, $description, $txn ) = $ticket->correspond(
+    cc_message_to => 'onetime at example.org',
     content       => 'test dry run and previews',
     time_taken    => '10',
     dry_run       => 1,
@@ -25,21 +27,20 @@
 
 ok($txn_id, 'created txn');
 
-diag $txn;
-diag $description;
-diag $txn_id;
-
 my $preview = {};
-
-sub hints_callback {
-    my ($action_class, $description, $key, $value) = @_;
-    if ($action_class eq 'SendEmail') {
-        push @{$preview->{$description}{$key} ||= []}, $value;
-    }
-}
-
 for (@{$txn->rules}) {
-    $_->hints(\&hints_callback);
+    my $hints = $_->hints;
+    next unless $hints->{class} eq 'SendEmail';
+    $preview->{$hints->{description}} = $hints->{recipients};
 }
 
-warn Dumper($preview);use Data::Dumper;
+is_deeply($preview,
+          { 'On Correspond Notify Other Recipients' =>
+            { to => [], cc => [], bcc => [] },
+            'On Correspond Notify requestors and ccs' =>
+            { to => [ 'root at localhost' ],
+              cc => [ 'moosecc at example.org' ],
+              bcc => [] },
+            'On Correspond Notify admin_ccs' =>
+            { to => [], cc => [], bcc => [] } }
+      );


More information about the Rt-commit mailing list