Skinny/Iterator.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 136 CC 14 SCAR 37.3
CC
14
Cov
0%
iterator
110.0
new
6.0
all
6.0
CRAP 41.7
1 package DBIx::Skinny::Iterator;
2 use strict;
3 use warnings;
4 use Scalar::Util qw(blessed);
5 use Carp ();
6  
7 sub new {
8 my ($class, %args) = @_;
9  
10 my $self = bless \%args, $class;
11 $self->{_cache} = 1;
12  
13 $self->reset;
14  
15 return wantarray ? $self->all : $self;
16 }
17  
18 sub iterator {
19 my $self = shift;
20  
21 my $position = $self->{_position};
22 if ( $self->{_cache}
23 && ( my $row_cache = $self->{_rows_cache}->[$position] ) ) {
24 $self->{_position} = $position + 1;
25 return $row_cache;
26 }
27  
28 my $row;
29 if ($self->{sth}) {
30 $row = $self->{sth}->fetchrow_hashref('NAME_lc');
31 unless ( $row ) {
32 $self->{skinny}->_close_sth($self->{sth});
33 $self->{sth} = undef;
34 return;
35 }
36 } elsif ($self->{data} && ref $self->{data} eq 'ARRAY') {
37 $row = shift @{$self->{data}};
38 unless ( $row ) {
39 return;
40 }
41 } else {
42 return;
43 }
44  
45 my $obj;
46 if ( Scalar::Util::blessed($row) ) {
47 $obj = $row;
48 } elsif ($self->suppress_objects) {
49 $obj = $row;
50 } else {
51 $obj = $self->{row_class}->new(
52 {
53 sql => $self->{sql},
54 row_data => $row,
55 skinny => $self->{skinny},
56 opt_table_info => $self->{opt_table_info},
57 }
58 );
59  
60 unless ($self->{_setup}) {
61 $obj->setup;
62 $self->{_setup}=1;
63 }
64 }
65  
66 $self->{_rows_cache}->[$position] = $obj if $self->{_cache};
67 $self->{_position} = $position + 1;
68  
69 return $obj;
70 }
71  
72 sub first {
73 my $self = shift;
74 $self->reset;
75 scalar($self->next);
76 }
77  
78 sub next { $_[0]->iterator }
79  
80 sub all {
81 my $self = shift;
82 my @result;
83 while ( my $row = $self->next ) {
84 push @result, $row;
85 }
86 return wantarray ? @result : \@result;
87 }
88  
89 sub reset {
90 my $self = shift;
91 $self->{_position} = 0;
92 return $self;
93 }
94  
95 sub count {
96 my $self = shift;
97 my $rows = $self->reset->all;
98 $self->reset;
99 scalar @$rows;
100 }
101  
102 sub suppress_objects {
103 my ($self, $mode) = @_;
104 return $self->{suppress_objects} unless defined $mode;
105 $self->{suppress_objects} = $mode;
106 }
107  
108 sub no_cache {
109 Carp::carp( "no_cache method has been deprecated. Please use cache method instead" );
110 $_[0]->{_cache} = 0;
111 }
112  
113 sub cache {
114 my ($self, $mode) = @_;
115 return $self->{_cache} unless defined $mode;
116 $self->{_cache} = $mode;
117 }
118  
119 sub position { $_[0]->{_position} }
120  
121 1;
122  
123 __END__