File Coverage

blib/lib/Siebel/Srvrmgr/Daemon/Action.pm
Criterion Covered Total %
statement 20 22 90.9
branch 1 2 50.0
condition n/a
subroutine 4 6 66.6
pod 2 2 100.0
total 27 32 84.3


line stmt bran cond sub pod time code
1             package Siebel::Srvrmgr::Daemon::Action;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Siebel::Srvrmgr::Daemon::Action - base class for Siebel::Srvrmgr::Daemon action
8              
9             =head1 SYNOPSIS
10              
11             This class must be subclassed and the C<do> method overrided.
12              
13             An subclass should return true ONLY when was able to identify the type of output received. Beware that the output expected must include also
14             the command executed or the L<Siebel::Srvrmgr::ListParser> object will not be able to identify the type of the output (L<Siebel::Srvrmgr::Daemon> does that).
15              
16             This can be accomplish using something like this in the C<do> method:
17              
18             sub do {
19              
20             my $self = shift;
21             my $buffer = shift;
22              
23             $self->get_parser()->parse($buffer);
24              
25             my $tree = $self->get_parser()->get_parsed_tree();
26              
27             foreach my $obj ( @{$tree} ) {
28              
29             if ( $obj->isa('Siebel::Srvrmgr::ListParser::Output::MyOutputSubclassName') ) {
30              
31             my $data = $obj->get_data_parsed();
32              
33             # do something
34              
35             return 1;
36              
37             }
38              
39             } # end of foreach block
40              
41             return 0;
42            
43             }
44              
45             Where MyOutputSubclassName is a subclass of L<Siebel::Srvrmgr::ListParser::Output>.
46              
47             If this kind of output is not identified and the proper C<return> given, L<Siebel::Srvrmgr::Daemon> can enter in a
48             infinite loop.
49              
50             =cut
51              
52 16     16   11263 use Moose;
  16         120  
  16         83  
53 16     16   74327 use namespace::autoclean;
  16         38  
  16         144  
54 16     16   1060 use Carp;
  16         21  
  16         4847  
55              
56             =pod
57              
58             =head1 ATTRIBUTES
59              
60             =head2 parser
61              
62             A reference to a L<Siebel::Srvrmgr::ListParser> object. This attribute is required during object creation
63             and is read-only.
64              
65             =cut
66              
67             has parser => (
68             isa => 'Siebel::Srvrmgr::ListParser',
69             is => 'ro',
70             required => 1,
71             reader => 'get_parser'
72             );
73              
74             =pod
75              
76             =head2 params
77              
78             An array reference. C<params> is an optional attribute during the object creation and it is used to pass additional parameters. How
79             those parameters are going to be used is left to who is creating subclasses of L<Siebel::Srvrmgr::Daemon::Action>.
80              
81             This attribute is read-only.
82              
83             =cut
84              
85             # :TODO:19-01-2014:: add a method to remove the params reference when desired to release memory
86              
87             has params => (
88             isa => 'ArrayRef',
89             is => 'ro',
90             reader => 'get_params',
91             default => sub { [] }
92             );
93              
94             =pod
95              
96             =head2 expected_output
97              
98             A string telling the expected output type the L<Siebel::Srvrmgr::Daemon::Action> subclass expectes to find. This is a lazy attribute since each subclass may have a
99             different type of expected output.
100              
101             The string may be anything related to L<Siebel::Srvrmgr::ListParser::Output> subclasses and it will be used for validation during execution of C<do_parsed> method.
102              
103             =cut
104              
105             has expected_output => (
106             isa => 'Str',
107             is => 'ro',
108             reader => 'get_exp_output',
109             writer => '_set_exp_output',
110             builder => '_build_exp_output',
111             lazy => 1 #some subclasses will not use it
112             );
113              
114             sub _build_exp_output {
115              
116 0     0   0 confess
117             'subclasses of Siebel::Srvrmgr::Daemon::Action must overrided _build_exp_output';
118              
119             }
120              
121             =pod
122              
123             =head1 METHODS
124              
125             =head2 get_parser
126              
127             Returns the L<Siebel::Srvrmgr::ListParser> object stored into the C<parser> attribute.
128              
129             =head2 get_params
130              
131             Returns the array reference stored in the C<params> attribute.
132              
133             =head2 do
134              
135             This method expects to receive a array reference (with the content to be parsed) as parameter and it will do something with it. Usually this should be
136             identify the type of output received, giving it to the proper parse and processing it somehow.
137              
138             Every C<do> method must return true (1) if output was used, otherwise false (0).
139              
140             This method does:
141              
142             =over
143              
144             =item parsing of the content of the buffer with the parser returned by C<get_parser> method
145              
146             =item invokes the C<do_parsed> method passing the tree returned from the parser
147              
148             =item clear the parser with the C<clear_parsed_tree> method.
149              
150             =item returns the returned value of the C<do_parsed> method.
151              
152             =back
153              
154             =cut
155              
156             sub do {
157              
158 38     38 1 10818 my $self = shift;
159 38         58 my $buffer = shift;
160              
161 38         1586 $self->get_parser()->parse($buffer);
162              
163 38         1264 my $tree = $self->get_parser()->get_parsed_tree();
164              
165 38         77 my $was_found = 0;
166              
167 38         62 foreach my $item ( @{$tree} ) {
  38         96  
168              
169 35         236 $was_found = $self->do_parsed($item);
170 33 50       142 last if ($was_found);
171              
172             }
173              
174 36         1359 $self->get_parser()->clear_parsed_tree();
175              
176 36         1220 return $was_found;
177              
178             }
179              
180             =pod
181              
182             =head2 do_parsed
183              
184             This method must be overrided by subclasses or it will C<die> with trace.
185              
186             It expects an array reference with the parsed tree given by C<get_parsed_tree> of a L<Siebel::Srvrmgr::ListParser> instance.
187              
188             This method is invoked internally by C<do> method, but is also usable directly if the parsed tree is given as expected.
189              
190             If the output is used, this method must returns true, otherwise false.
191              
192             =cut
193              
194             sub do_parsed {
195              
196 0     0 1   confess
197             'do_parsed must be overrided by subclasses of Siebel::Srvrmgr::Daemon::Action';
198              
199             }
200              
201             =pod
202              
203             =head1 CAVEATS
204              
205             This class may be changed to a role instead of a superclass in the future since it's methods could be used by different classes.
206              
207             =head1 SEE ALSO
208              
209             =over
210              
211             =item *
212              
213             L<Moose>
214              
215             =item *
216              
217             L<Moose::Manual::MethodModifiers>
218              
219             =item *
220              
221             L<Siebel::Srvrmgr::ListParser>
222              
223             =item *
224              
225             L<Siebel::Srvrmgr::Daemon>
226              
227             =item *
228              
229             L<Siebel::Srvrmgr::ListParser::Output>
230              
231             =back
232              
233             =head1 AUTHOR
234              
235             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>
236              
237             =head1 COPYRIGHT AND LICENSE
238              
239             This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>
240              
241             This file is part of Siebel Monitoring Tools.
242              
243             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
244             it under the terms of the GNU General Public License as published by
245             the Free Software Foundation, either version 3 of the License, or
246             (at your option) any later version.
247              
248             Siebel Monitoring Tools is distributed in the hope that it will be useful,
249             but WITHOUT ANY WARRANTY; without even the implied warranty of
250             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
251             GNU General Public License for more details.
252              
253             You should have received a copy of the GNU General Public License
254             along with Siebel Monitoring Tools. If not, see <http://www.gnu.org/licenses/>.
255              
256             =cut
257              
258             __PACKAGE__->meta->make_immutable;