separate scope setup in rule bodies into its own op
Matt S Trout [Tue, 11 Feb 2014 07:03:00 +0000 (07:03 +0000)]
lib/DX/Op/CallRule.pm
lib/DX/Op/SetupScope.pm [new file with mode: 0644]
lib/DX/Role/Op/WithArgs.pm [new file with mode: 0644]
lib/DX/RuleSet.pm

index f3d1b4f..17deac0 100644 (file)
@@ -26,9 +26,8 @@ sub run {
   my $invoke = DX::Op::FromCode->new(
     code => sub {
       my ($self, $state) = @_;
-      my ($names, $body) = @{$var->bound_value};
-      my %new; @new{@$names} = @args;
-      $state->but(scope => {})->assign_vars(%new)->then($body);
+      my $call = $var->bound_value->but(args => \@args);
+      $state->but(scope => {})->then($call);
     }
   );
   my $ret_op = DX::Op::SetScope->new(
diff --git a/lib/DX/Op/SetupScope.pm b/lib/DX/Op/SetupScope.pm
new file mode 100644 (file)
index 0000000..90c170d
--- /dev/null
@@ -0,0 +1,15 @@
+package DX::Op::SetupScope;
+
+use Moo;
+
+has arg_names => (is => 'ro', required => 1);
+
+with 'DX::Role::Op::WithArgs';
+
+sub run {
+  my ($self, $state) = @_;
+  my %new; @new{@{$self->arg_names}} = @{$self->args};
+  $state->assign_vars(%new)->then($self->next);
+}
+
+1;
diff --git a/lib/DX/Role/Op/WithArgs.pm b/lib/DX/Role/Op/WithArgs.pm
new file mode 100644 (file)
index 0000000..69bc22d
--- /dev/null
@@ -0,0 +1,9 @@
+package DX::Role::Op::WithArgs;
+
+use Moo::Role;
+
+has args => (is => 'ro');
+
+with 'DX::Role::Op';
+
+1;
index 2ae880d..09a2ff5 100644 (file)
@@ -1,6 +1,7 @@
 package DX::RuleSet;
 
 use Moo;
+use DX::Op::SetupScope;
 use DX::Op::CallRule;
 use DX::Op::MemberOf;
 use DX::Op::ApplyConstraint;
@@ -27,7 +28,7 @@ sub add_rule {
 sub _make_rule {
   my ($self, $vars, @body) = @_;
   my $head = $self->expand_and_link(DX::Op::Return->new, @body);
-  [ $vars, $head ];
+  DX::Op::SetupScope->new(arg_names => $vars, next => $head);
 }
 
 sub expand_and_link {