introduce root set
[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
71217e42 25has action => (is => 'ro');
26
b373788e 27sub is_bound {
28 my ($self) = @_;
d95799c4 29 $self->has_bound_value || $self->has_bound_stream || $self->has_root_set;
b373788e 30}
31
60cda014 32sub with_stream {
33 my ($self, $stream) = @_;
34 $self->new(%$self, bound_stream => $stream);
35}
36
b40d5c51 37sub with_value {
38 my ($self, $stream) = @_;
39 $self->new(%$self, bound_value => $stream);
40}
41
71217e42 42sub with_action {
43 my ($self, $action) = @_;
44 $self->new(%$self, action => $action);
45}
46
d95799c4 47sub with_root_set {
48 my ($self, $set) = @_;
49 $self->new(%$self, root_set => $set);
50}
51
71217e42 52sub copy {
53 my ($self) = @_;
54 ref($self)->new(%$self);
55}
56
60cda014 571;