use VarRef for derived bindings
Matt S Trout [Tue, 11 Feb 2014 18:56:34 +0000 (18:56 +0000)]
lib/DX/Op/Prop.pm
lib/DX/VarRef.pm [new file with mode: 0644]

index dc4ae78..c2b494b 100644 (file)
@@ -1,5 +1,6 @@
 package DX::Op::Prop;
 
+use DX::VarRef;
 use Moo;
 
 with 'DX::Role::Op';
@@ -31,7 +32,8 @@ sub run {
     if ($value->can("has_${name}") and not $value->${\"has_${name}"}) {
       return $state->backtrack;
     }
-    return $state->bind_value($args{value}->id, $value->$name)
+    my $var_ref = DX::VarRef->new(var_id => $args{of}->id, derive => $name);
+    return $state->bind_value($args{value}->id, $var_ref)
                  ->add_dependencies($args{value}->id => $args{of}->id)
                  ->then($self->next);
   }
diff --git a/lib/DX/VarRef.pm b/lib/DX/VarRef.pm
new file mode 100644 (file)
index 0000000..8b82db6
--- /dev/null
@@ -0,0 +1,16 @@
+package DX::VarRef;
+
+use Moo;
+
+with 'DX::Role::Ref';
+
+has var_id => (is => 'ro', required => 1);
+
+has derive => (is => 'ro', default => sub { $_[0] });
+
+sub resolve {
+  my ($self, $state) = @_;
+  $state->resolve_value($state->by_id->{$self->var_id})->${\$self->derive};
+}
+
+1;