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