File Coverage

blib/lib/Business/UPS/Tracking/Role/Print.pm
Criterion Covered Total %
statement 17 58 29.3
branch 0 18 0.0
condition 0 15 0.0
subroutine 6 8 75.0
pod 1 1 100.0
total 24 100 24.0


line stmt bran cond sub pod time code
1             # ============================================================================
2             package Business::UPS::Tracking::Role::Print;
3             # ============================================================================
4 1     1   526 use utf8;
  1         2  
  1         4  
5 1     1   35 use 5.0100;
  1         3  
6              
7 1     1   3 use Moose::Role;
  1         1  
  1         9  
8 1     1   3568 no if $] >= 5.017004, warnings => qw(experimental::smartmatch);
  1         1  
  1         7  
9              
10 1     1   547 use Text::SimpleTable;
  1         1304  
  1         382  
11              
12             =encoding utf8
13              
14             =head1 NAME
15              
16             Business::UPS::Tracking::Role::Serialize - Serialize objects
17            
18             =head1 DESCRIPTION
19              
20             This role provides methods to serialize objects into a L<Text::SimpleTable>
21             object.
22              
23             =head1 METHODS
24              
25             =head3 printall
26              
27             $tale = $self->printall($tale);
28             say $tale->draw();
29              
30             Serialize an object into a table.
31              
32             =cut
33              
34             sub printall {
35 0     0 1   my ($self,$table) = @_;
36            
37 0   0       $table ||= Text::SimpleTable->new(27,44);
38            
39 0           foreach my $attribute ($self->meta->get_all_attributes) {
40             next
41 0 0         unless $attribute->does('Printable');
42            
43 0           my $value = $attribute->get_value($self);
44            
45             next
46 0 0         unless defined $value;
47            
48 0 0         my $name = $attribute->has_documentation ?
49             $attribute->documentation() :
50             $attribute->name;
51            
52             # if ($attribute->has_printer) {
53             # $value = $attribute->printer->($self);
54             # }
55            
56 0           $self->_print_value(
57             table => $table,
58             value => $value,
59             name => $name,
60             );
61             }
62 0           return $table;
63             }
64              
65             sub _print_value {
66 0     0     my ($self,%params) = @_;
67            
68            
69 0           my $table = $params{table};
70 0           my $value = $params{value};
71 0           my $name = $params{name};
72            
73 0 0         return unless $value;
74              
75             $name = $params{index}.'. '.$name
76 0 0         if $params{index};
77            
78 0           given (ref $value) {
79 0           when('') {
80 0           $table->row($name,$value);
81             }
82 0           when('ARRAY') {
83 0           my $index = 1;
84 0           foreach my $element (@$value) {
85 0           $self->_print_value(
86             table => $table,
87             value => $element,
88             name => $name,
89             index => $index,
90             );
91 0           $index ++;
92             }
93             }
94 0           when('HASH') {
95             # TODO
96             }
97 0           when(['CODE','IO','GLOB','FORMAT']) {
98 0           warn('Cannot print $_');
99             }
100 0           when('DateTime') {
101 0 0 0       if ($value->hour == 0
      0        
102             && $value->minute == 0
103             && $value->second == 0) {
104 0           $table->row($name,$value->ymd('.'));
105             } else {
106 0           $table->row($name,$value->ymd('.').' '.$value->hms(':'));
107             }
108             }
109             # Some object
110 0           default {
111 0 0 0       if ($value->can('meta')
    0 0        
112             && $value->meta->does_role('Business::UPS::Tracking::Role::Print')
113             && $value->can('printall')) {
114 0           $table->hr();
115 0           $table->row($name,'');
116 0           $table->hr();
117 0           $value->printall($table);
118             } elsif ($value->can('printall')) {
119 0           my $output = $value->printall;
120             return
121 0 0         unless $output;
122 0           $table->row($name,$output)
123             } else {
124 0           $table->row($name,$value);
125             }
126             }
127             }
128 0           return;
129             }
130              
131 1     1   6 no Moose::Role;
  1         2  
  1         6  
132             1;