File Coverage

lib/Google/RestApi/SheetsApi4/Range/Cell.pm
Criterion Covered Total %
statement 40 45 88.8
branch 10 12 83.3
condition 1 3 33.3
subroutine 11 13 84.6
pod 4 5 80.0
total 66 78 84.6


line stmt bran cond sub pod time code
1             package Google::RestApi::SheetsApi4::Range::Cell;
2              
3             our $VERSION = '1.0.3';
4              
5 1     1   631 use Google::RestApi::Setup;
  1         4  
  1         6  
6              
7 1     1   14099 use Try::Tiny qw( try catch );
  1         3  
  1         51  
8              
9 1     1   6 use aliased 'Google::RestApi::SheetsApi4::Range';
  1         2  
  1         9  
10              
11 1     1   205 use parent Range;
  1         3  
  1         9  
12              
13             # make sure the translated range refers to a single cell (no ':').
14             sub new {
15 135     135 1 340 my $class = shift;
16 135         515 my %self = @_;
17              
18             # this is fucked up, but want to support creating this object directly and also
19             # via the range::factory method, so have to handle both cases here. try to create
20             # the cell directly first (which could also come via the factory method, which will
21             # have already translated the address into a cell), and failing that, see if the
22             # range factory can create a cell (which will resolve any named or header references).
23             # this has the potential of looping between this and factory method.
24              
25             # if factory has already been used, then this should resolve here.
26 135         265 my $err;
27             try {
28 135     135   6285 state $check = compile(RangeCell);
29 135         377979 ($self{range}) = $check->($self{range});
30             } catch {
31 2     2   210 $err = $_;
32 135         1207 };
33 135 100       8703 return $class->SUPER::new(%self) if !$err;
34              
35             # see if the range passed can be translated to what we want via the factory.
36 2         15 my $factory_range;
37             try {
38 2     2   88 $factory_range = Google::RestApi::SheetsApi4::Range::factory(%self);
39 2     0   15 } catch {};
40 2 50 33     56 return $factory_range if $factory_range && $factory_range->isa(__PACKAGE__);
41              
42 0         0 LOGDIE sprintf("Unable to translate '%s' into a worksheet cell: $err", flatten_range($self{range}));
43             }
44              
45             sub values {
46 101     101 1 223 my $self = shift;
47 101         193 state $check = compile_named(
48             values => Str, { optional => 1 },
49             _extra_ => slurpy Any,
50             );
51 101         4152 my $p = named_extra($check->(@_));
52 101 50       378 $p->{values} = [[ $p->{values} ]] if defined $p->{values};
53 101         374 my $values = $self->SUPER::values(%$p);
54 101 100       871 return defined $values ? $values->[0]->[0] : undef;
55             }
56              
57             sub batch_values {
58 36     36 1 64 my $self = shift;
59              
60 36         69 state $check = compile_named(
61             values => Str, { optional => 1 },
62             );
63 36         2117 my $p = $check->(@_);
64              
65 36 100       764 $p->{values} = [[ $p->{values} ]] if $p->{values};
66 36         158 return $self->SUPER::batch_values(%$p);
67             }
68              
69             # is this 0 or infinity? return self if offset is 0, undef otherwise.
70             sub cell_at_offset {
71 4     4 0 436 my $self = shift;
72 4         13 state $check = compile(Int, DimColRow, { optional => 1 });
73 4         3935 my ($offset) = $check->(@_); # we're a cell, no dim required.
74 4 100       132 return $self if !$offset;
75 2         8 return;
76             }
77              
78             sub is_other_inside {
79 0     0 1   my $self = shift;
80 0           state $check = compile(HasRange);
81 0           my ($inside_range) = $check->(@_);
82 0           return $self->range() eq $inside_range->range();
83             }
84              
85             1;
86              
87             __END__
88              
89             =head1 NAME
90              
91             Google::RestApi::SheetsApi4::Range::Cell - Represents a cell within a Worksheet.
92              
93             =head1 DESCRIPTION
94              
95             A Range::Cell object modifies the behaviour of the parent Range object
96             to treat the values used within the range as a plain string instead of
97             arrays of arrays. This object will encapsulate the passed string value
98             into a [[$value]] array of arrays when interacting with Goolge API.
99              
100             See the description and synopsis at Google::RestApi::SheetsApi4.
101              
102             =head1 AUTHORS
103              
104             =over
105              
106             =item
107              
108             Robin Murray mvsjes@cpan.org
109              
110             =back
111              
112             =head1 COPYRIGHT
113              
114             Copyright (c) 2021, Robin Murray. All rights reserved.
115              
116             This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.