Collector/Engine.pm

filter: Statementstmt 61.5% 16 / 26 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 5 / 7 Podpod 100% 5 / 5 totaltotal 61.9% 26 / 42 CC 3 SCAR 11.7
CC
3
Cov
53%
file_exists
6.0
run_if_exists
6.0
BEGIN
1.0
CRAP 3.2
« Collector/App.pm ↑ Summary Collector/Engine/OpenSSH.pm »
1 package Data::Collector::Engine;
2 {
3 7 $Data::Collector::Engine::VERSION = '0.15';
4 }
5 # ABSTRACT: A base class for collecting engines
6  
7 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 with 'Data::Collector::Commands';
11  
12 7 has 'name' => ( is => 'ro', isa => 'Str' );
13 7 has 'connected' => (
14 is => 'rw',
15 isa => 'Bool',
16 default => 0,
17 );
18  
19 # basic overridable methods
20 2 ▶sub run { die 'No default run method' }
Subroutine run: called CC=1 SCAR=0.0
Pod documented
21 2 ▶sub connect {1}
Subroutine connect: called CC=1 SCAR=0.0
Pod documented
22 3 ▶sub disconnect {1}
Subroutine disconnect: called CC=1 SCAR=0.0
Pod documented
23  
24 0 ▶sub file_exists {
Subroutine file_exists: not called CC=2 SCAR=17.9
Pod documented
25 0 my ( $self, $file ) = @_;
26 0 my $test = $self->get_command('test');
27 0 my $echo = $self->get_command('echo');
28 0 my $cmd = "$test -f $file ; $echo \$?";
29 0 my $result = $self->run($cmd);
30 0 ▶ $result == 0 and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$result == 0 and return 1 0 0 0
31  
32 0 return 0;
33 }
34  
35 0 ▶sub run_if_exists {
Subroutine run_if_exists: not called CC=2 SCAR=17.9
Pod documented
36 0 my ( $self, $cmd, $opts ) = @_;
37 0 ▶ if ( $self->file_exists($cmd) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file_exists($cmd)) 0 0 0
38 0 $self->run("$cmd $opts");
39 }
40 }
41  
42 7 __PACKAGE__->meta->make_immutable;
43 7 1;
44  
45  
46  
47 =pod
48  
49 =head1 NAME
50  
51 Data::Collector::Engine - A base class for collecting engines
52  
53 =head1 VERSION
54  
55 version 0.15
56  
57 =head1 SYNOPSIS
58  
59 This synopsis shows how to write an (almost) full-fledged Telnet engine for
60 L<Data::Collector>.
61  
62 package Data::Collector::Engine::Telnet;
63  
64 use Moose;
65 use Net::Telnet;
66 use namespace::autoclean; # general recommendation
67 extends 'Data::Collector::Engine';
68  
69 has 'host' => ( is => 'ro', isa => 'Str', required => 1 );
70 has 'telnet' => ( is => 'ro', isa => 'Net::Telnet', lazy_build => 1 );
71  
72 has '+name' => ( default => 'Telnet' );
73  
74 sub _build_telnet {
75 my $self = shift;
76 my $telnet = Net::Telnet->new();
77 }
78  
79 sub connect {
80 my $self = shift;
81 $self->telnet->open( $self->host );
82 $self->telnet->login(...);
83 }
84  
85 sub run {
86 my ( $self, $command ) = @_;
87 my $telnet = $self->telnet;
88 my @lines = $telnet->cmd($command);
89 ...
90 }
91  
92 sub disconnect { ... }
93  
94 While we all hate long synopsises, this is the best way to demonstrate how
95 L<Data::Collector::Engine> works. You'll see we made a new engine that inherits
96 from this base class. We create a I<connect>, I<run> and I<disconnect>.
97  
98 =head1 ATTRIBUTES
99  
100 =head2 name(Str)
101  
102 This has no default, but should be set. It is currently not used, but it might
103 in the future. It's important that every engine has its own name.
104  
105 With L<Moose> goodness you can just change the value this way:
106  
107 has '+name' => ( default => 'MyEngine' );
108  
109 =head2 connected(Bool)
110  
111 A boolean to declare whether the engine is connected or not. This is in place
112 because engine are most likely to be connection-based (network, DB, etc.). The
113 I<connect> or I<disconnect> method calling is dependent on this boolean.
114  
115 =head1 SUBROUTINES/METHODS
116  
117 =head2 connect
118  
119 This method gets called before the I<run> method, to allow your engine to
120 connect to wherever it needs.
121  
122 This is also called in a lazy context, which means it will not be called on
123 load but as close as possible to whenever the engine is needed.
124  
125 At this point you would probably want to set the I<connected> boolean attribute
126 on. Read more below under I<disconnect>.
127  
128 =head2 disconnect
129  
130 A I<disconnect> is attempted if the I<connected> boolean is set.
131  
132 =head2 run
133  
134 This is the main method of the engine. The arguments are populated by the info
135 component. It may be a command to run, it may be something else. While there
136 should be an API of argument types and indicating support for them, there isn't
137 one at the moment. This should change.
138  
139 If you do not provide a run method, your engine will die, literally! :)
140  
141 =head2 file_exists
142  
143 Tries to run C<test -f file ; echo $?> to check if a file exists. You can
144 subclass it if you're doing it differently (or don't want to support it).
145  
146 $engine->check_files('file');
147  
148 =head2 run_if_exists
149  
150 A helper hybrid between C<file_exists> and C<run> to ease a common idiom:
151  
152 # instead of this:
153 if ( $engine->file_exists($cmd) ) {
154 $engine->run("$cmd $opts");
155 }
156  
157 # do this:
158 $engine->run_if_exists("$cmd $opts");
159  
160 =head1 AUTHOR
161  
162 Sawyer X <xsawyerx@cpan.org>
163  
164 =head1 COPYRIGHT AND LICENSE
165  
166 This software is copyright (c) 2012 by Sawyer X.
167  
168 This is free software; you can redistribute it and/or modify it under
169 the same terms as the Perl 5 programming language system itself.
170  
171 =cut
172  
173  
174 __END__
« Collector/App.pm ↑ Summary Collector/Engine/OpenSSH.pm »