File Coverage

blib/lib/App/Info/Request.pm
Criterion Covered Total %
statement 36 36 100.0
branch 13 14 92.8
condition 9 12 75.0
subroutine 11 11 100.0
pod 7 7 100.0
total 76 80 95.0


line stmt bran cond sub pod time code
1             package App::Info::Request;
2              
3             =head1 NAME
4              
5             App::Info::Request - App::Info event handler request object
6              
7             =head1 SYNOPSIS
8              
9             # In an App::Info::Handler subclass:
10             sub handler {
11             my ($self, $req) = @_;
12             print "Event Type: ", $req->type;
13             print "Message: ", $req->message;
14             print "Error: ", $req->error;
15             print "Value: ", $req->value;
16             }
17              
18             =head1 DESCRIPTION
19              
20             Objects of this class are passed to the C method of App::Info event
21             handlers. Generally, this class will be of most interest to App::Info::Handler
22             subclass implementers.
23              
24             The L in App::Info each construct
25             a new App::Info::Request object and initialize it with their arguments. The
26             App::Info::Request object is then the sole argument passed to the C
27             method of any and all App::Info::Handler objects in the event handling chain.
28             Thus, if you'd like to create your own App::Info event handler, this is the
29             object you need to be familiar with. Consult the
30             L documentation for details on creating
31             custom event handlers.
32              
33             Each of the App::Info event triggering methods constructs an
34             App::Info::Request object with different attribute values. Be sure to consult
35             the documentation for the L in
36             App::Info, where the values assigned to the App::Info::Request object are
37             documented. Then, in your event handler subclass, check the value returned by
38             the C method to determine what type of event request you're handling
39             to handle the request appropriately.
40              
41             =cut
42              
43 19     19   51859 use strict;
  19         33  
  19         708  
44 19     19   96 use vars qw($VERSION);
  19         89  
  19         803  
45 19     19   98 use Carp;
  19         34  
  19         15540  
46             $VERSION = '0.57';
47              
48             ##############################################################################
49              
50             =head1 INTERFACE
51              
52             The following sections document the App::Info::Request interface.
53              
54             =head2 Constructor
55              
56             =head3 new
57              
58             my $req = App::Info::Request->new(%params);
59              
60             This method is used internally by App::Info to construct new
61             App::Info::Request objects to pass to event handler objects. Generally, you
62             won't need to use it, other than perhaps for testing custom App::Info::Handler
63             classes.
64              
65             The parameters to C are passed as a hash of named parameters that
66             correspond to their like-named methods. The supported parameters are:
67              
68             =over 4
69              
70             =item type
71              
72             =item message
73              
74             =item error
75              
76             =item value
77              
78             =item callback
79              
80             =back
81              
82             See the object methods documentation below for details on these object
83             attributes.
84              
85             =cut
86              
87             sub new {
88 204     204 1 1630 my $pkg = shift;
89              
90             # Make sure we've got a hash of arguments.
91 204 100       951 Carp::croak("Odd number of parameters in call to " . __PACKAGE__ .
92             "->new() when named parameters expected" ) if @_ % 2;
93 203         838 my %params = @_;
94              
95             # Validate the callback.
96 203 100       728 if ($params{callback}) {
97 55 100       478 Carp::croak("Callback parameter '$params{callback}' is not a code ",
98             "reference")
99             unless UNIVERSAL::isa($params{callback}, 'CODE');
100             } else {
101             # Otherwise just assign a default approve callback.
102 148     2   1239 $params{callback} = sub { 1 };
  2         7  
103             }
104              
105             # Validate type parameter.
106 202 100       594 if (my $t = $params{type}) {
107 201 50 100     4236 Carp::croak("Invalid handler type '$t'")
      100        
      66        
108             unless $t eq 'error' or $t eq 'info' or $t eq 'unknown'
109             or $t eq 'confirm';
110             } else {
111 1         3 $params{type} = 'info';
112             }
113              
114             # Return the request object.
115 202   33     1882 bless \%params, ref $pkg || $pkg;
116             }
117              
118             ##############################################################################
119              
120             =head2 Object Methods
121              
122             =head3 key
123              
124             my $key = $req->key;
125              
126             Returns the key stored in the App::Info::Request object. The key is used by
127             the App::Info subclass to uniquely identify the information it is harvesting,
128             such as the path to an executable. It might be used by request handlers,
129             for example, to see if an option was passed on the command-line.
130              
131             =cut
132              
133 1     1 1 9 sub key { $_[0]->{key} }
134              
135             ##############################################################################
136              
137             =head3 message
138              
139             my $message = $req->message;
140              
141             Returns the message stored in the App::Info::Request object. The message is
142             typically informational, or an error message, or a prompt message.
143              
144             =cut
145              
146 91     91 1 4396 sub message { $_[0]->{message} }
147              
148             ##############################################################################
149              
150             =head3 error
151              
152             my $error = $req->error;
153              
154             Returns any error message associated with the App::Info::Request object. The
155             error message is typically there to display for users when C
156             returns false.
157              
158             =cut
159              
160 11     11 1 37 sub error { $_[0]->{error} }
161              
162             ##############################################################################
163              
164             =head3 type
165              
166             my $type = $req->type;
167              
168             Returns a string representing the type of event that triggered this request.
169             The types are the same as the event triggering methods defined in App::Info.
170             As of this writing, the supported types are:
171              
172             =over
173              
174             =item info
175              
176             =item error
177              
178             =item unknown
179              
180             =item confirm
181              
182             =back
183              
184             Be sure to consult the App::Info documentation for more details on the event
185             types.
186              
187             =cut
188              
189 15     15 1 61 sub type { $_[0]->{type} }
190              
191             ##############################################################################
192              
193             =head3 callback
194              
195             if ($req->callback($value)) {
196             print "Value '$value' is valid.\n";
197             } else {
198             print "Value '$value' is not valid.\n";
199             }
200              
201             Executes the callback anonymous subroutine supplied by the App::Info concrete
202             base class that triggered the event. If the callback returns false, then
203             C<$value> is invalid. If the callback returns true, then C<$value> is valid
204             and can be assigned via the C method.
205              
206             Note that the C method itself calls C if it was passed a
207             value to assign. See its documentation below for more information.
208              
209             =cut
210              
211             sub callback {
212 20     20 1 1083 my $self = shift;
213 20         38 my $code = $self->{callback};
214 20         37 local $_ = $_[0];
215 20         56 $code->(@_);
216             }
217              
218             ##############################################################################
219              
220             =head3 value
221              
222             my $value = $req->value;
223             if ($req->value($value)) {
224             print "Value '$value' successfully assigned.\n";
225             } else {
226             print "Value '$value' not successfully assigned.\n";
227             }
228              
229             When called without an argument, C simply returns the value currently
230             stored by the App::Info::Request object. Typically, the value is the default
231             value for a confirm event, or a value assigned to an unknown event.
232              
233             When passed an argument, C attempts to store the the argument as a
234             new value. However, C calls C on the new value, and if
235             C returns false, then C returns false and does not store
236             the new value. If C returns true, on the other hand, then
237             C goes ahead and stores the new value and returns true.
238              
239             =cut
240              
241             sub value {
242 85     85 1 669 my $self = shift;
243 85 100       275 if ($#_ >= 0) {
244             # grab the value.
245 16         25 my $value = shift;
246             # Validate the value.
247 16 100       40 if ($self->callback($value)) {
248             # The value is good. Assign it and return true.
249 7         78 $self->{value} = $value;
250 7         27 return 1;
251             } else {
252             # Invalid value. Return false.
253 9         80 return;
254             }
255             }
256             # Just return the value.
257 69         358 return $self->{value};
258             }
259              
260             1;
261             __END__