File Coverage

blib/lib/MooseX/App/Message/Envelope.pm
Criterion Covered Total %
statement 31 47 65.9
branch 0 4 0.0
condition n/a
subroutine 10 16 62.5
pod 2 2 100.0
total 43 69 62.3


line stmt bran cond sub pod time code
1             # ============================================================================
2             package MooseX::App::Message::Envelope;
3             # ============================================================================
4              
5 15     15   379 use 5.010;
  15         57  
6 15     15   91 use utf8;
  15         36  
  15         115  
7              
8 15     15   400 use namespace::autoclean;
  15         38  
  15         148  
9 15     15   1467 use Moose;
  15         35  
  15         108  
10              
11 15     15   111446 use MooseX::App::Message::Block;
  15         54  
  15         639  
12              
13             use overload
14 15     15   125 '""' => "overload";
  15         34  
  15         89  
15              
16             has 'blocks' => (
17             is => 'ro',
18             isa => 'ArrayRef[MooseX::App::Message::Block]',
19             traits => ['Array'],
20             handles => {
21             add_block => 'push',
22             list_blocks => 'elements',
23             },
24             );
25              
26             has 'exitcode' => (
27             is => 'ro',
28             isa => 'Int',
29             predicate => 'has_exitcode',
30             );
31              
32             around 'BUILDARGS' => sub {
33             my $orig = shift;
34             my $self = shift;
35             my @args = @_;
36              
37             my $params;
38             if (scalar @args == 1
39             && ref($args[0]) eq 'HASH') {
40             $params = $args[0];
41             } else {
42             $params = {
43             blocks => [],
44             };
45             my @blocks;
46             foreach my $element (@args) {
47             next
48             unless defined $element;
49             if (blessed $element
50             && $element->isa('MooseX::App::Message::Block')) {
51             push(@{$params->{blocks}},$element);
52             } elsif ($element =~ /^\d+$/
53             && $element <= 255
54             && $element >= 0) {
55             $params->{exitcode} = $element;
56             } else {
57             push(@{$params->{blocks}},MooseX::App::Message::Block->new(
58             header => $element,
59             ));
60             }
61             }
62             }
63              
64             return $self->$orig($params);
65             };
66              
67             sub overload {
68 0     0 1 0 my ($self) = @_;
69              
70 0 0       0 if ($self->has_exitcode) {
71 0         0 my $exitcode = $self->exitcode;
72 0 0       0 if ($exitcode == 0) {
73 0         0 print $self->stringify;
74             } else {
75 0         0 print STDERR $self->stringify;
76             }
77 0         0 exit $exitcode;
78             } else {
79 0         0 print $self->stringify;
80             }
81 0         0 return;
82             }
83              
84             sub stringify {
85 2     2 1 1675 my ($self) = @_;
86              
87 2         5 my $message = '';
88 2         93 foreach my $block ($self->list_blocks) {
89 3         17 $message .= $block->stringify;
90             }
91              
92 2         8 return $message;
93             }
94              
95             sub AUTOLOAD {
96 0     0     my ($self) = @_;
97 0           $self->overload;
98 0           return $MooseX::App::Null::NULL;
99             }
100              
101             {
102             package MooseX::App::Null;
103              
104 15     15   9521 use strict;
  15         42  
  15         463  
105 15     15   98 use warnings;
  15         50  
  15         1420  
106              
107             use overload
108 0     0   0 'bool' => sub { 0 },
109 0     0   0 '""' => sub { '' },
110 15     15   106 '0+' => sub { 0 };
  15     0   52  
  15         168  
  0         0  
111             our $NULL = bless {}, __PACKAGE__;
112 0     0     sub AUTOLOAD { return $NULL }
113             }
114              
115             __PACKAGE__->meta->make_immutable;
116             1;
117              
118             __END__
119              
120             =encoding utf8
121              
122             =head1 NAME
123              
124             MooseX::App::Message::Envelope - Message presented to the user
125              
126             =head1 DESCRIPTION
127              
128             Whenever MooseX::App needs to pass a message to the user, it does so by
129             generating a MooseX::App::Message::Envelope object. The object usually
130             contains one or more blocks (L<MooseX::App::Message::Block>) and can be
131             easily stringified.
132              
133             Usually a MooseX::App::Message::Envelope object is generated and returned
134             by the L<new_with_command method in MooseX::App::Base|MooseX::App::Base/new_with_command>
135             if there is an error or if the user requests help.
136              
137             To avoid useless object type checks when working with this method,
138             MooseX::App::Message::Envelope follows the Null-class pattern. So you can do
139             this, no matter if new_with_command fails or not:
140              
141             MyApp->new_with_command->some_method->only_called_if_successful;
142              
143             If
144              
145             =head1 METHODS
146              
147             =head2 stringify
148              
149             Stringifies the messages
150              
151             =head2 overload
152              
153             This method is called whenever the object is stringified via overload. In this
154             case it prints the message on either STDERR or STDOUT, and exits the process
155             with the given exitcode (if any).
156              
157             =head2 add_block
158              
159             Adds a new message block. Param must be a L<MooseX::App::Message::Block>
160              
161             =head2 list_blocks
162              
163             Returns a list on message blocks.
164              
165             =head2 blocks
166              
167             Message block accessor.
168              
169             =head2 exitcode
170              
171             Exitcode accessor.
172              
173             =head2 has_exitcode
174              
175             Check if exitcode is set.
176              
177             =head2 OVERLOAD
178              
179             Stringification of this object is overloaded.
180              
181             =head2 AUTOLOAD
182              
183             You can call any method on the message class.