File Coverage

blib/lib/ODS/Iterator.pm
Criterion Covered Total %
statement 71 84 84.5
branch 14 16 87.5
condition 4 9 44.4
subroutine 16 19 84.2
pod 0 16 0.0
total 105 144 72.9


line stmt bran cond sub pod time code
1             package ODS::Iterator;
2              
3 70     70   438 use YAOO;
  70         83  
  70         438  
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 70     70   24670 '@{}' => sub { $_[0]->all };
  70     114   88  
  70         750  
  114         16587  
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 114     114 0 383 return $_[0]->table->rows;
21             }
22              
23             sub first {
24 4     4 0 5172 return $_[0]->table->rows->[0];
25             }
26              
27             sub last {
28 4     4 0 3560 return $_[0]->table->rows->[-1];
29             }
30              
31             sub next {
32 16     16 0 24516 my $next = $_[0]->table->rows->[$_[0]->next_index];
33 16         335 $_[0]->next_index($_[0]->next_index + 1);
34 16 100 50     672 return $next ? $next : $_[0]->next_index(0) && 0;
35             }
36              
37             sub prev {
38 16 50   16 0 11037 if (not defined $_[0]->prev_index ) {
39 0         0 $_[0]->prev_index(scalar @{ $_[0] });
  0         0  
40             }
41 16         185 my $prev = $_[0]->table->rows->[$_[0]->prev_index];
42 16         263 $_[0]->prev_index($_[0]->prev_index + 1);
43 16 100 50     576 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 3     3 0 22 my ($self, $keyfield) = @_;
57 3   33     32 $keyfield ||= $self->table->keyfield;
58 3         42 my %hash;
59 3         8 for my $row (@{ $self }) {
  3         6  
60 9         40 $hash{$row->{$keyfield}} = {%{$row}};
  9         31  
61             }
62 3         26 return \%hash;
63             }
64              
65             sub foreach {
66 3     3 0 154 my ($self, $cb) = @_;
67 3         9 my @results;
68 3         8 foreach my $row ( @{ $self }) {
  3         10  
69 9         69 push @results, $cb->($row);
70             }
71 3 50       20 return wantarray ? @results : \@results;
72             }
73              
74             sub find {
75 11     11 0 4280 my ($self, $cb) = @_;
76 11         73 my $result;
77 11         28 foreach my $row ( @{ $self }) {
  11         36  
78 33         184 my $valid = $cb->({%{$row}});
  33         85  
79 33 100 50     197 do { $result = $row } and last if $valid;
  9         47  
80             }
81 11         54 return $result;
82             }
83              
84             sub find_index {
85 8     8 0 3134 my ($self, $cb) = @_;
86 8         18 my $i;
87 8         16 my @rows = @{ $self };
  8         26  
88 8         150 for ($i = 0; $i < scalar @rows; $i++) {
89 24         40 my $row = $rows[$i];
90 24         34 my $valid = $cb->({%{$row}});
  24         59  
91 24 100       244 last if $valid;
92             }
93 8         29 return $i;
94             }
95              
96             sub reverse {
97 6     6 0 5287 my $self = shift;
98 6         15 @{$self} = reverse @{$self};
  6         92  
  6         3885  
99 6         66 return [@{$self}];
  6         13  
100             }
101              
102             sub filter {
103 6     6 0 84 my ($self, $cb) = @_;
104             my @results = grep {
105 12 100       78 $cb->({%{$_}}) && $_
  12         73  
106 6         14 } @{ $self };
  6         17  
107 6 100       30 return wantarray ? @results : \@results;
108             }
109              
110             sub sort {
111 1     1 0 946 my ($self, $cb) = @_;
112 1         8 @{ $self } = sort { $cb->($a, $b) } @{ $self };
  1         22  
  28         612  
  1         6  
113 1         21 $self;
114             }
115              
116             sub shift {
117 0     0 0 0 CORE::shift @{ $_[0] };
  0         0  
118             }
119              
120             sub pop {
121 0     0 0 0 CORE::pop @{ $_[0] };
  0         0  
122             }
123              
124             sub splice {
125 5     5 0 15 my ($self, @params) = @_;
126              
127 5         10 return splice( @{$self}, CORE::shift @params, CORE::shift @params);
  5         15  
128             }
129              
130             1;