f2c11dd7a38c3110e18a6c3451ae935ddbf4141b
[scpubgit/DKit.git] / lib / DX / Var.pm
1 package DX::Var;
2
3 use Moo;
4
5 has id => (is => 'ro', required => 1);
6
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 );
16
17 has bound_value => (is => 'lazy', predicate => 1, clearer => 1, builder => sub {
18   if (defined(my $next = $_[0]->bound_stream->next)) {
19     return $next;
20   }
21   DX::State->return_from_op('backtrack');
22   return;
23 });
24
25 has action => (is => 'ro', predicate => 1);
26
27 sub is_bound {
28   my ($self) = @_;
29   $self->has_bound_value || $self->has_bound_stream || $self->has_root_set;
30 }
31
32 sub with_stream {
33   my ($self, $stream) = @_;
34   $self->new(%$self, bound_stream => $stream);
35 }
36
37 sub with_value {
38   my ($self, $stream) = @_;
39   $self->new(%$self, bound_value => $stream);
40 }
41
42 sub with_action {
43   my ($self, $action) = @_;
44   $self->new(%$self, action => $action);
45 }
46
47 sub with_root_set {
48   my ($self, $set) = @_;
49   $self->new(%$self, root_set => $set);
50 }
51
52 sub copy {
53   my ($self) = @_;
54   ref($self)->new(%$self);
55 }
56
57 1;