File Coverage

lib/Google/RestApi/SheetsApi4/RangeGroup/Iterator.pm
Criterion Covered Total %
statement 22 22 100.0
branch 2 4 50.0
condition 3 5 60.0
subroutine 7 7 100.0
pod 5 5 100.0
total 39 43 90.7


line stmt bran cond sub pod time code
1             package Google::RestApi::SheetsApi4::RangeGroup::Iterator;
2              
3             our $VERSION = '1.0.3';
4              
5 1     1   582 use Google::RestApi::Setup;
  1         3  
  1         8  
6              
7 1     1   13577 use aliased 'Google::RestApi::SheetsApi4::RangeGroup';
  1         2  
  1         8  
8              
9             sub new {
10 2     2 1 6 my $class = shift;
11 2         9 state $check = compile_named(
12             range_group => HasMethods[qw(ranges)],
13             dim => StrMatch[qr/^(col|row)$/], { default => 'row' },
14             by => PositiveInt, { default => 1 },
15             from => PositiveOrZeroInt, { optional => 1 },
16             to => PositiveOrZeroInt, { optional => 1 },
17             );
18 2         7807 my $self = $check->(@_);
19 2   100     184 $self->{current} = delete $self->{from} || 0;
20 2         19 return bless $self, $class;
21             }
22              
23             sub iterate {
24 6     6 1 16 my $self = shift;
25              
26 6 50 33     38 return if defined $self->{to} && $self->{current} + 1 > $self->{to};
27              
28             my @ranges = map {
29 6         28 $_->cell_at_offset($self->{current}, $self->{dim});
  14         69  
30             } $self->range_group()->ranges();
31 6 50       18 return if grep { undef; } @ranges;
  14         40  
32              
33 6         26 $self->{current} += $self->{by};
34              
35 6         33 return $self->spreadsheet()->range_group(@ranges);
36             }
37 4     4 1 17 sub next { iterate(@_); }
38              
39 12     12 1 51 sub range_group { shift->{range_group}; }
40 6     6 1 20 sub spreadsheet { shift->range_group()->spreadsheet(); }
41              
42             1;
43              
44             __END__
45              
46             =head1 NAME
47              
48             Google::RestApi::SheetsApi4::RangeGroup::Iterator - An iterator for a group of Ranges.
49              
50             =head1 DESCRIPTION
51              
52             A RangeGroup::Iterator is used to iterate through a range group, returning
53             a range group of cells, one group at a time.
54              
55             Iterating over a range group assumes the range group is made up of
56             a series of ranges that implement a 'cell_at_offset' subroutine. This
57             routine is called on each iteration to return a Cell object that
58             represents that iteration at a particular offset. The offset increases
59             for each iteration.
60              
61             See the description and synopsis at Google::RestApi::SheetsApi4.
62              
63             =head1 SUBROUTINES
64              
65             =over
66              
67             =item new(range => <Range>, dim => <dimension>, by => <int>);
68              
69             Creates a new Iterator object for the given range group.
70              
71             range_group: The parent range group for this iterator.
72             by: The number of cells to skip between each iteration. Defaults to 1.
73             from: The offset from which to start the iteration. Defaults to 0.
74             to: The offset to stop the iteration. No default.
75              
76             'by' is used to allow you to only return, say, every second cell in the
77             iteration ('by' = '2').
78              
79             If you don't specify a 'to' then you will need to have a method to
80             end the iteration yourself (e.g. 'last if cell value eq ""') or you
81             will iterate off the end of the sheet and get a 403 back.
82              
83             You would not normally call this directly, you'd use the RangeGroup::iterator
84             method to create the iterator object for you.
85              
86             =item iterate();
87              
88             Return the next group of cells in the iteration sequence.
89              
90             =item next();
91              
92             An alias for iterate().
93              
94             =item range_group();
95              
96             Returns the RangeGroup object for this iterator.
97              
98             =item spreadsheet();
99              
100             Returns the Spreadsheet object for this iterator.
101              
102             =back
103              
104             =head1 AUTHORS
105              
106             =over
107              
108             =item
109              
110             Robin Murray mvsjes@cpan.org
111              
112             =back
113              
114             =head1 COPYRIGHT
115              
116             Copyright (c) 2021, Robin Murray. All rights reserved.
117              
118             This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.