blib/lib/Yukki/Web/Plugin/Spreadsheet.pm

filter: Statementstmt 67.6% 69 / 102 Branchbran 18.1% 4 / 22 Conditioncond 31.5% 6 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 77.7% 14 / 18 Podpod 100% 7 / 7 totaltotal 55.8% 100 / 179 CC 20 SCAR 23.7
CC
20
Cov
55%
lookup_name
42.0
__ANON__
12.0
initialize_context
7.3
CRAP 10.7
« blib/lib/Yukki/Web/Controller/Attachment.pm ↑ Summary blib/lib/Yukki/Web/Router/Route/Match.pm »
1 package Yukki::Web::Plugin::Spreadsheet;
2 1 $Yukki::Web::Plugin::Spreadsheet::VERSION = '0.991_007'; # TRIAL
3 1 $Yukki::Web::Plugin::Spreadsheet::VERSION = '0.991007';
4 1 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Types::Standard qw( HashRef Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 extends 'Yukki::Web::Plugin';
13  
14 # ABSTRACT: add spreadsheet functionality to wiki pages
15  
16 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Yukki::Error qw( http_throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 has format_helpers => (
22 is => 'ro',
23 isa => HashRef[Str],
24 required => 1,
25 1 ▶ default => sub { +{
Subroutine __ANON__: called CC=1 SCAR=0.0
26 1 '=' => 'spreadsheet_eval',
27 } },
28 1 );
29  
30 1 with 'Yukki::Web::Plugin::Role::FormatHelper';
31  
32  
33 1 ▶sub initialize_context {
Subroutine initialize_context: called CC=6 SCAR=19.9
Pod documented
34 1 my ($self, $ctx) = @_;
35  
36 1 ▶ $ctx->stash->{'Spreadsheet.sheet'} //= Spreadsheet::Engine->new;
Condition33%1/3
$ctx->stash->{"Spreadsheet.sheet"} //= "Spreadsheet::Engine"->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$ctx->stash->{"Spreadsheet.sheet"} //= "Spreadsheet::Engine"->new
$ctx->stash->{"Spreadsheet.sheet"} "Spreadsheet::Engine"->new
Truth table33%1/3
A //= B
A= $ctx->stash->{"Spreadsheet.sheet"}B= "Spreadsheet::Engine"->new
ABresult
1X1
011
000
37 1 ▶ $ctx->stash->{'Spreadsheet.map'} //= {};
Condition50%1/2
$ctx->stash->{"Spreadsheet.map"} //= {}
l!l
01
MC/DC0%0/1
$ctx->stash->{"Spreadsheet.map"} //= {}
$ctx->stash->{"Spreadsheet.map"}
Truth table50%1/2
A
A= $ctx->stash->{"Spreadsheet.map"}
Aresult
11
00
38 1 ▶ $ctx->stash->{'Spreadsheet.rowmap'} //= {};
Condition50%1/2
$ctx->stash->{"Spreadsheet.rowmap"} //= {}
l!l
01
MC/DC0%0/1
$ctx->stash->{"Spreadsheet.rowmap"} //= {}
$ctx->stash->{"Spreadsheet.rowmap"}
Truth table50%1/2
A
A= $ctx->stash->{"Spreadsheet.rowmap"}
Aresult
11
00
39 1 ▶ $ctx->stash->{'Spreadsheet.nextrow'} //= 'A';
Condition50%1/2
$ctx->stash->{"Spreadsheet.nextrow"} //= "A"
l!l
01
MC/DC0%0/1
$ctx->stash->{"Spreadsheet.nextrow"} //= "A"
$ctx->stash->{"Spreadsheet.nextrow"}
Truth table50%1/2
A
A= $ctx->stash->{"Spreadsheet.nextrow"}
Aresult
11
00
40 1 ▶ $ctx->stash->{'Spreadsheet.nextcol'} //= {};
Condition50%1/2
$ctx->stash->{"Spreadsheet.nextcol"} //= {}
l!l
01
MC/DC0%0/1
$ctx->stash->{"Spreadsheet.nextcol"} //= {}
$ctx->stash->{"Spreadsheet.nextcol"}
Truth table50%1/2
A
A= $ctx->stash->{"Spreadsheet.nextcol"}
Aresult
11
00
41  
42 1 return $ctx->stash->{'Spreadsheet.sheet'};
43 }
44  
45  
46 1 ▶sub setup_spreadsheet {
Subroutine setup_spreadsheet: called CC=2 SCAR=6.9
Pod documented
47 1 my ($self, $params) = @_;
48  
49 1 my $ctx = $params->{context};
50 1 my $file = $params->{file};
51 1 my $arg = $params->{arg};
52  
53 1 my $sheet = $ctx->stash->{'Spreadsheet.sheet'};
54 1 my $row = $self->row($ctx, $file);
55  
56 1 my ($name, $formula) = $arg =~ /^(?:([\w -]+):)?(.*)/;
57  
58 1 my $new_cell = $row . ($sheet->raw->{sheetattribs}{lastrow} + 1);
59  
60 1 ▶ $self->cell($ctx, $file, $name, $new_cell) if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 1 1
61  
62 1 return ($new_cell, $name, $formula);
63 }
64  
65  
66 1 ▶sub row {
Subroutine row: called CC=2 SCAR=7.0
Pod documented
67 1 my ($self, $ctx, $file) = @_;
68  
69 1 my $rowmap = $ctx->stash->{'Spreadsheet.rowmap'};
70  
71 my $row = $rowmap->{ $file->repository_name }{ $file->full_path }
72 1 ▶ // $ctx->stash->{'Spreadsheet.nextrow'}++;
Condition50%1/2
$$rowmap{$file->repository_name}{$file->full_path} // $ctx->stash->{"Spreadsheet.nextrow"}++
l!l
01
MC/DC0%0/1
$$rowmap{$file->repository_name}{$file->full_path} // $ctx->stash->{"Spreadsheet.nextrow"}++
$$rowmap{$file->repository_name}{$file->full_path}
Truth table50%1/2
A
A= $$rowmap{$file->repository_name}{$file->full_path}
Aresult
11
00
73  
74 1 return $rowmap->{ $file->repository_name }{ $file->full_path } = $row;
75 }
76  
77  
78 0 ▶sub cell {
Subroutine cell: not called CC=2 SCAR=17.9
Pod documented
79 0 my ($self, $ctx, $file, $name, $new_cell) = @_;
80 0 my $map = $ctx->stash->{'Spreadsheet.map'};
81 0 ▶ $map->{ $file->repository_name }{ $file->full_path }{ $name } = $new_cell
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_cell 0 0 0
82 if defined $new_cell;
83 0 return $map->{ $file->repository_name }{ $file->full_path }{ $name };
84 }
85  
86  
87 0 ▶sub lookup_name {
Subroutine lookup_name: not called CC=6 SCAR=37.4
Pod documented
88 0 my ($self, $params) = @_;
89  
90 0 my $ctx = $params->{context};
91 0 my $file = $params->{file};
92 0 my $name = $params->{name};
93  
94 0 ▶ if ($name =~ /!/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /!/u) 0 0 0
95 0 my ($path, $name) = split /!/, $name, 2;
96  
97 0 my $repository_name;
98 0 ▶ if ($path =~ /^(\w+):/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /^(\w+):/u) { } 0 0 0
99 0 ($repository_name, $path) = split /:/, $path, 2;
100 }
101 else {
102 0 $repository_name = $file->repository_name;
103 }
104  
105 0 $path = $self->app->munge_label($path);
106  
107 0 my $other_repo = $self->model('Repository', {
108 name => $repository_name,
109 });
110  
111 0 my $other_file = $other_repo->file({
112 full_path => $path,
113 });
114  
115 0 ▶ $self->load_spreadsheet($ctx, $other_file)
Branch: 1 branch
BranchTrueFalseTotal
unless $other_file->repository_name eq $file->repository_name and $other_file->full_path eq $file->full_path 0 0 0
Condition0%0/3
$other_file->repository_name eq $file->repository_name and $other_file->full_path eq $file->full_path
!ll&&!rl&&r
000
MC/DC0%0/2
$other_file->repository_name eq $file->repository_name and $other_file->full_path eq $file->full_path
$other_file->repository_name eq $file->repository_name $other_file->full_path eq $file->full_path
Truth table0%0/3
A and B
A= $other_file->repository_name eq $file->repository_nameB= $other_file->full_path eq $file->full_path
ABresult
0X0
100
111
116 unless $other_file->repository_name eq $file->repository_name
117 and $other_file->full_path eq $file->full_path;;
118  
119 0 return $self->cell($ctx, $other_file, $name);
120 }
121  
122 0 my $cell = $self->cell($ctx, $file, $name);
123  
124 0 ▶ http_throw('unknown name') if not defined $cell;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cell 0 0 0
125  
126 0 return $cell;
127 }
128  
129  
130 1 ▶sub spreadsheet_eval {
Subroutine spreadsheet_eval: called CC=4 SCAR=16.7
Pod documented
131 1 my ($self, $params) = @_;
132  
133 1 my $ctx = $params->{context};
134 1 my $file = $params->{file};
135 1 my $arg = $params->{arg};
136  
137 1 my $sheet = $self->initialize_context($ctx);
138  
139 1 my ($new_cell, $name, $formula) = $self->setup_spreadsheet($params);
140  
141 1 my $error = 0;
142  
143 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=1.2
144 1 $formula =~ s/ \[ ([^\]]+) \] /
145 0 $self->lookup_name({
146 %$params,
147 name => $1,
148 })
149 /gex;
150 }
151  
152 0 ▶ catch {
Subroutine __ANON__: not called CC=3 SCAR=24.8
153 0 $error++;
154 0 ▶ if (blessed $_ and $_->isa('Yukki::Error')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_) and $_->isa('Yukki::Error')) { } 0 0 0
Condition0%0/3
builtin::blessed($_) and $_->isa("Yukki::Error")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) and $_->isa("Yukki::Error")
builtin::blessed($_) $_->isa("Yukki::Error")
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->isa("Yukki::Error")
ABresult
0X0
100
111
155 0 my $msg = $_->message;
156 0 $sheet->execute("set $new_cell constant e#NAME? $msg");
157 }
158 else {
159 0 die $_;
160 }
161 1 };
162  
163 1 ▶ $sheet->execute("set $new_cell formula $formula") unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 1 0 1
164 1 $sheet->recalc;
165  
166 1 my $raw = $sheet->raw;
167 1 ▶ my $attrs = defined $name ? qq[ id="spreadsheet-$name"] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 1 1
168 1 my $value;
169 1 ▶ if ($raw->{cellerrors}{ $new_cell }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw->{'cellerrors'}{$new_cell}) { } 0 1 1
170 0 $attrs .= qq[ title="$arg (ERROR: $raw->{formulas}{ $new_cell })"]
171 . qq[ class="spreadsheet-cell error" ];
172 0 $value = $raw->{cellerrors}{ $new_cell };
173 }
174 else {
175 1 $attrs .= qq[ title="$arg" class="spreadsheet-cell"];
176 1 $value = $raw->{datavalues}{ $new_cell };
177 }
178  
179 1 return qq[<span$attrs>$value</span>];
180 }
181  
182  
183 0 ▶sub load_spreadsheet {
Subroutine load_spreadsheet: not called CC=2 SCAR=17.9
Pod documented
184 0 my ($self, $ctx, $file) = @_;
185 0 ▶ http_throw('no such spreadsheet exists') unless $file->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $file->exists 0 0 0
186 0 $file->fetch_formatted($ctx);
187 }
188  
189 1 1;
190  
191 __END__
« blib/lib/Yukki/Web/Controller/Attachment.pm ↑ Summary blib/lib/Yukki/Web/Router/Route/Match.pm »