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