File Coverage

blib/lib/Business/UPS/Tracking/Role/Print.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             # ============================================================================
2             package Business::UPS::Tracking::Role::Print;
3             # ============================================================================
4 1     1   2565 use utf8;
  1         3  
  1         10  
5 1     1   58 use 5.0100;
  1         5  
  1         45  
6              
7 1     1   510 use Moose::Role;
  0            
  0            
8             no if $] >= 5.017004, warnings => qw(experimental::smartmatch);
9              
10             use Text::SimpleTable;
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             my ($self,$table) = @_;
36            
37             $table ||= Text::SimpleTable->new(27,44);
38            
39             foreach my $attribute ($self->meta->get_all_attributes) {
40             next
41             unless $attribute->does('Printable');
42            
43             my $value = $attribute->get_value($self);
44            
45             next
46             unless defined $value;
47            
48             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             $self->_print_value(
57             table => $table,
58             value => $value,
59             name => $name,
60             );
61             }
62             return $table;
63             }
64              
65             sub _print_value {
66             my ($self,%params) = @_;
67            
68            
69             my $table = $params{table};
70             my $value = $params{value};
71             my $name = $params{name};
72            
73             return unless $value;
74              
75             $name = $params{index}.'. '.$name
76             if $params{index};
77            
78             given (ref $value) {
79             when('') {
80             $table->row($name,$value);
81             }
82             when('ARRAY') {
83             my $index = 1;
84             foreach my $element (@$value) {
85             $self->_print_value(
86             table => $table,
87             value => $element,
88             name => $name,
89             index => $index,
90             );
91             $index ++;
92             }
93             }
94             when('HASH') {
95             # TODO
96             }
97             when(['CODE','IO','GLOB','FORMAT']) {
98             warn('Cannot print $_');
99             }
100             when('DateTime') {
101             if ($value->hour == 0
102             && $value->minute == 0
103             && $value->second == 0) {
104             $table->row($name,$value->ymd('.'));
105             } else {
106             $table->row($name,$value->ymd('.').' '.$value->hms(':'));
107             }
108             }
109             # Some object
110             default {
111             if ($value->can('meta')
112             && $value->meta->does_role('Business::UPS::Tracking::Role::Print')
113             && $value->can('printall')) {
114             $table->hr();
115             $table->row($name,'');
116             $table->hr();
117             $value->printall($table);
118             } elsif ($value->can('printall')) {
119             my $output = $value->printall;
120             return
121             unless $output;
122             $table->row($name,$output)
123             } else {
124             $table->row($name,$value);
125             }
126             }
127             }
128             return;
129             }
130              
131             no Moose::Role;
132             1;