File Coverage

blib/lib/Script/Toolbox/TableO.pm
Criterion Covered Total %
statement 51 66 77.2
branch 20 30 66.6
condition 4 6 66.6
subroutine 11 13 84.6
pod 0 3 0.0
total 86 118 72.8


line stmt bran cond sub pod time code
1             package Script::Toolbox::TableO;
2             # vim: ts=4 sw=4 ai
3              
4 10     10   32 use strict;
  10         14  
  10         256  
5 10     10   30 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  10         8  
  10         6028  
6              
7             require Exporter;
8              
9             @ISA = qw(Exporter);
10             # Items to export into callers namespace by default. Note: do not export
11             # names by default without a very good reason. Use EXPORT_OK instead.
12             # Do not simply export all your public functions/methods/constants.
13             @EXPORT = qw(
14            
15             );
16             #$VERSION = '0.03';
17              
18              
19             # Preloaded methods go here.
20              
21             #-----------------------------------------------------------------------------
22             #-----------------------------------------------------------------------------
23             sub new
24             {
25 10     10 0 16 my $classname = shift;
26 10         20 my $self = {};
27 10         14 bless( $self, $classname );
28 10         25 $self->_init( @_ );
29 10         19 return $self;
30             }
31              
32             #-----------------------------------------------------------------------------
33             #-----------------------------------------------------------------------------
34             sub _init
35             {
36 10     10   9 my ($self, $param, $separator) = @_;
37              
38 10 50       13 return undef if( _noData( $param ) );
39 10         14 my $para = _normParam($param, $separator);
40              
41 10         33 my $form = Script::Toolbox::Util::Formatter->new( $para );
42 10         21 my $result= $form->matrix();
43 10 50 66     66 if( ref $param eq 'ARRAY' || !defined $param->{'sumCols'} ){
44 10         54 $self->{'result'} = $result;
45             }else{
46             $self->{'result'} = $form->sumBy($result, $param->{'sumCols'},
47 0         0 $param->{'notGroupBy'});
48             }
49             }
50              
51             #------------------------------------------------------------------------------
52             # $param must be a hash reference. This Hash must have a key "data".
53             # This key may point to:
54             # arrayref
55             # hashref
56             #------------------------------------------------------------------------------
57             sub _noData($)
58             {
59 10     10   9 my ($param) = @_;
60              
61 10 100       22 return 0 if( ref $param ne 'HASH' );
62 6 100       15 return 0 if( ref $param->{'data'} eq 'HASH' );
63 5 50       15 return 0 if( ref $param->{'data'} eq 'ARRAY');
64              
65 0 0       0 if( !defined $param->{'data'}[0] )
66             {
67 0         0 Log( "WARNING: no input data for Table()." );
68 0         0 return 1;
69             }
70 0         0 return 0;
71             }
72              
73             #------------------------------------------------------------------------------
74             # Valid Calls:
75             # [ "csvString", "csvString",...], undef
76             # [ "csvString", "csvString",...], separatorString
77             # [ "TitelString", [headArray], [dataArray],...], undef
78             # [ [dataArray],...], undef
79             # {title=>"", head=>[], data=>[[],[],...] }, undef
80             # {title=>"", head=>[], data=>[{},{},...] }, undef
81             # {title=>"", head=>[], data=>{r1=>{c1=>,c2=>,},r2=>{c1=>,c2=>,},}, undef
82             #------------------------------------------------------------------------------
83             sub _normParam($$)
84             {
85 10     10   10 my ($param, $separator) = @_;
86              
87 10 100       15 if( ref $param eq 'HASH' )
88             {
89             # keine Ahnung wozu: return _sepHash($param, $separator) if( _isCSV($param->{'data'}) );
90 6         7 return $param;
91             }
92 4 100       11 return _sepTitleHead($param) if( _isTitleHead($param) );
93 3 50       4 return _sepCSV($param, $separator) if( _isCSV($param, $separator) );
94 0         0 return { 'data' => $param };
95             }
96              
97             #------------------------------------------------------------------------------
98             #------------------------------------------------------------------------------
99             sub _sepHash($$)
100             {
101 0     0   0 my ($param,$separator) = @_;
102              
103 0         0 my $d = _sepCSV($param->{'data'}, $separator);
104 0         0 $param->{'data'} = $d->{'data'};
105 0         0 return $param;
106             }
107              
108             # ------------------------------------------------------------------------------
109             # Check if we found the special data array format.
110             # ["TitleString", [headString,headString,...],[data,...],...]
111             #------------------------------------------------------------------------------
112             sub _isTitleHead($)
113             {
114 4     4   71 my ($param) = @_;
115              
116 4 100 66     26 return 1 if( ref \$param->[0] eq 'SCALAR' && ref $param->[1] eq 'ARRAY' );
117 3         7 return 0;
118             }
119              
120             #------------------------------------------------------------------------------
121             # Transform the special data array
122             # ["TitleString", [headString,headString,...],[data,...],...]
123             # into hash format.
124             #------------------------------------------------------------------------------
125             sub _sepTitleHead($)
126             {
127 1     1   2 my ($param) = @_;
128              
129 1         2 my $title= splice @{$param}, 0,1;
  1         3  
130 1         2 my $head = splice @{$param}, 0,1;
  1         1  
131              
132             return {
133 1         4 'title' => $title,
134             'head' => $head,
135             'data' => $param
136             };
137             }
138              
139              
140             #------------------------------------------------------------------------------
141             # [[],[],...]
142             # [{},{},...]
143             # {r1=>{c1=>,c2=>,},r2=>{c1=>,c2=>,},}
144             #------------------------------------------------------------------------------
145             sub _isCSV($$)
146             {
147 3     3   2 my ($param, $separator) = @_;
148              
149 3 50       7 return 0 if( ref $param ne 'ARRAY' );
150              
151 3 100       8 $separator = ';' unless defined $separator; #FIXME default sep
152 3 50       41 return 1 if( $param->[0] =~ /$separator/ ); #assume it's a CSV record
153 0         0 return 0;
154             }
155              
156             #------------------------------------------------------------------------------
157             # Convert an array of CSV strings into an array of arrays.
158             #
159             # [ "a;b","c,d"] becomes
160             # [[a,b], [c,d]]
161             #------------------------------------------------------------------------------
162             sub _sepCSV($$)
163             {
164 3     3   4 my ($param, $separator) = @_;
165              
166 3 100       7 $separator = ';' if( !defined $separator);
167 3         3 my @R;
168 3         3 foreach my $l ( @{$param} )
  3         6  
169             {
170 9         26 my @r = split /$separator/, $l;
171 9         12 push @R, \@r;
172             }
173              
174 3         7 return { 'data' => \@R };
175             }
176              
177             #------------------------------------------------------------------------------
178             #------------------------------------------------------------------------------
179             sub asArray($){
180 10     10 0 13 my ($self) = @_;
181              
182 10         7 return @{$self->{'result'}};
  10         31  
183             }
184              
185             #------------------------------------------------------------------------------
186             #------------------------------------------------------------------------------
187             sub asString($$){
188 0     0 0   my ($self,$sep) = @_;
189              
190 0 0         $sep = defined $sep ? $sep : "\n";
191 0           return sprintf "%s", join $sep, @{$self->{'result'}};
  0            
192             }
193              
194             1;
195             __END__