TableDataRole/Source/Iterator.pm

filter: Statementstmt 88.7% 79 / 89 Branchbran 52.6% 20 / 38 Conditioncond 30.0% 3 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 8 totaltotal 69.2% 115 / 166 CC 26 SCAR 17.0
CC
26
Cov
74%
_get_row
11.2
_peek_row
8.1
get_column_names
6.2
CRAP 5.5
« TableDataRole/Munge/Filter.pm ↑ Summary TableDataRole/Munge/GroupRows.pm »
1 package TableDataRole::Source::Iterator;
2  
3 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 3 our $DATE = '2024-05-14'; # DATE
11 3 our $DIST = 'TableDataRoles-Standard'; # DIST
12 3 our $VERSION = '0.025'; # VERSION
13  
14 3 with 'TableDataRole::Spec::Basic';
15  
16 1 ▶sub _new {
Subroutine _new: called CC=4 SCAR=14.6
17 1 my ($class, %args) = @_;
18  
19 1 ▶ my $gen_iterator = delete $args{gen_iterator} or die "Please specify 'gen_iterator' argument";
Branch: 1 branch
BranchTrueFalseTotal
my $gen_iterator = delete $args{"gen_iterator"} or die "Please specify 'gen_iterator' argument" 1 0 1
20 1 ▶ my $gen_iterator_params = delete $args{gen_iterator_params} // {};
Condition50%1/2
delete $args{"gen_iterator_params"} // {}
l!l
01
MC/DC0%0/1
delete $args{"gen_iterator_params"} // {}
delete $args{"gen_iterator_params"}
Truth table50%1/2
A
A= delete $args{"gen_iterator_params"}
Aresult
11
00
21  
22 1 ▶ die "Unknown argument(s): ". join(", ", sort keys %args)
Branch: 1 branch
BranchTrueFalseTotal
if keys %args 0 1 1
23 if keys %args;
24  
25 1 bless {
26 gen_iterator => $gen_iterator,
27 gen_iterator_params => $gen_iterator_params,
28 iterator => undef,
29 pos => 0,
30 # buffer => undef,
31 # column_names => undef,
32 # column_idxs => undef,
33 }, $class;
34 }
35  
36 8 ▶sub _get_row {
Subroutine _get_row: called CC=6 SCAR=24.1
37 # get a row from iterator or buffer, and empty the buffer
38 8 my $self = shift;
39 8 ▶ if ($self->{buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'buffer'}) { } 0 8 8
40 0 my $row = delete $self->{buffer};
41 0 ▶ if (!ref($row) && $row == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $row and $row == -1) { } 0 0 0
Condition0%0/3
not ref $row and $row == -1
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $row and $row == -1
not ref $row $row == -1
Truth table0%0/3
not(A) and B
A= not ref $rowB= $row == -1
ABresult
0X0
100
111
42 0 return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
43 } else {
44 0 return $row;
45 }
46 } else {
47 8 ▶ $self->reset_iterator unless $self->{iterator};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"iterator"} 0 8 8
48 8 my $row = $self->{iterator}->();
49 8 ▶ return undef unless $row; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
unless $row 1 7 8
50 7 return $row;
51 }
52 }
53  
54 1 ▶sub _peek_row {
Subroutine _peek_row: called CC=6 SCAR=20.9
55 # get a row from iterator, put it in buffer. will return the existing buffer
56 # content if it exists.
57 1 my $self = shift;
58 1 ▶ unless ($self->{buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"buffer"}) 1 0 1
59 1 ▶ $self->reset_iterator unless $self->{iterator};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"iterator"} 1 0 1
60 1 ▶ $self->{buffer} = $self->{iterator}->() // -1;
Condition50%1/2
$self->{"iterator"}->() // -1
l!l
10
MC/DC0%0/1
$self->{"iterator"}->() // -1
$self->{"iterator"}->()
Truth table50%1/2
A
A= $self->{"iterator"}->()
Aresult
11
00
61 }
62 1 ▶ if (!ref($self->{buffer}) && $self->{buffer} == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $self->{'buffer'} and $self->{'buffer'} == -1) { } 0 1 1
Condition33%1/3
not ref $self->{"buffer"} and $self->{"buffer"} == -1
!ll&&!rl&&r
100
MC/DC0%0/2
not ref $self->{"buffer"} and $self->{"buffer"} == -1
not ref $self->{"buffer"} $self->{"buffer"} == -1
Truth table33%1/3
not(A) and B
A= not ref $self->{"buffer"}B= $self->{"buffer"} == -1
ABresult
0X0
100
111
63 0 return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
64 } else {
65 1 return $self->{buffer};
66 }
67 }
68  
69 1 ▶sub get_column_count {
Subroutine get_column_count: called CC=1 SCAR=0.0
Pod undocumented
70 1 my $self = shift;
71 1 $self->get_column_names;
72 1 scalar(@{ $self->{column_names} });
73 }
74  
75 4 ▶sub get_column_names {
Subroutine get_column_names: called CC=6 SCAR=18.3
Pod undocumented
76 4 my $self = shift;
77 4 ▶ unless ($self->{column_names}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"column_names"}) 1 3 4
78 1 my $row = $self->_peek_row;
79 1 ▶ unless ($row) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row) 0 1 1
80 0 ▶ return wantarray ? () : [];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
81 }
82 1 my $i = -1;
83 1 $self->{column_names} = [];
84 1 $self->{column_idxs} = {};
85 1 for (sort keys %$row) {
86 1 push @{ $self->{column_names} }, $_;
87 1 $self->{column_idxs}{$_} = ++$i;
88 }
89 }
90 4 ▶ wantarray ? @{ $self->{column_names} } : $self->{column_names};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 3 4
91 }
92  
93 0 ▶sub has_next_item {
Subroutine has_next_item: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my $self = shift;
95 0 ▶ $self->_peek_row ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_peek_row ? : 0 0 0
96 }
97  
98 2 ▶sub get_next_item {
Subroutine get_next_item: called CC=4 SCAR=14.0
Pod undocumented
99 2 my $self = shift;
100 2 $self->get_column_names;
101 2 my $row_hashref = $self->_get_row;
102 2 ▶ die "StopIteration" unless $row_hashref;
Branch: 1 branch
BranchTrueFalseTotal
unless $row_hashref 0 2 2
103 2 my $row_aryref = [];
104 2 for (keys %$row_hashref) {
105 2 my $idx = $self->{column_idxs}{$_};
106 2 ▶ next unless defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 0 2 2
107 2 $row_aryref->[$idx] = $row_hashref->{$_};
108 }
109 2 $self->{pos}++;
110 2 $row_aryref;
111 }
112  
113 2 ▶sub get_next_row_hashref {
Subroutine get_next_row_hashref: called CC=2 SCAR=7.0
Pod undocumented
114 2 my $self = shift;
115 2 my $row_hashref = $self->_get_row;
116 2 ▶ die "StopIteration" unless $row_hashref;
Branch: 1 branch
BranchTrueFalseTotal
unless $row_hashref 0 2 2
117 2 $self->{pos}++;
118 2 $row_hashref;
119 }
120  
121 1 ▶sub get_row_count {
Subroutine get_row_count: called CC=3 SCAR=11.0
Pod undocumented
122 1 my $self = shift;
123 1 $self->reset_iterator;
124 1 ▶ unless (defined $self->{row_count}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"row_count"}) 1 0 1
125 1 my $i = 0;
126 1 ▶ $i++ while $self->_get_row;
Branch: 1 branch
BranchTrueFalseTotal
$self->_get_row and do { ++$i } 3 1 4
127 1 $self->{row_count} = $i;
128 }
129 1 $self->{row_count};
130 }
131  
132 4 ▶sub reset_iterator {
Subroutine reset_iterator: called CC=1 SCAR=0.0
Pod undocumented
133 4 my $self = shift;
134 4 $self->{iterator} = $self->{gen_iterator}->(%{ $self->{gen_iterator_params} });
135 4 delete $self->{buffer};
136 4 $self->{pos} = 0;
137 }
138  
139 0 ▶sub get_iterator_pos {
Subroutine get_iterator_pos: not called CC=1 SCAR=6.9
Pod undocumented
140 0 my $self = shift;
141 0 $self->{pos};
142 }
143  
144 3 1;
145 # ABSTRACT: Get table data from an iterator
146  
147 __END__
« TableDataRole/Munge/Filter.pm ↑ Summary TableDataRole/Munge/GroupRows.pm »