File Coverage

blib/lib/Dallycot/AST/Index.pm
Criterion Covered Total %
statement 15 49 30.6
branch 0 8 0.0
condition n/a
subroutine 5 12 41.6
pod 0 2 0.0
total 20 71 28.1


line stmt bran cond sub pod time code
1             package Dallycot::AST::Index;
2             our $AUTHORITY = 'cpan:JSMITH';
3              
4             # ABSTRACT: Select value at given index in collection-like value
5              
6 23     23   12330 use strict;
  23         41  
  23         731  
7 23     23   93 use warnings;
  23         30  
  23         532  
8              
9 23     23   84 use utf8;
  23         30  
  23         103  
10 23     23   467 use parent 'Dallycot::AST';
  23         34  
  23         110  
11              
12 23     23   1191 use Promises qw(deferred);
  23         38  
  23         105  
13              
14             sub to_rdf {
15 0     0 0   my($self, $model) = @_;
16              
17 0           my @exprs = @$self;
18              
19 0           my $node = $model -> apply(
20             $model -> meta_uri('loc:index'),
21             [ $exprs[0], $exprs[1] ]
22             );
23              
24 0           shift @exprs; shift @exprs;
  0            
25              
26 0           while(@exprs) {
27 0           $node = $model -> apply(
28             $model -> meta_uri('loc:index'),
29             [ $node, shift @exprs ]
30             );
31             }
32            
33 0           return $node;
34             }
35              
36             sub execute {
37 0     0 0   my ( $self, $engine ) = @_;
38              
39 0           my $d = deferred;
40              
41 0           my @expressions = @$self;
42              
43 0 0         if (@expressions) {
44             $engine->execute( shift @expressions )->done(
45             sub {
46 0     0     my ($root) = @_;
47 0           $self->_do_next_index(
48             $engine, $d,
49             root => $root,
50             indices => \@expressions
51             );
52             },
53             sub {
54 0     0     $d->reject(@_);
55             }
56 0           );
57             }
58             else {
59 0           $d->reject('missing expressions');
60             }
61              
62 0           return $d->promise;
63             }
64              
65             sub _do_next_index {
66 0     0     my ( $self, $engine, $d, %state ) = @_;
67 0 0         my ( $root, $index_expr, @indices )
68 0           = ( $state{root}, @{ $state{indices} || [] } );
69              
70 0 0         if ($index_expr) {
71             $engine->execute($index_expr)->done(
72             sub {
73 0     0     my ($index) = @_;
74              
75 0 0         if ( $index->isa('Dallycot::Value::Numeric') ) {
76 0           $index = $index->value;
77             }
78             else {
79 0           $d->reject("Vector indices must be numeric");
80 0           return;
81             }
82             $root->value_at( $engine, $index )->done(
83             sub {
84 0           $self->_do_next_index(
85             $engine, $d,
86             root => $_[0],
87             indices => \@indices
88             );
89             },
90             sub {
91 0           $d->reject(@_);
92             }
93 0           );
94             },
95             sub {
96 0     0     $d->reject(@_);
97             }
98 0           );
99             }
100             else {
101 0           $d->resolve($root);
102             }
103              
104 0           return;
105             }
106              
107             1;