Commit | Line | Data |
60cda014 |
1 | package DX::Var; |
2 | |
3 | use Moo; |
4 | |
5 | has id => (is => 'ro', required => 1); |
6 | |
d95799c4 |
7 | has root_set => (is => 'ro', predicate => 1); |
8 | |
9 | has 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 |
17 | has 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 |
25 | sub is_bound { |
26 | my ($self) = @_; |
d95799c4 |
27 | $self->has_bound_value || $self->has_bound_stream || $self->has_root_set; |
b373788e |
28 | } |
29 | |
60cda014 |
30 | sub with_stream { |
31 | my ($self, $stream) = @_; |
32 | $self->new(%$self, bound_stream => $stream); |
33 | } |
34 | |
b40d5c51 |
35 | sub with_value { |
36 | my ($self, $stream) = @_; |
37 | $self->new(%$self, bound_value => $stream); |
38 | } |
39 | |
d95799c4 |
40 | sub with_root_set { |
41 | my ($self, $set) = @_; |
42 | $self->new(%$self, root_set => $set); |
43 | } |
44 | |
71217e42 |
45 | sub copy { |
46 | my ($self) = @_; |
47 | ref($self)->new(%$self); |
48 | } |
49 | |
60cda014 |
50 | 1; |