DR/Iterator.pm

filter: Statementstmt 88.9% 169 / 190 Branchbran 64.3% 85 / 132 Conditioncond 46.4% 26 / 56 MC/DCmcdc 17.1% 6 / 35 Subroutinesub 89.6% 26 / 29 Podpod 71.4% 10 / 14 totaltotal 70.6% 322 / 456 CC 87 SCAR 24.3
CC
87
Cov
74%
push
38.4
get
15.0
AUTOLOAD
14.8
CRAP 11.4
↑ Summary DR.pm »
1 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package DBIx::DR::Iterator;
6 3 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use DBIx::DR::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 # Perl 5.18 refuses smartmatch
12 15 ▶my $is = sub($$) {
Subroutine __ANON__: called CC=4 SCAR=16.4
13 15 my ($v1, $v2) = @_;
14 15 ▶ return 0 if defined($v1) xor defined($v2);
Branch: 1 branch
BranchTrueFalseTotal
if defined $v1 xor defined $v2 0 15 15
Condition25%1/4
defined $v1 xor defined $v2
l&&rl&&!r!l&&r!l&&!r
15000
MC/DC0%0/2
defined $v1 xor defined $v2
defined $v1 defined $v2
Truth table25%1/4
A xor B
A= defined $v1B= defined $v2
ABresult
110
101
011
000
15 15 ▶ return 1 unless defined $v1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v1 0 15 15
16 15 return $v1 eq $v2;
17 3 };
18  
19  
20 22 ▶sub new {
Subroutine new: called CC=13 SCAR=26.8
Pod documented
21 22 my ($class, $fetch, %opts) = @_;
22  
23 22 my ($is_hash, $is_array) = (0, 0);
24  
25 22 my $count;
26  
27 22 ▶ if ('ARRAY' eq ref $fetch) {
Branch: 2 branches
BranchTrueFalseTotal
if ('ARRAY' eq ref $fetch) { } 12 10 22
elsif ('HASH' eq ref $fetch) { } 10 0 10
28 12 $is_array = 1;
29 12 ▶ if ($count = @$fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count = @$fetch) 12 0 12
30 12 ▶ croak 'You must use array of hashrefs'
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $fetch->[0] or builtin::blessed($fetch->[0]) 0 12 12
Condition66%2/3
"HASH" eq ref $fetch->[0] or builtin::blessed($fetch->[0])
l!l&&r!l&&!r
1020
MC/DC0%0/2
"HASH" eq ref $fetch->[0] or builtin::blessed($fetch->[0])
"HASH" eq ref $fetch->[0] builtin::blessed($fetch->[0])
Truth table66%2/3
A or B
A= "HASH" eq ref $fetch->[0]B= builtin::blessed($fetch->[0])
ABresult
1X1
011
000
31 unless 'HASH' eq ref $fetch->[0] or blessed $fetch->[0];
32 }
33 } elsif ('HASH' eq ref $fetch) {
34 10 $is_hash = 1;
35 10 my ($k) = each %$fetch;
36 10 ▶ if ($count = keys %$fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count = keys %$fetch) 10 0 10
37 croak 'You must use hash of hashrefs'
38 10 ▶ unless 'HASH' eq ref $fetch->{$k} or blessed $fetch->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $fetch->{$k} or builtin::blessed($fetch->{$k}) 0 10 10
Condition66%2/3
"HASH" eq ref $fetch->{$k} or builtin::blessed($fetch->{$k})
l!l&&r!l&&!r
730
MC/DC0%0/2
"HASH" eq ref $fetch->{$k} or builtin::blessed($fetch->{$k})
"HASH" eq ref $fetch->{$k} builtin::blessed($fetch->{$k})
Truth table66%2/3
A or B
A= "HASH" eq ref $fetch->{$k}B= builtin::blessed($fetch->{$k})
ABresult
1X1
011
000
39 }
40 } else {
41 0 croak "You should bless 'HASHREF' or 'ARRAYREF' value";
42 }
43  
44  
45 my ($item_class, $item_constructor) =
46 22 ▶ camelize($opts{'-item'} || 'dbix-dr-iterator-item#new');
Condition100%2/2
$opts{'-item'} || 'dbix-dr-iterator-item#new'
l!l
202
MC/DC100%1/1
$opts{'-item'} || 'dbix-dr-iterator-item#new'
$opts{'-item'}
Truth table100%2/2
A
A= $opts{'-item'}
Aresult
11
00
47  
48 22 my $keep_blessed = $opts{-keep_blessed};
49 22 ▶ $keep_blessed //= 1;
Condition100%2/2
$keep_blessed //= 1
l!l
418
MC/DC100%1/1
$keep_blessed //= 1
$keep_blessed
Truth table100%2/2
A
A= $keep_blessed
Aresult
11
00
50  
51  
52 return bless {
53 fetch => $fetch,
54 is_hash => $is_hash,
55 is_array => $is_array,
56 count => $count,
57 iterator => 0,
58 item_class => $item_class,
59 item_constructor => $item_constructor,
60 is_changed => 0,
61 keep_blessed => $keep_blessed,
62 22 ▶ noitem_iter => $opts{-noitem_iter} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$opts{'-noitem_iter'} ? : 2 20 22
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
5170
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
63 } => ref($class) || $class;
64 }
65  
66  
67 8 ▶sub is_changed {
Subroutine is_changed: called CC=3 SCAR=11.0
Pod documented
68 8 my ($self, $value) = @_;
69 8 ▶ $self->{is_changed} = $value ? 1 : 0 if @_ > 1;
Branch: 2 branches
BranchTrueFalseTotal
if @_ > 1 4 4 8
$value ? : 3 1 4
70 8 return $self->{is_changed};
71 }
72  
73  
74 24 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
75 24 my ($self) = @_;
76 24 return $self->{count};
77 }
78  
79  
80 3 ▶sub reset {
Subroutine reset: called CC=2 SCAR=6.9
Pod documented
81 3 my ($self) = @_;
82 3 $self->{iterator} = 0;
83 3 ▶ keys %{ $self->{fetch} } if $self->{is_hash};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_hash"} 2 1 3
84 3 return;
85 }
86  
87  
88 76 ▶sub next : method {
Subroutine next: called CC=4 SCAR=13.9
Pod documented
89 76 my ($self) = @_;
90  
91 76 ▶ if ($self->{is_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is_array"}) 38 38 76
92 return $self->get($self->{iterator}++)
93 38 ▶ if $self->{iterator} < $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"iterator"} < $self->{"count"} 30 8 38
94 8 $self->{iterator} = 0;
95 8 return;
96 }
97  
98 38 my ($k) = each %{ $self->{fetch} };
99 38 ▶ return unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 7 31 38
100 31 return $self->get($k);
101 }
102  
103  
104 117 ▶sub get {
Subroutine get: called CC=14 SCAR=27.1
Pod documented
105 117 my ($self, $name) = @_;
106 117 ▶ croak "Usage \$collection->get('name|number')"
Branch: 1 branch
BranchTrueFalseTotal
if @_ <= 1 or not defined $name 0 117 117
Condition33%1/3
@_ <= 1 or not defined $name
l!l&&r!l&&!r
00117
MC/DC0%0/2
@_ <= 1 or not defined $name
@_ <= 1 not defined $name
Truth table33%1/3
A or not(B)
A= @_ <= 1B= not defined $name
ABresult
1X1
011
000
107 if @_ <= 1 or !defined($name);
108 117 my $item;
109 117 ▶ if ($self->{is_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'is_array'}) { } 51 66 117
110 51 ▶ croak "Element number must be digit value" unless $name =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^-?\d+$/ 0 51 51
111 croak "Element number is out of arraybound"
112 51 ▶ if $name >= $self->{count} || $name < -$self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $name >= $self->{"count"} or $name < -$self->{"count"} 0 51 51
Condition33%1/3
$name >= $self->{"count"} or $name < -$self->{"count"}
l!l&&r!l&&!r
0051
MC/DC0%0/2
$name >= $self->{"count"} or $name < -$self->{"count"}
$name >= $self->{"count"} $name < -$self->{"count"}
Truth table33%1/3
A or B
A= $name >= $self->{"count"}B= $name < -$self->{"count"}
ABresult
1X1
011
000
113 51 $item = $self->{fetch}[ $name ];
114 } else {
115 66 ▶ croak "Key '$name' is not exists" unless exists $self->{fetch}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"fetch"}{$name} 0 66 66
116 66 $item = $self->{fetch}{ $name };
117 }
118  
119 117 ▶ unless(blessed $item) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($item)) 51 66 117
120 51 ▶ if (my $method = $self->{item_constructor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->{'item_constructor'}) { } 39 12 51
121 $item = $self->{item_class}->$method(
122 $item,
123 39 ▶ ( $self->{noitem_iter} ? () : $self )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'noitem_iter'} ? : 2 37 39
124 );
125
126 39 ▶ if (blessed($item) and $self->{keep_blessed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($item) and $self->{"keep_blessed"}) 30 9 39
Condition100%3/3
builtin::blessed($item) and $self->{"keep_blessed"}
!ll&&!rl&&r
3630
MC/DC100%2/2
builtin::blessed($item) and $self->{"keep_blessed"}
builtin::blessed($item) $self->{"keep_blessed"}
Truth table100%3/3
A and B
A= builtin::blessed($item)B= $self->{"keep_blessed"}
ABresult
0X0
100
111
127 30 ▶ if ($self->{is_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'is_array'}) { } 18 12 30
128 18 $self->{fetch}[ $name ] = $item;
129 } else {
130 12 $self->{fetch}{ $name } = $item;
131 }
132 }
133 } else {
134 12 bless $item => $self->{item_class};
135 }
136  
137 }
138 117 return $item;
139 }
140  
141  
142 6 ▶sub exists {
Subroutine exists: called CC=8 SCAR=23.7
Pod documented
143 6 my ($self, $name) = @_;
144 6 ▶ croak "Usage \$collection->exists('name|number')"
Branch: 1 branch
BranchTrueFalseTotal
if @_ <= 1 or not defined $name 0 6 6
Condition33%1/3
@_ <= 1 or not defined $name
l!l&&r!l&&!r
006
MC/DC0%0/2
@_ <= 1 or not defined $name
@_ <= 1 not defined $name
Truth table33%1/3
A or not(B)
A= @_ <= 1B= not defined $name
ABresult
1X1
011
000
145 if @_ <= 1 or !defined($name);
146  
147 6 ▶ if ($self->{is_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is_array"}) 2 4 6
148 2 ▶ croak "Element number must be digit value" unless $name =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^\d+$/ 0 2 2
149 2 ▶ return 0 if $name >= $self->{count} || $name < -$self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $name >= $self->{"count"} or $name < -$self->{"count"} 0 2 2
Condition33%1/3
$name >= $self->{"count"} or $name < -$self->{"count"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$name >= $self->{"count"} or $name < -$self->{"count"}
$name >= $self->{"count"} $name < -$self->{"count"}
Truth table33%1/3
A or B
A= $name >= $self->{"count"}B= $name < -$self->{"count"}
ABresult
1X1
011
000
150 2 return 1;
151 }
152  
153 4 ▶ return exists($self->{fetch}{$name}) || 0;
Condition50%1/2
exists $self->{'fetch'}{$name} || 0
l!l
40
MC/DC0%0/1
exists $self->{'fetch'}{$name} || 0
exists $self->{'fetch'}{$name}
Truth table50%1/2
A
A= exists $self->{'fetch'}{$name}
Aresult
11
00
154 }
155  
156  
157 7 ▶sub all {
Subroutine all: called CC=6 SCAR=18.1
Pod documented
158 7 my ($self, $field) = @_;
159 7 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 7 7
160 7 my @res;
161 7 ▶ if ($self->{is_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'is_array'}) { } 5 2 7
162 5 ▶ for (my $i = 0; $i < @{ $self->{fetch} }; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"fetch"};}) 15 5 20
163 15 push @res => $self->get($i);
164 }
165 } else {
166 2 push @res => $self->get($_) for keys %{ $self->{fetch} };
167 }
168  
169 0 ▶ @res = map { $_->$field } @res if $field;
Branch: 1 branch
BranchTrueFalseTotal
if $field 0 7 7
170  
171 7 return @res;
172 }
173  
174 5 ▶sub grep : method {
Subroutine grep: called CC=3 SCAR=11.0
Pod documented
175 5 my ($self, $key, $value) = @_;
176  
177 5 my $cb;
178 5 ▶ if ('CODE' eq ref $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $key) { } 2 3 5
179 2 $cb = $key;
180 } else {
181 3 ▶ $cb = sub { $is->($_[0]->$key, $value) };
Subroutine __ANON__: called CC=1 SCAR=0.0
182 }
183  
184 5 my $obj;
185 5 ▶ if ($self->{is_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'is_array'}) { } 2 3 5
186 2 $obj = [ grep { $cb->($_) } $self->all ];
187 } else {
188 $obj = {
189 6 map {( $_ => $self->get($_) )}
190 15 grep { $cb->( $self->get($_) ) }
191 3 keys %{ $self->{fetch} }
192 };
193 }
194  
195 return $self->new(
196 $obj,
197 -item => decamelize($self->{item_class}, $self->{item_constructor})
198 5 );
199 }
200  
201 0 ▶sub first {
Subroutine first: not called CC=3 SCAR=24.8
Pod undocumented
202 0 my ($self) = @_;
203  
204 0 ▶ if ($self->{is_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is_array"}) 0 0 0
205 0 ▶ return ($self->{iterator} == 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'iterator'} == 1 ? : 0 0 0
206 }
207  
208 0 croak "'first' and 'last' methods aren't provided for hashiterators";
209 0 return;
210 }
211  
212 0 ▶sub last : method {
Subroutine last: not called CC=3 SCAR=24.8
Pod undocumented
213 0 my ($self) = @_;
214  
215 0 ▶ if ($self->{is_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is_array"}) 0 0 0
216 0 ▶ return ($self->{iterator} == $self->{count}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'iterator'} == $self->{'count'} ? : 0 0 0
217 }
218  
219 0 croak "'first' and 'last' methods aren't provided for hashiterators";
220 0 return;
221 }
222  
223  
224 1 ▶sub push : method {
Subroutine push: called CC=10 SCAR=36.5
Pod documented
225 1 my ($self, $k, $v) = @_;
226  
227 1 ▶ if ($self->{is_hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is_hash"}) 0 1 1
228 0 ▶ croak 'Usage $it->push(key => $value)' unless @_ >= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 3 0 0 0
229 0 ▶ croak 'Value is undefined' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
230 0 ▶ croak "Value isn't HASHREF or object"
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $v or builtin::blessed($v) 0 0 0
Condition0%0/3
"HASH" eq ref $v or builtin::blessed($v)
l!l&&r!l&&!r
000
MC/DC0%0/2
"HASH" eq ref $v or builtin::blessed($v)
"HASH" eq ref $v builtin::blessed($v)
Truth table0%0/3
A or B
A= "HASH" eq ref $vB= builtin::blessed($v)
ABresult
1X1
011
000
231 unless 'HASH' eq ref $v or blessed $v;
232 0 ▶ $self->{count}++ unless exists $self->{fetch}{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"fetch"}{$k} 0 0 0
233 0 $self->{fetch}{$k} = $v;
234 0 $self->is_changed(1);
235 0 return $self->get($k);
236 }
237  
238 1 ▶ croak "Value isn't defined" unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 1 1
239 1 ▶ croak "Value isn't HASHREF or object"
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $k or builtin::blessed($k) 0 1 1
Condition33%1/3
"HASH" eq ref $k or builtin::blessed($k)
l!l&&r!l&&!r
100
MC/DC0%0/2
"HASH" eq ref $k or builtin::blessed($k)
"HASH" eq ref $k builtin::blessed($k)
Truth table33%1/3
A or B
A= "HASH" eq ref $kB= builtin::blessed($k)
ABresult
1X1
011
000
240 unless 'HASH' eq ref $k or blessed $k;
241 1 push @{ $self->{fetch} }, $k;
242 1 $self->{count}++;
243 1 return $self->get(-1);
244 }
245  
246 1 ▶sub pop : method {
Subroutine pop: called CC=3 SCAR=11.1
Pod undocumented
247 1 my ($self) = @_;
248 1 ▶ croak "Can't use pop method with HASHREF iterator" if $self->{is_hash};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_hash"} 0 1 1
249 1 ▶ return undef unless $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"count"} 0 1 1
250  
251 1 my $item = $self->get(-1);
252 1 pop @{ $self->{fetch} };
253 1 $self->{count}--;
254 1 $item;
255 }
256  
257  
258 1 ▶sub find : method {
Subroutine find: called CC=3 SCAR=11.3
Pod undocumented
259 1 my ($self, $field, $value) = @_;
260  
261 1 $self->reset;
262 1 ▶ while(my $item = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $self->next) 2 0 2
263 2 ▶ return $item if $is->($item->$field, $value);
Branch: 1 branch
BranchTrueFalseTotal
if &$is($item->$field, $value) 1 1 2
264 }
265 0 return;
266 }
267  
268  
269  
270 package DBIx::DR::Iterator::Item;
271 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
272 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
273  
274 # to exclude this method from AUTOLOAD
275 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
276  
277 112 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=12 SCAR=26.9
278 112 our $AUTOLOAD;
279 112 my ($method) = $AUTOLOAD =~ /.*::(.*)/;
280 112 my ($self, $value) = @_;
281  
282 112 ▶ Carp::croak "Can't find method '$self->$method'" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 112 112
283 Carp::croak "Can't find method '$method' in this item"
284 112 ▶ unless exists $self->{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$method} 1 111 112
285  
286 111 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 4 107 111
287 4 my $is_changed;
288  
289 4 ▶ if (ref $value and ref $self->{$method}) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $value and ref $self->{$method}) { } 2 2 4
elsif (ref $value ne ref $self->{$method}) { } 1 1 2
elsif (defined $value and defined $self->{$method}) { } 1 0 1
elsif (defined $value xor defined $self->{$method}) { } 0 0 0
Condition100%3/3
ref $value and ref $self->{$method}
!ll&&!rl&&r
112
MC/DC100%2/2
ref $value and ref $self->{$method}
ref $value ref $self->{$method}
Truth table100%3/3
A and B
A= ref $valueB= ref $self->{$method}
ABresult
0X0
100
111
290 $is_changed = Scalar::Util::refaddr($value)
291 2 != Scalar::Util::refaddr($self->{$method});
292 } elsif(ref($value) ne ref($self->{$method})) {
293 1 $is_changed = 1;
294 ▶ } elsif(defined $value and defined $self->{$method}) {
Condition33%1/3
defined $value and defined $self->{$method}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $value and defined $self->{$method}
defined $value defined $self->{$method}
Truth table33%1/3
A and B
A= defined $valueB= defined $self->{$method}
ABresult
0X0
100
111
295 1 $is_changed = $value ne $self->{$method};
296 ▶ } elsif(defined $value xor defined $self->{$method}) {
Condition0%0/4
defined $value xor defined $self->{$method}
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
defined $value xor defined $self->{$method}
defined $value defined $self->{$method}
Truth table0%0/4
A xor B
A= defined $valueB= defined $self->{$method}
ABresult
110
101
011
000
297 0 $is_changed = 1;
298 }
299  
300 4 ▶ $self->is_changed(1) if $is_changed;
Branch: 1 branch
BranchTrueFalseTotal
if $is_changed 3 1 4
301 4 $self->{$method} = $value;
302 }
303  
304 111 return $self->{$method};
305 }
306  
307 22 ▶sub new {
Subroutine new: called CC=4 SCAR=14.6
308 22 my ($class, $object, $iterator) = @_;
309 22 ▶ return unless defined $object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object 0 22 22
310 22 ▶ Carp::croak "Usage: DBIx::DR::Iterator::Item->new(HASHREF [, iterator ])"
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $object 0 22 22
311 unless 'HASH' eq ref $object;
312 22 ▶ my $self = bless $object => ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0220
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
313 22 $self->{iterator} = $iterator;
314 22 Scalar::Util::weaken($self->{iterator});
315 22 $self->{is_changed} = 0;
316 22 return $self;
317 }
318  
319 8 ▶sub is_changed {
Subroutine is_changed: called CC=6 SCAR=18.0
320 8 my ($self, $value) = @_;
321 8 ▶ if (@_ > 1) {{
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 4 4 8
322 4 ▶ $self->{is_changed} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 3 1 4
323  
324 4 ▶ last unless $self->{is_changed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"is_changed"} 1 3 4
325 3 ▶ last unless Scalar::Util::blessed $self->{iterator};
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self->{"iterator"}) 0 3 3
326 3 ▶ last unless $self->{iterator}->can('is_changed');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"iterator"}->can("is_changed") 0 3 3
327 3 $self->{iterator}->is_changed( 1 );
328 }}
329 8 return $self->{is_changed};
330 }
331  
332 1 ▶sub can {
Subroutine can: called CC=3 SCAR=12.5
333 1 my ($self, $method) = @_;
334 1 ▶ return 1 if ref $self and exists $self->{$method};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and exists $self->{$method} 0 1 1
Condition33%1/3
ref $self and exists $self->{$method}
!ll&&!rl&&r
010
MC/DC0%0/2
ref $self and exists $self->{$method}
ref $self exists $self->{$method}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{$method}
ABresult
0X0
100
111
335 1 return $self->SUPER::can($method);
336 }
337  
338  
339 3 1;
340  
341 =head1 NAME
342  
343 DBIx::DR::Iterator - iterator for L<DBIx::DR>.
344  
345 =head1 SYNOPSIS
346  
347 my $it = DBIx::DR::Iterator->new($arrayref);
348  
349 printf "Rows count: %d\n", $it->count;
350  
351 while(my $row == $it->next) {
352 print "Row: %s\n", $row->field;
353 }
354  
355 my $row = $it->get(15); # element 15
356  
357  
358  
359 my $it = DBIx::DR::Iterator->new($hashref);
360  
361 printf "Rows count: %d\n", $it->count;
362  
363 while(my $row == $it->next) {
364 print "Row: %s\n", $row->field;
365 }
366  
367 my $row = $it->get('abc'); # element with key name eq 'abc'
368  
369  
370 =head1 DESCRIPTION
371  
372 The package constructs iterator from HASHREF or ARRAYREF value.
373  
374 =head1 Methods
375  
376 =head2 new
377  
378 Constructor.
379  
380 my $i = DBIx::DR::Iterator->new($arrayset [, OPTIONS ]);
381  
382 Where B<OPTIONS> are:
383  
384 =over
385  
386 =item -item => 'decamelized_obj_define';
387  
388 It will bless (or construct) row into specified class. See below.
389  
390 By default it constructs L<DBIx::DR::Iterator::Item> objects.
391  
392 =item -noitem_iter => TRUE|FALSE
393  
394 Don't pass iterator to item constructor.
395  
396 =item -keep_blessed => TRUE|FALSE
397  
398 Force store blessed item into iterator. Default value is C<true>.
399  
400 =back
401  
402 =head2 count
403  
404 Returns count of elements.
405  
406 =head2 is_changed
407  
408 Returns (or set) flag that one of contained elements was changed.
409  
410 =head2 exists(name|number)
411  
412 Returns B<true> if element 'B<name|number>' is exists.
413  
414 =head2 get(name|number)
415  
416 Returns element by 'B<name|number>'. It will throw exception if element
417 isn't L<exists|exists(name|number)>.
418  
419 =head2 next
420  
421 Returns next element or B<undef>.
422  
423 =head2 reset
424  
425 Resets internal iterator (that is used by L<next>).
426  
427 =head2 all
428  
429 Returns all elements (as an array).
430  
431 If You notice an argument it will extract specified fields:
432  
433 my @ids = $it->all('id');
434  
435 The same as:
436  
437 my @ids = map { $_->id } $it->all;
438  
439 =head2 grep
440  
441 Constructs new iterator that is subset of parent iterator.
442  
443 my $busy = $list->grep(sub { $_[0]->busy ? 1 : 0 });
444  
445 =head2 push
446  
447 Pushes one element into iterator.
448  
449 If You use HASH-iterator You have to note key name.
450  
451 =head3 Example
452  
453 $hiter->push(abc => { id => 1 });
454 $hiter->push(abc => $oiter->get('abc'));
455  
456 $aiter->push({ id => 1 });
457  
458 =head1 DBIx::DR::Iterator::Item
459  
460 One row. It has methods names coincident with field names. Also it has a few
461 additional methods:
462  
463 =head2 new
464  
465 Constructor. Receives two arguments: B<HASHREF> and link to
466 L<iterator|DBIx::DR::Iterator>.
467  
468 my $row = DBIx::DR::Iterator::Item->new({ id => 1 });
469 $row = DBIx::DR::Iterator::Item->new({ id => 1 }, $iterator); }
470  
471 =head2 iterator
472  
473 Returns (or set) iterator object. The link is created by constructor.
474 This is a L<weaken|Scalar::Util/weaken> link.
475  
476 =head2 is_changed
477  
478 Returns (or set) flag if the row has been changed. If You change any of
479 row's fields the flag will be set. Also iterator's flag will be set.
480  
481  
482 =head1 COPYRIGHT
483  
484 Copyright (C) 2011 Dmitry E. Oboukhov <unera@debian.org>
485 Copyright (C) 2011 Roman V. Nikolaev <rshadow@rambler.ru>
486  
487 This program is free software, you can redistribute it and/or
488 modify it under the terms of the Artistic License.
489  
490 =cut
491  
↑ Summary DR.pm »