| 1 | package Bot::BasicBot::Pluggable::FromConfig; | ||||||||||||
| 2 | 1 | ▶ | use 5.008005; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 1 | our $VERSION = "0.6"; | |||||||||||
| 7 | |||||||||||||
| 8 | =encoding utf-8 | ||||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | Bot::BasicBot::Pluggable::FromConfig - Create a bot from a config file. | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||
| 15 | |||||||||||||
| 16 | use Bot::BasicBot::Pluggable::FromConfig; | ||||||||||||
| 17 | |||||||||||||
| 18 | my $bot = Bot::BasicBot::Pluggable::FromConfig->new_with_config( | ||||||||||||
| 19 | config => {name => 'my_bot', path => 'some/path'}, | ||||||||||||
| 20 | ); | ||||||||||||
| 21 | |||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||
| 23 | |||||||||||||
| 24 | C<Bot::BasicBot::Pluggable::FromConfig> is a config loader for L<Bot::BasicBot::Pluggable> allowing all of your Bot configuration to be declared in its own file. It is largely based on L<Bot::BasicBot::Pluggable::WithConfig>. It's designed to allow for a wider degree of flexibility with its range of accepted config files. | ||||||||||||
| 25 | |||||||||||||
| 26 | FromConfig uses Config::JFDI to load a config based on the name supplied to the config argument. This allows pretty much any config style you want and also allows for a '_local' file to override on a per-instance bases if you need. | ||||||||||||
| 27 | |||||||||||||
| 28 | =head1 Running a Bot | ||||||||||||
| 29 | |||||||||||||
| 30 | This library provides a command line script called L<run_bot>. This provides a complete implementation of this module. Documentation can be found there. | ||||||||||||
| 31 | |||||||||||||
| 32 | Alternatively you can create your own implementation of this in your own scripts. The simplest method is to call new_with_config() and pass it a config name and then call run on the returned Bot object. | ||||||||||||
| 33 | |||||||||||||
| 34 | =head1 METHODS | ||||||||||||
| 35 | |||||||||||||
| 36 | =head2 new_with_config( config => 'my_bot' ) | ||||||||||||
| 37 | |||||||||||||
| 38 | This is the only method provided in this module beyond those described in L<Bot::BasicBot::Pluggable>. It accepts a hash as its arguments which must contain a config key. This key can either be the name of the config sans extension (which will be passed as the name param to Config::JFDI) or a hashref of params to pass through to Config::JFDI. | ||||||||||||
| 39 | |||||||||||||
| 40 | A new Bot::BasicBot::Pluggable::FromConfig object which inherits from L<Bot::BasicBot::Pluggable> is returned. | ||||||||||||
| 41 | |||||||||||||
| 42 | =cut | ||||||||||||
| 43 | |||||||||||||
| 44 | 1 | ▶ | use Config::JFDI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | 1 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 46 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 47 | |||||||||||||
| 48 | 1 | ▶ | use base 'Bot::BasicBot::Pluggable'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 49 | |||||||||||||
| 50 | 0 | ▶ | sub new_with_config { | ||||||||||
|
Subroutine
new_with_config: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||
| 51 | 0 | my ($class, %args) = @_; | |||||||||||
| 52 | |||||||||||||
| 53 | 0 | ▶ | croak 'No config param supplied.' unless $args{config}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | |||||||||||||
| 55 | 0 | ▶ | $args{config} = {name => $args{config}} unless ref $args{config} eq 'HASH'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | 0 | ▶ | my $conf = Config::JFDI->open(%{$args{config}}) or croak "Unable to load config file for: ".$args{config}->{name}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | 0 | ▶ | my @plugins = @{delete $conf->{plugins} || [] }; | ||||||||||
|
Condition0%0/2
delete $conf->{'plugins'} || []
MC/DC0%0/1
delete $conf->{'plugins'} || []
delete $conf->{'plugins'}
Truth table0%0/2
A
A= delete $conf->{'plugins'}
| |||||||||||||
| 58 | 0 | ▶ | my $bot = $class->new(%{$conf||{}}); | ||||||||||
|
Condition0%0/2
$conf || {}
MC/DC0%0/1
$conf || {}
$conf
Truth table0%0/2
A
A= $conf
| |||||||||||||
| 59 | |||||||||||||
| 60 | 0 | for my $plugin (@plugins){ | |||||||||||
| 61 | 0 | my $module_name = $plugin->{module}; | |||||||||||
| 62 | 0 | $module_name =~ s/Bot::BasicBot::Pluggable::Module//; | |||||||||||
| 63 | 0 | my $plugin_obj = $bot->load($module_name); | |||||||||||
| 64 | |||||||||||||
| 65 | 0 | ▶ | my %config = %{$plugin->{config} || {}}; | ||||||||||
|
Condition0%0/2
$plugin->{'config'} || {}
MC/DC0%0/1
$plugin->{'config'} || {}
$plugin->{'config'}
Truth table0%0/2
A
A= $plugin->{'config'}
| |||||||||||||
| 66 | 0 | for my $param (keys %config){ | |||||||||||
| 67 | 0 | $plugin_obj->set($param => $config{$param}); | |||||||||||
| 68 | } | ||||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | 0 | return $bot; | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 1 | 1; | |||||||||||
| 75 | __END__ | ||||||||||||
| 76 | |||||||||||||
| 77 | =head1 Config Keys | ||||||||||||
| 78 | |||||||||||||
| 79 | All attributes accepted by the constructor of L<Bot::BasicBot::Pluggable> and thus L<Bot::BasicBot> are valid configuration items. | ||||||||||||
| 80 | |||||||||||||
| 81 | =head2 plugins | ||||||||||||
| 82 | |||||||||||||
| 83 | An arrayref of plugins are also accepted: | ||||||||||||
| 84 | |||||||||||||
| 85 | { | ||||||||||||
| 86 | "channels": ["#perl"], | ||||||||||||
| 87 | "plugins": [ | ||||||||||||
| 88 | { | ||||||||||||
| 89 | "module": 'Karma', | ||||||||||||
| 90 | "config": { | ||||||||||||
| 91 | "karma_change_reponse": 0 | ||||||||||||
| 92 | }, | ||||||||||||
| 93 | }, | ||||||||||||
| 94 | { | ||||||||||||
| 95 | "module": "Bot::BasicBot::Pluggable::Module::Auth", | ||||||||||||
| 96 | }, | ||||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | Each should provide at minimum the name of the Module that implements the plugin. This can either be the full qualified name (Bot::BasicBot::Pluggable::Module::Name) or just the qualifying module name. | ||||||||||||
| 100 | |||||||||||||
| 101 | A config hashref can also be specified and these items will be passed to the plugin objects set() method. | ||||||||||||
| 102 | |||||||||||||
| 103 | =head1 KNOW ISSUES | ||||||||||||
| 104 | |||||||||||||
| 105 | There is known issue in Bot::BasicBot::Pluggable when using more recent version of Perl and POD::Checker. This will cause it to fail its t/release-pod-syntax test (https://rt.cpan.org/Public/Bug/Display.html?id=89806). Users should --force install B::BB::P before installing this module. | ||||||||||||
| 106 | |||||||||||||
| 107 | =head1 SEE ALSO | ||||||||||||
| 108 | |||||||||||||
| 109 | L<Config::JFDI> | ||||||||||||
| 110 | |||||||||||||
| 111 | L<Bot::BasicBot::Pluggable> | ||||||||||||
| 112 | |||||||||||||
| 113 | =head1 LICENSE | ||||||||||||
| 114 | |||||||||||||
| 115 | Copyright (C) Mike Francis. | ||||||||||||
| 116 | |||||||||||||
| 117 | This library is free software; you can redistribute it and/or modify | ||||||||||||
| 118 | it under the same terms as Perl itself. | ||||||||||||
| 119 | |||||||||||||
| 120 | =head1 AUTHOR | ||||||||||||
| 121 | |||||||||||||
| 122 | Mike Francis E<lt>ungrim97@gmail.comE<gt> | ||||||||||||
| 123 | |||||||||||||
| 124 | =cut | ||||||||||||
| 125 | |||||||||||||