File Coverage

blib/lib/Data/Pipeline/Action/ForEach.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Data::Pipeline::Action::ForEach;
2              
3 1     1   1879 use Moose;
  0            
  0            
4             with 'Data::Pipeline::Action';
5              
6             use namespace::autoclean;
7              
8             use MooseX::Types::Moose qw( CodeRef );
9             use Data::Pipeline::Types qw( Iterator );
10              
11             use Data::Pipeline::Machine;
12              
13             has pipeline => (
14             isa => 'Object',
15             is => 'rw',
16             required => 1
17             );
18              
19             sub transform {
20             my($self, $option_iterator) = @_;
21              
22             $option_iterator = to_Iterator($option_iterator);
23              
24             my $options = { %Data::Pipeline::Machine::current_options, %{$option_iterator -> next || {}} };
25              
26             #print STDERR "Foreach making the following options available: ", join(", ", map { "$_ => " . $options -> {$_} } keys %$options), "\n";
27             my $iterator = to_Iterator(Data::Pipeline::Machine::with_options(sub{
28             #print STDERR "Options available: ", join(", ", map { "$_ => " . $Data::Pipeline::Machine::current_options{$_} } keys %Data::Pipeline::Machine::current_options), "\n";
29             $self -> pipeline -> from( %$options )
30             }, $options));
31              
32             return Data::Pipeline::Iterator -> new(
33             source => Data::Pipeline::Iterator::Source -> new(
34             has_next => sub {
35             my $i = !($option_iterator -> finished && $iterator -> finished);
36             #print STDERR "ForEach finished? ", ($i ? 'no' : 'yes'), "\n";
37             return $i;
38             },
39             get_next => sub {
40             if($iterator -> finished) {
41             if($option_iterator -> finished) {
42             return;
43             }
44             $options = { %Data::Pipeline::Machine::current_options, %{$option_iterator -> next || {}} };
45             #print STDERR "Foreach making the following options available for a new iterator: ", join(", ", map { "$_ => " . $options -> {$_} } keys %$options), "\n";
46             #print STDERR "iterator before: $iterator\n";
47             $iterator = to_Iterator(Data::Pipeline::Machine::with_options(sub{
48             #print STDERR "Options available: ", join(", ", map { "$_ => " . $Data::Pipeline::Machine::current_options{$_} } keys %Data::Pipeline::Machine::current_options), "\n";
49             $self -> pipeline -> from( %$options )
50             }, $options));
51             #print STDERR "iterator after: $iterator\n";
52             }
53              
54             my $n = $iterator -> next;
55             #print STDERR "it: @{[$iterator->finished]} option: @{[$option_iterator->finished]} n: $n\n";
56             return $n;
57             },
58             )
59             );
60             }
61              
62             1;
63              
64             __END__
65