File Coverage

blib/lib/Siebel/Srvrmgr/Daemon/Action.pm
Criterion Covered Total %
statement 20 23 86.9
branch 1 2 50.0
condition n/a
subroutine 4 6 66.6
pod 2 2 100.0
total 27 33 81.8


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