File Coverage

blib/lib/ODS/Iterator.pm
Criterion Covered Total %
statement 65 85 76.4
branch 14 16 87.5
condition 4 9 44.4
subroutine 15 19 78.9
pod 0 16 0.0
total 98 145 67.5


line stmt bran cond sub pod time code
1             package ODS::Iterator;
2              
3 2     2   12 use YAOO;
  2         2  
  2         10  
4              
5             auto_build;
6              
7             has table => isa(object);
8              
9             has next_index => isa(integer(0));
10              
11             has prev_index => isa(integer(0));
12              
13             use overload
14 2     2   658 '@{}' => sub { $_[0]->all };
  2     48   3  
  2         11  
  48         2960  
15              
16             # The following sub routines only manipulate the data that is in "memory"
17             # to query a larger dataset you should use the storage api/sub routines.
18              
19             sub all {
20 48     48 0 85 return $_[0]->table->rows;
21             }
22              
23             sub first {
24 2     2 0 1266 return $_[0]->table->rows->[0];
25             }
26              
27             sub last {
28 2     2 0 1028 return $_[0]->table->rows->[-1];
29             }
30              
31             sub next {
32 8     8 0 3912 my $next = $_[0]->table->rows->[$_[0]->next_index];
33 8         110 $_[0]->next_index($_[0]->next_index + 1);
34 8 100 50     192 return $next ? $next : $_[0]->next_index(0) && 0;
35             }
36              
37             sub prev {
38 8 50   8 0 2747 if (not defined $_[0]->prev_index ) {
39 0         0 $_[0]->prev_index(scalar @{ $_[0] });
  0         0  
40             }
41 8         57 my $prev = $_[0]->table->rows->[$_[0]->prev_index];
42 8         92 $_[0]->prev_index($_[0]->prev_index + 1);
43 8 100 50     178 return $prev ? $prev : $_[0]->prev_index(scalar @{$_[0]}) && 0;
44             }
45              
46             sub flat {
47 0     0 0 0 my ($self, $keyfield) = @_;
48 0         0 my @array;
49 0         0 for my $row (@{ $self }) {
  0         0  
50 0         0 push @array, {%{$row}};
  0         0  
51             }
52 0         0 return \@array;
53             }
54              
55             sub array_to_hash {
56 2     2 0 12 my ($self, $keyfield) = @_;
57 2   33     8 $keyfield ||= $self->table->keyfield;
58 2         23 my %hash;
59 2         3 for my $row (@{ $self }) {
  2         3  
60 6         20 $hash{$row->{$keyfield}} = {%{$row}};
  6         11  
61             }
62 2         4 return \%hash;
63             }
64              
65             sub foreach {
66 2     2 0 74 my ($self, $cb) = @_;
67 2         3 my @results;
68 2         3 foreach my $row ( @{ $self }) {
  2         3  
69 6         36 push @results, $cb->($row);
70             }
71 2 50       8 return wantarray ? @results : \@results;
72             }
73              
74             sub find {
75 8     8 0 1968 my ($self, $cb) = @_;
76 8         10 my $result;
77 8         9 foreach my $row ( @{ $self }) {
  8         14  
78 28         95 my $valid = $cb->({%{$row}});
  28         51  
79 28 100 50     103 do { $result = $row } and last if $valid;
  6         19  
80             }
81 8         19 return $result;
82             }
83              
84             sub find_index {
85 6     6 0 514 my ($self, $cb) = @_;
86 6         8 my $i;
87 6         8 my @rows = @{ $self };
  6         11  
88 6         74 for ($i = 0; $i < scalar @rows; $i++) {
89 20         25 my $row = $rows[$i];
90 20         21 my $valid = $cb->({%{$row}});
  20         36  
91 20 100       181 last if $valid;
92             }
93 6         14 return $i;
94             }
95              
96             sub reverse {
97 4     4 0 1857 my $self = shift;
98 4         5 @{$self} = reverse @{$self};
  4         42  
  4         10  
99 4         32 return [@{$self}];
  4         5  
100             }
101              
102             sub filter {
103 4     4 0 32 my ($self, $cb) = @_;
104             my @results = grep {
105 8 100       36 $cb->({%{$_}}) && $_
  8         19  
106 4         6 } @{ $self };
  4         7  
107 4 100       16 return wantarray ? @results : \@results;
108             }
109              
110             sub sort {
111 0     0 0 0 my ($self, $cb) = @_;
112 0         0 @{ $self } = sort { $cb->($_) } @{ $self };
  0         0  
  0         0  
  0         0  
113 0         0 @{ $self };
  0         0  
114             }
115              
116             sub shift {
117 0     0 0 0 shift @{ $_[0] };
  0         0  
118             }
119              
120             sub pop {
121 0     0 0 0 pop @{ $_[0] };
  0         0  
122             }
123              
124             sub splice {
125 4     4 0 7 my ($self, @params) = @_;
126 4         4 return splice @{$self}, shift @params, shift @params;
  4         9  
127             }
128              
129             1;