File Coverage

lib/Google/RestApi/SheetsApi4/RangeGroup/Tie.pm
Criterion Covered Total %
statement 81 88 92.0
branch 13 18 72.2
condition n/a
subroutine 21 25 84.0
pod 14 16 87.5
total 129 147 87.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   485  
  1         2  
  1         18  
6             use Tie::Hash ();
7 1     1   19007 use aliased 'Google::RestApi::SheetsApi4::RangeGroup::Tie::Iterator';
  1         4  
  1         19  
8 1     1   6 use parent -norequire, 'Tie::StdHash';
  1         3  
  1         8  
9 1     1   103  
  1         2  
  1         6  
10             my $self = shift;
11              
12 1     1 1 11 my $tied = $self->spreadsheet()->tie(%{ $self->ranges() });
13             tied(%$tied)->default_worksheet($self->worksheet())
14 1         5 if $self->worksheet();
  1         5  
15 1 50       4  
16             return Iterator->new(@_, tied => $tied);
17             }
18 1         15  
19             my $self = shift;
20             my $range_group = $self->range_group();
21             return $range_group->values(@_);
22 3     3 1 15 }
23 3         13  
24 3         107 my $self = shift;
25             my $range_group = $self->range_group();
26             return $range_group->batch_values(@_);
27             }
28 0     0 1 0  
29 0         0 my $self = shift;
30 0         0 my %ranges = @_;
31             state $check = compile(slurpy ArrayRef[HasRange]);
32             $check->(CORE::values %ranges);
33             @{ $self->{ranges} }{ keys %ranges } = CORE::values %ranges;
34 21     21 1 36 return \%ranges;
35 21         94 }
36 21         39  
37 21         3481 my $self = shift;
38 21         378 state $check = compile(HashRef);
  21         63  
39 21         50 my ($tied) = $check->(@_);
40              
41             my $fetch_range = tied(%$tied)->fetch_range();
42             tied(%$tied)->fetch_range(1);
43 5     5 1 17 $self->add_ranges(%$tied);
44 5         12 tied(%$tied)->fetch_range($fetch_range);
45 5         646  
46             return $tied;
47 5         49 }
48 5         14  
49 5         18 # fetch_range(1) turns it on, fetch_range(0) turns it off.
50 5         22 # fetch_range() returns current setting. return $self so it can be
51             # chained with requests.
52 5         14 my $self = shift;
53             my $fetch_range = shift;
54             return $self->{fetch_range} if !defined $fetch_range;
55              
56             if ($fetch_range) {
57             $self->{fetch_range} = 1;
58             } else {
59 25     25 1 1264 delete $self->{fetch_range};
60 25         39 }
61 25 100       65  
62             return $self;
63 15 100       38 }
64 10         22  
65             my $self = shift;
66 5         19 return $self->spreadsheet()->range_group(CORE::values %{ $self->ranges() });
67             }
68              
69 15         37 my $self = shift;
70             state $check = compile(
71             HasMethods[qw(tie_ranges tie_cells)], { optional => 1 },
72             );
73 12     12 1 26 my ($worksheet) = $check->(@_);
74 12         44 $self->{worksheet} = $worksheet if $worksheet;
  12         32  
75             return $self->{worksheet};
76             }
77              
78 18     18 1 35 my $class = shift;
79 18         29 state $check = compile(
80             HasMethods[qw(range_group)],
81             );
82 18         1092 my ($spreadsheet) = $check->(@_);
83 18 100       282 my $self = bless {}, $class;
84 18         64 $self->{spreadsheet} = $spreadsheet;
85             return $self;
86             }
87              
88 16     16   36 my $self = shift;
89 16         33 my $a = keys %{ $self->ranges() }; # reset each() iterator
90             return each %{ $self->ranges() };
91             }
92 16         993  
93 16         201 return each %{ shift->ranges() };
94 16         59 }
95 16         53  
96             my $self = shift;
97             my $key = shift;
98             my $range = $self->ranges()->{$key}
99 7     7   16 or LOGDIE "No range found for key '$key'";
100 7         12 return $self->{fetch_range} ? $range : $range->values();
  7         20  
101 7         14 }
  7         15  
102              
103             my $self = shift;
104              
105 13     13   20 my ($key, $value) = @_;
  13         21  
106             if (!$self->ranges()->{$key}) {
107             my $worksheet = $self->worksheet()
108             or LOGDIE "No default worksheet provided for new range '$key'. Call default_worksheet() first.";
109 34     34   4600 my $tied = $worksheet->tie_ranges({ $key => $key });
110 34         55 $self->add_tied($tied);
111 34 50       64 }
112              
113 34 50       186 if (ref($value) eq "HASH") {
114             $self->ranges()->{$key}->batch_requests($value);
115             } else {
116             $self->ranges()->{$key}->batch_values(values => $value);
117 25     25   155 }
118              
119 25         60 return;
120 25 100       59 }
121 4 50       11  
122              
123 4         17 1;
124 4         40  
125              
126             =head1 NAME
127 25 50       90  
128 0         0 Google::RestApi::SheetsApi4::RangeGroup::Tie - Makes Ranges addressible via a hash key.
129              
130 25         47 =head1 DESCRIPTION
131              
132             A RangeGroup::Tie is a
133 25         102  
134             See the description and synopsis at Google::RestApi::SheetsApi4.
135              
136 0     0 0 0 =head1 SUBROUTINES
137 0     0 1 0  
138 8     8 1 67 =over
139 1     1 1 6  
140 0     0 0 0 =item iterator(%args);
141 125     125 1 475  
142 6     6 1 22 Returns an iterator for this tied range group. Each call to 'iterate'
143 14     14 1 41 (or 'next') will return a tied range group of individual cells
144             representing the iteration at that point.
145              
146             Any 'args' are passed through to RangeGroup::Iterator::new.
147              
148             =item values(%args);
149              
150             Gets the values for the underlying range group.
151              
152             Any 'args' are passed through to RangeGroup::Iterator::values.
153              
154             =item batch_values {
155              
156             Gets or sets the queued batch values for the underlying range group.
157              
158             Any 'args' are passed through to RangeGroup::Iterator::batch_values.
159              
160             =item add_ranges(<hash<Range>>);
161              
162             Adds the passed 'index => Range' pairs to this tied range group.
163              
164             =item add_tied(<hashref<Range>>);
165              
166             Adds the passed tied range group to this tied range group.
167              
168             =item fetch_range(<bool>);
169              
170             Sets the option to return the underlying ranges when fetching the
171             value. This allows you to set things like formatting on the underlying
172             ranges when fetching by index.
173              
174             =item range_group();
175              
176             Returns the parent RangeGroup object.
177              
178             =item default_worksheet();
179              
180             Sets the default worksheet to be used when auto-creating a new
181             index. Each index points to a range, and each range must have
182             a worksheet with which it is associated. So if a new index is
183             created, we need to know to which worksheet that index needs
184             to point.
185              
186             =item refresh_values();
187              
188             Calls the parent RangeGroup's refresh_values routine.
189              
190             =item submit_values();
191              
192             Calls the parent RangeGroup's submit_values routine.
193              
194             =item submit_requests();
195              
196             Calls the parent RangeGroup's submit_requests routine.
197              
198             =item ranges();
199              
200             Calls the parent RangeGroup's ranges routine.
201              
202             =item worksheet();
203              
204             Returns the parent default Worksheet object.
205              
206             =item spreadsheet();
207              
208             Returns the parent Spreadsheet object.
209              
210             =back
211              
212             =head1 AUTHORS
213              
214             =over
215              
216             =item
217              
218             Robin Murray mvsjes@cpan.org
219              
220             =back
221              
222             =head1 COPYRIGHT
223              
224             Copyright (c) 2021, Robin Murray. All rights reserved.
225              
226             This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.