File Coverage

blib/lib/Data/Conveyor/Service/Result/Tabular.pm
Criterion Covered Total %
statement 50 58 86.2
branch 12 20 60.0
condition 1 3 33.3
subroutine 9 11 81.8
pod 4 4 100.0
total 76 96 79.1


line stmt bran cond sub pod time code
1 1     1   723 use 5.008;
  1         6  
  1         48  
2 1     1   33 use strict;
  1         7  
  1         36  
3 1     1   7 use warnings;
  1         2  
  1         58  
4              
5             package Data::Conveyor::Service::Result::Tabular;
6             BEGIN {
7 1     1   19 $Data::Conveyor::Service::Result::Tabular::VERSION = '1.103130';
8             }
9             # ABSTRACT: Stage-based conveyor-belt-like ticket handling system
10              
11 1     1   911 use Text::Table;
  1         19626  
  1         15  
12 1     1   41 use Data::Miscellany 'trim';
  1         3  
  1         52  
13 1     1   6 use parent 'Data::Conveyor::Service::Result';
  1         2  
  1         11  
14             __PACKAGE__->mk_array_accessors(qw(headers rows));
15              
16             sub result_as_string {
17 0     0 1 0 my $self = shift;
18 0 0       0 unless ($self->rows_count) {
19 0         0 return "No results\n";
20             }
21 0         0 my @fields = $self->headers;
22 0         0 my $table = Text::Table->new(@fields);
23 0         0 $table->load($self->rows);
24 0         0 $table;
25             }
26              
27             # Given a LoH (list of hashes, a typical DBI result set), it populates the
28             # result object with those rows. It can also be a list of objects if those
29             # objects have methods that correspond to the headers.
30             sub set_from_rows {
31 3     3 1 13 my ($self, %args) = @_;
32 3         4 my ($did_set_headers, $count);
33 3 50       15 my $limit = $args{limit} if defined $args{limit};
34 3 50       13 my @fields = @{ $args{fields} } if defined $args{fields};
  3         12  
35 3         9 for my $row (@{ $args{rows} }) {
  3         9  
36 7 50 33     49 last if defined($limit) && ++$count > $limit;
37 7 100       19 unless ($did_set_headers) {
38 3 50       10 scalar @fields or @fields = sort keys %$row;
39 3         14 $self->headers(@fields);
40 3         59 $did_set_headers++;
41             }
42 7         8 my @values;
43 7         12 for (@fields) {
44 21 100       122 if (ref $row eq 'HASH') {
    100          
45 9         18 push @values => $row->{$_};
46             } elsif (UNIVERSAL::can($row, $_)) {
47 11         33 push @values => $row->$_;
48             } else {
49 1         23 throw Error::Hierarchy::Internal::CustomMessage(
50             custom_message => "can't set field [$_] from row [$row]");
51             }
52             }
53 6 50       31 $self->rows_push([ map { defined($_) ? $_ : '' } @values ]);
  18         53  
54             }
55 2         21 $self;
56             }
57 0     0 1 0 sub result { $_[0]->rows }
58              
59             sub result_as_list_of_hashes {
60 2     2 1 6 my $self = shift;
61 2         5 my @result;
62 2         9 my @headers = $self->headers; # don't call this accessor for every row
63 2         27 for my $row_ref ($self->rows) {
64 6         20 my $index = 0;
65 6         8 my %row_hash;
66 6         8 for my $header (@headers) {
67 18         37 $row_hash{$header} = $row_ref->[ $index++ ];
68             }
69 6         17 push @result => \%row_hash;
70             }
71 2 50       21 wantarray ? @result : \@result;
72             }
73             1;
74              
75              
76             __END__