move actions to being held by fact objects
[scpubgit/DKit.git] / lib / DX / Var.pm
CommitLineData
60cda014 1package DX::Var;
2
3use Moo;
4
5has id => (is => 'ro', required => 1);
6
d95799c4 7has root_set => (is => 'ro', predicate => 1);
8
9has bound_stream => (
10 is => 'lazy', predicate => 1, clearer => 1,
11 builder => sub {
12 my ($self) = @_;
13 $self->root_set->to_stream;
14 }
15);
60cda014 16
b373788e 17has bound_value => (is => 'lazy', predicate => 1, clearer => 1, builder => sub {
71217e42 18 if (defined(my $next = $_[0]->bound_stream->next)) {
19 return $next;
20 }
21 DX::State->return_from_op('backtrack');
22 return;
60cda014 23});
24
b373788e 25sub is_bound {
26 my ($self) = @_;
d95799c4 27 $self->has_bound_value || $self->has_bound_stream || $self->has_root_set;
b373788e 28}
29
60cda014 30sub with_stream {
31 my ($self, $stream) = @_;
32 $self->new(%$self, bound_stream => $stream);
33}
34
b40d5c51 35sub with_value {
36 my ($self, $stream) = @_;
37 $self->new(%$self, bound_value => $stream);
38}
39
d95799c4 40sub with_root_set {
41 my ($self, $set) = @_;
42 $self->new(%$self, root_set => $set);
43}
44
71217e42 45sub copy {
46 my ($self) = @_;
47 ref($self)->new(%$self);
48}
49
60cda014 501;