File Coverage

blib/lib/Bot/Cobalt/Conf/File/Plugins.pm
Criterion Covered Total %
statement 66 68 97.0
branch 15 22 68.1
condition 3 9 33.3
subroutine 19 19 100.0
pod 5 5 100.0
total 108 123 87.8


line stmt bran cond sub pod time code
1             package Bot::Cobalt::Conf::File::Plugins;
2             $Bot::Cobalt::Conf::File::Plugins::VERSION = '0.021003';
3 5     5   12763 use v5.10;
  5         12  
4 5     5   384 use strictures 2;
  5         1455  
  5         133  
5 5     5   684 use Carp;
  5         7  
  5         236  
6              
7 5     5   18 use Scalar::Util 'blessed';
  5         5  
  5         199  
8              
9 5     5   448 use Bot::Cobalt::Common ':types';
  5         8  
  5         23  
10 5     5   1822 use Bot::Cobalt::Conf::File::PerPlugin;
  5         10  
  5         142  
11              
12 5     5   25 use Path::Tiny;
  5         5  
  5         220  
13 5     5   18 use Types::Path::Tiny -types;
  5         5  
  5         24  
14              
15 5     5   2573 use List::Objects::WithUtils;
  5         6  
  5         35  
16 5     5   5007 use List::Objects::Types -types;
  5         5  
  5         25  
17              
18 5     5   3819 use Moo;
  5         7  
  5         17  
19             extends 'Bot::Cobalt::Conf::File';
20              
21              
22             has etcdir => (
23             required => 1,
24             is => 'rwp',
25             isa => Path,
26             coerce => 1,
27             );
28              
29              
30             has _per_plug_objs => (
31             lazy => 1,
32             is => 'ro',
33             isa => HashObj,
34             coerce => 1,
35             builder => '_build_per_plugin_objs',
36             );
37              
38             sub _build_per_plugin_objs {
39 2     2   1310 my ($self) = @_;
40            
41             ## Create PerPlugin cf objs for each plugin
42             hash(
43             $self->cfg_as_hash->keys->map(sub {
44 40     40   3020 ( $_ => $self->_create_perplugin_obj($_) )
45 2         36 })->all
46             )
47             }
48              
49             sub _create_perplugin_obj {
50 41     41   43 my ($self, $alias) = @_;
51            
52 41   33     617 my $this_cfg = $self->cfg_as_hash->get($alias)
53             || confess "_create_perplugin_obj passed unknown alias $alias";
54              
55 41         329 my %new_opts;
56              
57             $new_opts{module} = $this_cfg->{Module}
58 41   33     90 || confess "No Module defined for plugin $alias";
59              
60 41 100       67 if (defined $this_cfg->{Config}) {
61 14         34 my $this_cf_path = path($this_cfg->{Config});
62 14 50       266 unless ( $this_cf_path->is_absolute ) {
63 14         323 $this_cf_path = path( $self->etcdir .'/'. $this_cf_path );
64             }
65              
66 14         246 $new_opts{config_file} = $this_cf_path
67             }
68              
69             $new_opts{autoload} = 0
70 41 100       67 if $this_cfg->{NoAutoLoad};
71              
72             $new_opts{priority} = $this_cfg->{Priority}
73 41 100       67 if defined $this_cfg->{Priority};
74              
75 41 100       58 if (defined $this_cfg->{Opts}) {
76             confess "Opts: directive for plugin $alias is not a hash"
77 7 50       23 unless ref $this_cfg->{Opts} eq 'HASH';
78            
79 7         11 $new_opts{extra_opts} = $this_cfg->{Opts};
80             }
81              
82             Bot::Cobalt::Conf::File::PerPlugin->new(
83 41         628 %new_opts
84             );
85             }
86              
87             sub plugin {
88 52     52 1 11349 my ($self, $plugin) = @_;
89            
90 52 50       101 confess "plugin() requires a plugin alias"
91             unless defined $plugin;
92              
93 52         976 $self->_per_plug_objs->{$plugin}
94             }
95              
96             sub list_plugins {
97 2     2 1 435 my ($self) = @_;
98            
99 2         4 [ keys %{ $self->_per_plug_objs } ]
  2         23  
100             }
101              
102             sub clear_plugin {
103 1     1 1 190 my ($self, $plugin) = @_;
104            
105 1 50       4 confess "clear_plugin requires a plugin alias"
106             unless defined $plugin;
107              
108 1         19 delete $self->_per_plug_objs->{$plugin}
109             }
110              
111             sub load_plugin {
112 1     1 1 201 my ($self, $plugin) = @_;
113            
114 1 50       4 confess "load_plugin requires a plugin alias"
115             unless defined $plugin;
116            
117 1         3 $self->_per_plug_objs->{$plugin}
118             = $self->_create_perplugin_obj($plugin)
119             }
120              
121             sub install_plugin {
122 1     1 1 341 my ($self, $plugin, $plugin_obj) = @_;
123            
124 1 50       5 unless (defined $plugin_obj) {
125 0         0 confess
126             "install_plugin requires a plugin alias and Conf object"
127             }
128            
129 1 50 33     12 unless (blessed $plugin_obj &&
130             $plugin_obj->isa('Bot::Cobalt::Conf::File::PerPlugin') ) {
131            
132 0         0 confess
133             "install_plugin requires a Bot::Cobalt::Conf::File::PerPlugin object"
134             }
135            
136 1         27 $self->_per_plug_objs->{$plugin} = $plugin_obj
137             }
138              
139              
140             around 'validate' => sub {
141             my ($orig, $self, $cfg) = @_;
142              
143             1
144             };
145              
146              
147             1;
148             __END__