blib/lib/App/Bot/BasicBot/Pluggable.pm

filter: Statementstmt 88.4% 92 / 104 Branchbran 62.5% 10 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 86.9% 20 / 23 Podpod 50.0% 1 / 2 totaltotal 85.3% 128 / 150 SCAR 26.8
CC
14
Cov
85%
run
30.0
_load_modules
7.0
__ANON__
2.0
CRAP 14.6
« lib/Bot/BasicBot/Pluggable/Module.pm ↑ Summary blib/lib/Bot/BasicBot/Pluggable/Module/DNS.pm »
1 package App::Bot::BasicBot::Pluggable;
2 3 $App::Bot::BasicBot::Pluggable::VERSION = '1.20';
3 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Config::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Bot::BasicBot::Pluggable;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Bot::BasicBot::Pluggable::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use List::MoreUtils qw(any uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 with 'MooseX::Getopt::Dashes';
13 3 with 'MooseX::SimpleConfig';
14  
15 use Module::Pluggable
16 3 sub_name => '_available_stores',
17 3 ▶ search_path => 'Bot::BasicBot::Pluggable::Store';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 subtype 'App::Bot::BasicBot::Pluggable::Channels'
20 => as 'ArrayRef'
21 ## Either it's an empty ArrayRef or all channels start with #
22 7 ▶ => where { @{$_} ? any { /^#/ } @{$_} : 1 };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@{$_;} ? : 4 2 6
23  
24 coerce 'App::Bot::BasicBot::Pluggable::Channels'
25 => from 'ArrayRef'
26 2 ▶ => via { [ map { /^#/ ? $_ : "#$_" } @{$_} ] };
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
/^#/ ? : 0 4 4
27  
28 3 subtype 'App::Bot::BasicBot::Pluggable::Store'
29 => as 'Bot::BasicBot::Pluggable::Store';
30  
31 3 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
32 'App::Bot::BasicBot::Pluggable::Store' => '=s%'
33 );
34  
35 coerce 'App::Bot::BasicBot::Pluggable::Store'
36 => from 'Str'
37 0 ▶ => via { Bot::BasicBot::Pluggable::Store->new_from_hashref({ type => 'Str' }) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
38  
39 coerce 'App::Bot::BasicBot::Pluggable::Store'
40 => from 'HashRef'
41 2 ▶ => via { Bot::BasicBot::Pluggable::Store->new_from_hashref( shift ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 3 has server => ( is => 'rw', isa => 'Str', default => 'localhost' );
44 3 has nick => ( is => 'rw', isa => 'Str', default => 'basicbot' );
45 3 has charset => ( is => 'rw', isa => 'Str', default => 'utf8' );
46 has channel => (
47 is => 'rw',
48 isa => 'App::Bot::BasicBot::Pluggable::Channels',
49 coerce => 1,
50 1 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
51 3 );
52 3 has password => ( is => 'rw', isa => 'Str' );
53 3 has port => ( is => 'rw', isa => 'Int', default => 6667 );
54 3 has useipv6 => ( is => 'rw', isa => 'Bool', default => 1 );
55 3 has localaddr => ( is => 'rw', isa => 'Str' );
56 3 has bot_class =>
57 ( is => 'rw', isa => 'Str', default => 'Bot::BasicBot::Pluggable' );
58  
59 3 has list_modules => ( is => 'rw', isa => 'Bool', default => 0 );
60 3 has list_stores => ( is => 'rw', isa => 'Bool', default => 0 );
61  
62 3 has store => (
63 is => 'rw',
64 isa => 'App::Bot::BasicBot::Pluggable::Store',
65 coerce => 1,
66 builder => '_create_store'
67 );
68 has settings => (
69 metaclass => 'NoGetopt',
70 is => 'rw',
71 isa => 'HashRef',
72 2 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
73 3 );
74  
75 3 has loglevel => ( is => 'rw', isa => 'Str', default => 'warn' );
76 3 has logconfig => ( is => 'rw', isa => 'Str' );
77  
78 3 has configfile => (
79 is => 'rw',
80 isa => 'Str|Undef',
81 default => Config::Find->find( name => 'bot-basicbot-pluggable.yaml' ),
82 );
83  
84 3 has bot => (
85 metaclass => 'NoGetopt',
86 is => 'rw',
87 isa => 'Bot::BasicBot::Pluggable',
88 builder => '_create_bot',
89 lazy => 1,
90 );
91  
92 has module => (
93 is => 'rw',
94 isa => 'ArrayRef',
95 1 ▶ default => sub { return [qw( Auth Loader )] }
Subroutine __ANON__: called CC=1 SCAR=0.0
96 3 );
97  
98 sub BUILD {
99 3 ▶ my ($self) = @_;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
100  
101 3 ▶ if ( $self->password() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->password) 1 2 3
102 1 $self->module( [ uniq @{ $self->module }, 'Auth' ] );
103 }
104 3 $self->_load_modules();
105 }
106  
107 sub _load_modules {
108 3 ▶ my ($self) = @_;
Subroutine _load_modules: called CC=7 SCAR=19.5
109 3 my %settings = %{ $self->settings() };
110 3 my $logger = Log::Log4perl->get_logger( ref $self );
111  
112 # Implicit loading of modules via $self->settings
113 3 my @modules = uniq @{ $self->module() }, keys %settings;
114 3 $self->module( [@modules] );
115  
116 3 for my $module_name (@modules) {
117 my $module = try {
118 7 ▶ $self->bot->load($module_name);
Subroutine __ANON__: called CC=1 SCAR=0.0
119 }
120 catch {
121 0 ▶ $logger->error("$_");
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 7 };
123 7 ▶ next if !$module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 7 7
124 7 ▶ if ( exists( $settings{$module_name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $settings{$module_name}) 1 6 7
125 1 for my $key ( keys %{ $settings{$module_name} } ) {
126 1 $module->set( $key, $settings{$module_name}->{$key} );
127 }
128 }
129 7 ▶ if ( $module_name eq 'Auth' and $self->password() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module_name eq "Auth" and $self->password) 1 6 7
Condition100%3/3
$module_name eq "Auth" and $self->password
!ll&&!rl&&r
421
MC/DC100%2/2
$module_name eq "Auth" and $self->password
$module_name eq "Auth" $self->password
Truth table100%3/3
A and B
A= $module_name eq "Auth"B= $self->password
ABresult
0X0
100
111
130 1 $module->set( 'password_admin', $self->password() );
131 }
132 }
133 }
134  
135 sub _create_store {
136 1 ▶ return Bot::BasicBot::Pluggable::Store->new_from_hashref(
Subroutine _create_store: called CC=1 SCAR=0.0
137 { type => 'Memory' } );
138 }
139  
140 sub _create_bot {
141 3 ▶ my ($self) = @_;
Subroutine _create_bot: called CC=1 SCAR=0.0
142 3 my $class = $self->bot_class();
143 3 return $class->new(
144 channels => $self->channel(),
145 server => $self->server(),
146 nick => $self->nick(),
147 charset => $self->charset(),
148 port => $self->port(),
149 useipv6 => $self->useipv6(),
150 localaddr => $self->localaddr(),
151 store => $self->store(),
152 loglevel => $self->loglevel(),
153 logconfig => $self->logconfig(),
154 );
155 }
156  
157 sub run {
158 0 ▶ my ($self) = @_;
Subroutine run: not called CC=5 SCAR=34.0
Pod documented
159  
160 0 ▶ if ( $self->list_modules() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->list_modules) 0 0 0
161 0 print "$_\n" for $self->bot->available_modules;
162 0 exit 0;
163 }
164  
165 0 ▶ if ( $self->list_stores() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->list_stores) 0 0 0
166 0 for ( $self->_available_stores ) {
167 0 s/Bot::BasicBot::Pluggable::Store:://;
168 0 print "$_\n";
169 }
170 0 exit 0;
171 }
172 0 $self->bot->run();
173 }
174  
175 3 1;
176  
177 __END__
178  
179 =head1 NAME
180  
181 App::Bot::BasicBot::Pluggable - Base class for bot applications
182  
183 =head1 VERSION
184  
185 version 1.20
186  
187 =head1 SYNOPSIS
188  
189 my bot = App::Bot::BasicBot::Pluggable( modules => [ 'Karma' ] )
190 $bot->run();
191  
192 =head1 DESCRIPTION
193  
194 This module is basically intended as base class for
195 L<Bot::BasicBot::Pluggable> frontends. It's attributes can be set
196 by command line options or a configuration file.
197  
198 =head1 ATTRIBUTES
199  
200 All subsequently listed attributes are documented in the manpage
201 of L<bot-basicbot-pluggable>. Just replace all dashes with underscores.
202  
203 =over 4
204  
205 =item server
206  
207 =item nick
208  
209 =item charset
210  
211 =item password
212  
213 =item port
214  
215 =item list_modules
216  
217 =item list_stores
218  
219 =item loglevel
220  
221 =item logconfig
222  
223 =item configfile
224  
225 =item module
226  
227 =back
228  
229 =head1 METHODS
230  
231 =head2 run
232  
233 If list_modules or list_stores are set to a true value, the according
234 list is printed to stdout. Otherwise the run method of the bot
235 specified by the bot_class method is called.
236  
237 =head1 AUTHOR
238  
239 Mario Domgoergen <mdom@cpan.org>
240  
241 =head1 LICENSE
242  
243 This program is free software; you can redistribute it
244 and/or modify it under the same terms as Perl itself
« lib/Bot/BasicBot/Pluggable/Module.pm ↑ Summary blib/lib/Bot/BasicBot/Pluggable/Module/DNS.pm »