File Coverage

blib/lib/Data/Tabular.pm
Criterion Covered Total %
statement 71 80 88.7
branch 11 18 61.1
condition 4 7 57.1
subroutine 17 19 89.4
pod 10 10 100.0
total 113 134 84.3


line stmt bran cond sub pod time code
1             # Copyright (C) 2003-2007, G. Allen Morris III, all rights reserved
2              
3 7     7   88614 use strict;
  7         18  
  7         323  
4 7     7   40 use warnings;
  7         13  
  7         481  
5              
6             package Data::Tabular;
7              
8             our $VERSION = '0.29';
9              
10 7     7   53 use Carp qw (croak);
  7         14  
  7         650  
11              
12 7     7   4916 use Data::Tabular::Group;
  7         23  
  7         256  
13 7     7   54 use Data::Tabular::Table::Extra;
  7         18  
  7         174  
14 7     7   41 use Data::Tabular::Table::Data;
  7         12  
  7         157  
15 7     7   37 use Data::Tabular::Config::Output;
  7         14  
  7         148  
16 7     7   5297 use Data::Tabular::Config::Extra;
  7         19  
  7         196  
17 7     7   4122 use Data::Tabular::Extra;
  7         22  
  7         6142  
18              
19             sub new
20             {
21 9     9 1 4647 my $caller = shift;
22 9   33     83 my $class = ref($caller) || $caller;
23 9         61 my $self = bless { @_ }, $class;
24              
25 9         125 my $extra = Data::Tabular::Config::Extra->new(
26             headers => $self->{extra_headers},
27             columns => $self->{extra},
28             types => $self->{extra_types},
29             );
30              
31 9 50       41 if (ref($caller)) {
32 0 0       0 die q|Don't know how to copy object.|
33             unless $caller->isa(__PACKAGE__);
34 0         0 $self = $caller->clone()
35             }
36 9         19 my $count = 0;
37 9 50       53 if ($self->{headers}) {
38 9 50       22 $self->{_all_headers} = [ (@{$self->{headers} || []}, @{$self->{extra_headers} || []}) ];
  9 100       38  
  9         65  
39 9         20 for my $elm (@{$self->{_all_headers}}) {
  9         27  
40 72         165 $self->{_header_off}->{$elm} = $count++;
41             }
42             }
43 9         165 $self->{data_table} =
44             Data::Tabular::Table::Data->new(
45             data => bless({
46             headers => $self->{headers},
47             rows => $self->{data},
48             types => $self->{types},
49             }, 'Data::Tabular::Data'),
50             );
51 9         324 $self->{extra_table} =
52             Data::Tabular::Table::Extra->new(
53             table => $self->{data_table},
54             extra => $extra,
55             );
56 9   100     45 $self->{group_by} ||= {};
57 9 50       32 if (my $group_by = $self->{group_by}) {
58 9 50       2510 if (ref $group_by eq 'HASH') {
59 9   50     139 $self->{grouped_table} = Data::Tabular::Group->new(
60             table => $self->{extra_table},
61             title => $self->{title} || 1,
62             %$group_by
63             );
64             } else {
65 0         0 die "group_by data must be a hash.";
66             }
67             } else {
68 0         0 die "FIXME";
69 0         0 $self->{grouped_table} = $self->{extra_table};
70             }
71 7         30 $self;
72             }
73              
74             sub headers
75             {
76 2     2 1 7 my $self = shift;
77              
78 2         11 $self->{extra_table}->headers;
79             }
80              
81             sub output
82             {
83 10     10 1 148994 my $self = shift;
84 10 100       26 my $args = { %{$self->{output} || {}}, @_ };
  10         96  
85              
86 10         59 my $output = Data::Tabular::Config::Output->new(
87 7         122 headers => [ @{$self->{headers}},
88 10 100       32 $self->{extra_headers} ? @{$self->{extra_headers}} : keys %{$self->{extra}} ],
  3         50  
89             %$args,
90             );
91 10         115 $output;
92             }
93              
94             sub grouped
95             {
96 9     9 1 28 my $self = shift;
97              
98 9         106 $self->{grouped_table};
99             }
100              
101             sub extra_table
102             {
103 0     0 1 0 my $self = shift;
104              
105 0         0 $self->{extra_table};
106             }
107              
108             sub data_table
109             {
110 0     0 1 0 my $self = shift;
111              
112 0         0 $self->{data_table};
113             }
114              
115             sub html
116             {
117 2     2 1 818 my $self = shift;
118              
119 2         1673 require Data::Tabular::Output::HTML;
120              
121 2         18 return Data::Tabular::Output::HTML->new(
122             table => $self->grouped,
123             output => $self->output,
124             @_,
125             );
126             }
127              
128             sub xls
129             {
130 3     3 1 509837 my $self = shift;
131 3         2280 require Data::Tabular::Output::XLS;
132              
133 3         28 return Data::Tabular::Output::XLS->new(
134             table => $self->grouped,
135             output => $self->output,
136             @_,
137             );
138             }
139              
140             sub txt
141             {
142 3     3 1 420 my $self = shift;
143 3         3407 require Data::Tabular::Output::TXT;
144              
145 3         21 return Data::Tabular::Output::TXT->new(
146             table => $self->grouped,
147             output => $self->output,
148             @_,
149             );
150             }
151              
152             sub csv
153             {
154 1     1 1 7 my $self = shift;
155 1         866 require Data::Tabular::Output::CSV;
156              
157 1         7 return Data::Tabular::Output::CSV->new(
158             table => $self->grouped,
159             output => $self->output,
160             @_,
161             );
162             }
163              
164             1;
165             __END__