Tabular/Table/Group.pm

filter: Statementstmt 81.6% 111 / 136 Branchbran 53.1% 17 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 78.9% 15 / 19 Podpod 21.4% 3 / 14 totaltotal 72.6% 146 / 201 CC 22 SCAR 17.5
CC
22
Cov
76%
get_all
20.0
rows
9.1
column_sum
4.2
CRAP 5.8
« Tabular/Row/General.pm ↑ Summary Tabular/Group.pm »
1 # Copyright (C) 2003-2007, G. Allen Morris III, all rights reserved
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package
6 Data::Tabular::Table::Group;
7  
8 8 ▶use base 'Data::Tabular::Table';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use Data::Tabular::Group::Interface;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new
13 56 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
14 56 my $class = shift;
15 56 bless { @_ }, $class;
16 }
17  
18 sub _row_count
19 0 ▶{
Subroutine _row_count: not called CC=1 SCAR=6.9
20 0 my $self = shift;
21  
22 0 die;
23 }
24  
25 sub sum_list
26 0 ▶{
Subroutine sum_list: not called CC=1 SCAR=6.9
Pod undocumented
27 0 my $self = shift;
28 0 $self->group->sum_list;
29 }
30  
31 sub column_sum
32 89 ▶{
Subroutine column_sum: called CC=4 SCAR=14.3
Pod undocumented
33 89 my $self = shift;
34 89 my $column_name = shift;
35 89 my $ret;
36 89 ▶ if ($ret = $self->{memo}->{$column_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret = $self->{"memo"}{$column_name}) 0 89 89
37 0 return $ret;
38 }
39 89 my $formula = '=SUM(';
40 89 my $sum = 0;
41 89 my @rows;
42 89 for my $row ($self->raw_rows) {
43 355 push(@rows, $row->id());
44 355 my $next = $row->get($column_name);
45 355 ▶ if (UNIVERSAL::isa($next, 'Data::Tabular::Formula')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($next, "Data::Tabular::Formula")) 0 355 355
46 0 die;
47 0 $next = $next->{html};
48 }
49 355 $sum += $next;
50 }
51 89 require Data::Tabular::Type::Formula;
52  
53 89 $ret = Data::Tabular::Type::Formula->new(
54 data => $sum,
55 type => 'sum',
56 column => $column_name,
57 rows => \@rows,
58 );
59  
60 89 $self->{memo}->{$column_name} = $ret;
61 89 $ret;
62 }
63  
64 sub column_average
65 5 ▶{
Subroutine column_average: called CC=4 SCAR=14.2
Pod undocumented
66 5 my $self = shift;
67 5 my $column_name = shift;
68 5 my $ret;
69 5 ▶ if ($ret = $self->{memoa}->{$column_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret = $self->{"memoa"}{$column_name}) 0 5 5
70 0 return $ret;
71 }
72 5 my $formula = '=SUM(';
73 5 my $sum = 0;
74 5 my @rows;
75 5 my $count = 0;
76 5 for my $row ($self->raw_rows) {
77 55 push(@rows, $row->id());
78 55 my $next = $row->get($column_name);
79 55 ▶ if (UNIVERSAL::isa($next, 'Data::Tabular::Formula')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($next, "Data::Tabular::Formula")) 0 55 55
80 0 die;
81 0 $next = $next->{html};
82 }
83 55 $sum += $next;
84 55 $count++;
85 }
86 5 require Data::Tabular::Type::Formula;
87  
88 5 $ret = Data::Tabular::Type::Formula->new(
89 data => $sum / $count,
90 count => $count,
91 type => 'avg',
92 column => $column_name,
93 rows => \@rows,
94 );
95  
96 5 $self->{memoa}->{$column_name} = $ret;
97 5 $ret;
98 }
99  
100 sub group
101 256 ▶{
Subroutine group: called CC=1 SCAR=0.0
Pod undocumented
102 256 my $self = shift;
103 256 $self->{group};
104 }
105  
106 sub headers
107 256 ▶{
Subroutine headers: called CC=1 SCAR=0.0
Pod documented
108 256 my $self = shift;
109 256 $self->group->headers;
110 }
111  
112 sub all_headers
113 0 ▶{
Subroutine all_headers: not called CC=1 SCAR=6.9
Pod undocumented
114 0 my $self = shift;
115  
116 0 $self->group->headers;
117 }
118  
119 sub raw_rows
120 362 ▶{
Subroutine raw_rows: called CC=4 SCAR=13.9
Pod undocumented
121 362 my $self = shift;
122 362 my @ret = ();
123  
124 362 for my $value (@{$self->{data}}) {
125 678 ▶ next unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 678 678
126 678 ▶ if ($value->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->isa(__PACKAGE__)) { } 268 410 678
127 268 push(@ret, $value->raw_rows);
128 } else {
129 410 push(@ret, $value);
130 }
131 }
132 362 @ret;
133 }
134  
135 sub level
136 112 ▶{
Subroutine level: called CC=1 SCAR=0.0
Pod undocumented
137 112 shift->{level};
138 }
139  
140 sub groups
141 112 ▶{
Subroutine groups: called CC=1 SCAR=0.0
Pod undocumented
142 112 my $self = shift;
143 112 $self->{group}->{groups};
144 }
145  
146 sub get_column
147 32 ▶{
Subroutine get_column: called CC=1 SCAR=0.0
Pod undocumented
148 32 my $self = shift;
149 32 $self->get(@_);
150 }
151  
152 sub get
153 76 ▶{
Subroutine get: called CC=2 SCAR=7.0
Pod undocumented
154 76 my $self = shift;
155 76 my $key = shift;
156  
157 76 my $ret = $self->{data}->[0]->get_column($key);
158 76 my $ret1 = $self->{data}->[-1]->get_column($key);
159 76 ▶ unless ($ret->{data} eq $ret1->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret->{"data"} eq $ret1->{"data"}) 0 76 76
160 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 0 die Dumper $ret, $ret1;
162 }
163  
164 76 $ret;
165 }
166  
167 sub get_all
168 0 ▶{
Subroutine get_all: not called CC=4 SCAR=30.0
Pod undocumented
169 0 my $self = shift;
170 0 my $key = shift;
171 0 my $temp = {};
172 0 my @ret;
173  
174 0 for my $value (@{$self->{data}}) {
175 0 ▶ next unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
176 0 my $item = $value->get_column($key);
177 0 ▶ unless ($temp->{$item}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($temp->{$item}++) 0 0 0
178 0 push(@ret, $item);
179 }
180 }
181  
182 0 \@ret;
183 }
184  
185 sub rows
186 56 ▶{
Subroutine rows: called CC=9 SCAR=22.1
Pod documented
187 56 my $self = shift;
188 56 my $info = {
189 @_,
190 row => 0,
191 pre_once => 0,
192 x => 'ok'
193 };
194 56 ▶ return $self->{rows} if $self->{rows};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rows"} 0 56 56
195  
196 56 ▶ return unless $self->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"data"} 0 56 56
197 56 ▶ return unless $self->{data}->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"data"}[0] 0 56 56
198  
199 56 my $rid = 1;
200 56 my @ret = ();
201 56 ▶ if (my $action = $self->groups->[$self->level]->{pre}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $action = $self->groups->[$self->level]{"pre"}) 56 0 56
202 56 my $grouper = Data::Tabular::Group::Interface->new(
203 group => $self,
204 );
205 56 my @data = $action->($grouper);
206 56 eval {
207 56 push(@ret, map({ $_->{row_id} = $rid++; $_->{output} = $info->{output}; $_ } @data));
208 };
209 0 ▶ if ($@) { warn $@; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 56 56
210 }
211  
212 56 for my $value (@{$self->{data}}) {
213 141 ▶ if (my $x = $value->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $x = $value->isa(__PACKAGE__)) { } 44 97 141
214 44 push(@ret, map({ $_->{row_id} = $rid++; $_->{output} = $info->{output}; $_ } ($value->rows(%$info))));
215 } else {
216 97 push(@ret, map({ $_->{row_id} = $rid++; $_->{output} = $info->{output}; $_ } ($value)));
217 }
218 }
219  
220 56 $info->{x} = 'ok';
221 56 ▶ if (my $action = $self->groups->[$self->level]->{post}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $action = $self->groups->[$self->level]{"post"}) 48 8 56
222 48 my $grouper = Data::Tabular::Group::Interface->new(
223 group => $self,
224 table => $self,
225 );
226 48 my @data = $action->($grouper);
227 48 push(@ret, map({ $_->{row_id} = $rid++; $_->{output} = $info->{output}; $_ } @data));
228 }
229 56 $self->{rows} = \@ret;
230 56 @{$self->{rows}};
231 }
232  
233 8 1;
234 __END__
« Tabular/Row/General.pm ↑ Summary Tabular/Group.pm »