File Coverage

blib/lib/Siebel/Srvrmgr/ListParser/OutputFactory.pm
Criterion Covered Total %
statement 34 34 100.0
branch 5 6 83.3
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 51 52 98.0


line stmt bran cond sub pod time code
1             package Siebel::Srvrmgr::ListParser::OutputFactory;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Siebel::Srvrmgr::ListParser::OutputFactory - abstract factory class to create Siebel::Srvrmgr::ListParser::Output objects
8              
9             =cut
10              
11 23     23   1565 use warnings;
  23         31  
  23         654  
12 23     23   89 use strict;
  23         53  
  23         560  
13 23     23   8181 use MooseX::AbstractFactory;
  23         647968  
  23         127  
14 23     23   3281993 use Carp;
  23         46  
  23         1543  
15 23     23   18065 use Siebel::Srvrmgr::Regexes qw(CONN_GREET);
  23         37  
  23         1264  
16 23     23   10865 use Hash::Util qw(lock_hash);
  23         39793  
  23         102  
17              
18             =pod
19              
20             =head1 SYNOPSIS
21              
22             use Siebel::Srvrmgr::ListParser::OutputFactory;
23              
24             my $output = Siebel::Srvrmgr::ListParser::OutputFactory->create(
25             $type,
26             {
27             data_type => $type,
28             raw_data => \@data,
29             cmd_line => 'list something'
30             }
31             );
32              
33             if (Siebel::Srvrmgr::ListParser::OutputFactory->can_create('weirdo')) ? print "can\n" : print "cannot\n";
34              
35             =head1 DESCRIPTION
36              
37             This is an abstract factory class to create instances of subclass of L<Siebel::Srvrmgr::ListParser::Output> superclass.
38              
39             It has the mapping between the types parsed by L<Siebel::Srvrmgr::ListParser> class to the respective class of output. See
40             C<Siebel::Srvrmgr::ListParser::OutputFactory::table_mapping> for the mapping between types and classes.
41              
42             =head1 METHODS
43              
44             All methods below are class methods.
45              
46             =head2 build
47              
48             Returns the instance of the class defined by the type given as parameter. Expects two parameters: an string with the type
49             of output and an hash reference with the parameters expected by the C<new> method of L<Siebel::Srvrmgr::ListParser::Output> subclasses.
50              
51             A third, optional parameter, is required if the desired instance is a subclass of L<Siebel::Srvrmgr::ListParser::Output::Tabular>: one must
52             pass a single character as the field separator, if exists in the output to be parsed.
53              
54             Despite using L<MooseX::AbstractFactory> to implement the Abstract Factory pattern, this method must be invoked instead of C<create>
55             so the class will be able to make additional checkings necessary to define defaults for subclasses of L<Siebel::Srvrmgr::ListParser::Output::Tabular>.
56              
57             =head2 can_create
58              
59             Expects a string as the output type.
60              
61             Returns true if there is a mapping between the given type and a subclass of L<Siebel::Srvrmgr::ListParser::Output>;
62             otherwise it returns false;
63              
64             =head2 get_mapping
65              
66             Returns an hash reference with the mapping between the parsed types and subclasses of L<Siebel::Srvrmgr::ListParser::Ouput>.
67              
68             The values are array references with the partial classes names and a compiled regular expression to validate the command.
69              
70             =head1 SEE ALSO
71              
72             =over
73              
74             =item *
75              
76             L<MooseX::AbstractFactory>
77              
78             =item *
79              
80             L<Siebel::Srvrmgr::ListParser::Output>
81              
82             =item *
83              
84             L<Siebel::Srvrmgr::ListParser>
85              
86             =back
87              
88             =cut
89              
90             my %table_mapping = (
91             'list_comp' => ['Tabular::ListComp', qr/^list\scomps?$/],
92             'set_delimiter' => ['Set', qr/^set\sdelimiter/],
93             'list_params' => ['Tabular::ListParams', qr/list\s(advanced\s)?param(eter)?s?(\s(\w+\s)?(for\sserver\s\w+)?(\sfor\s((comp(nent)?)|named\ssubsystem|task)\s\w+)?)?/],
94             'list_comp_def' => ['Tabular::ListCompDef', qr/list\scomp\sdefs?(\s\w+)?/],
95             'greetings' => ['Enterprise', CONN_GREET],
96             'list_comp_types' => ['Tabular::ListCompTypes', qr/list\scomp(nent)?\stypes?$/],
97             'load_preferences' => ['LoadPreferences', qr/^load\spreferences$/],
98             'list_tasks' => ['Tabular::ListTasks', qr/list\stasks(\sfor\sserver\s\w+\scomponent\sgroup?\s\w+)?/],
99             'list_servers' => ['Tabular::ListServers', qr/list\sserver(s)?.*/],
100             'list_sessions' => ['Tabular::ListSessions', qr/^list\s(active|hung)?\s?sessions$/]
101             );
102              
103             lock_hash(%table_mapping);
104              
105             sub get_mapping {
106              
107 80     80 1 1186 my %copy = %table_mapping;
108              
109 80         234 return \%copy;
110              
111             }
112              
113             sub can_create {
114              
115 142     142 1 2797 my $class = shift;
116 142         174 my $type = shift;
117              
118 142         459 return ( exists( $table_mapping{$type} ) );
119              
120             }
121              
122             sub build {
123              
124 135     135 1 179 my $class = shift;
125 135         161 my $last_cmd_type = shift;
126 135         161 my $object_data = shift; # hash ref
127              
128 135 50       296 confess 'object data is required' unless ( defined($object_data) );
129              
130 135 100       656 if ( $table_mapping{$last_cmd_type}->[0] =~ /^Tabular/ ) {
131              
132 111         171 my $field_del = shift;
133              
134 111 100       247 if ( defined($field_del) ) {
135              
136 36         72 $object_data->{col_sep} = $field_del;
137 36         75 $object_data->{structure_type} = 'delimited';
138             }
139             else {
140              
141 75         176 $object_data->{structure_type} = 'fixed';
142              
143             }
144              
145             }
146              
147 135         762 $class->create( $last_cmd_type, $object_data );
148              
149             }
150              
151             implementation_class_via sub {
152              
153             my $last_cmd_type = shift;
154              
155             if ( exists( $table_mapping{$last_cmd_type} ) ) {
156              
157             return 'Siebel::Srvrmgr::ListParser::Output::'
158             . $table_mapping{$last_cmd_type}->[0];
159              
160             }
161             else {
162              
163             confess "Cannot defined a class for command '$last_cmd_type'";
164              
165             }
166              
167             };
168              
169             =pod
170              
171             =head1 AUTHOR
172              
173             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
174              
175             =head1 COPYRIGHT AND LICENSE
176              
177             This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
178              
179             This file is part of Siebel Monitoring Tools.
180              
181             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
182             it under the terms of the GNU General Public License as published by
183             the Free Software Foundation, either version 3 of the License, or
184             (at your option) any later version.
185              
186             Siebel Monitoring Tools is distributed in the hope that it will be useful,
187             but WITHOUT ANY WARRANTY; without even the implied warranty of
188             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
189             GNU General Public License for more details.
190              
191             You should have received a copy of the GNU General Public License
192             along with Siebel Monitoring Tools. If not, see L<http://www.gnu.org/licenses/>.
193              
194             =cut
195              
196             1;