File Coverage

lib/CHI/Config/DriverPool.pm
Criterion Covered Total %
statement 33 39 84.6
branch 3 4 75.0
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 49 56 87.5


line stmt bran cond sub pod time code
1 3     3   584 use 5.006;
  3         7  
  3         97  
2 3     3   11 use strict;
  3         9  
  3         79  
3 3     3   10 use warnings;
  3         2  
  3         164  
4              
5             package CHI::Config::DriverPool;
6              
7             our $VERSION = '0.001000'; # TRIAL
8              
9             # ABSTRACT: A Collection of Driver definitions
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 3     3   464 use Moo qw( has );
  3         11187  
  3         16  
14 3     3   1786 use Carp qw( carp croak );
  3         4  
  3         224  
15              
16             ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
17 3     3   13 use constant DEBUGGING => $ENV{CHI_CONFIG_DEBUG};
  3         3  
  3         909  
18              
19             has '_drivers' => ( is => ro =>, lazy => 1, default => sub { {} } );
20              
21             sub add_driver {
22 8     8 1 1625 my ( $self, %config ) = @_;
23              
24 8         714 require CHI::Config::Driver;
25              
26 8         104 my $instance = CHI::Config::Driver->new(%config);
27 8         1991 my $name = $instance->name;
28              
29 8 100       94 if ( not exists $self->_drivers->{$name} ) {
30 4         69 $self->_drivers->{$name} = $instance;
31 4         36 return;
32             }
33 4         51 return unless DEBUGGING;
34             ## Shadowing should be default anyway.
35 0         0 my $template = '%s: %s';
36 0         0 my $old = sprintf $template, ' Kept', $self->_drivers->{$name}->source;
37 0         0 my $new = sprintf $template, 'Ignored', $instance->source;
38 0         0 carp "Duplicate Driver definition ignored\n\t$old\n\t$new\n";
39 0         0 return;
40             }
41              
42             sub get_driver {
43 2     2 1 4 my ( $self, $name ) = @_;
44 2 50       44 return $self->_drivers->{$name} if exists $self->_drivers->{$name};
45 0         0 croak "No default for driver <$name> defined and none specified in configuration";
46             }
47              
48             sub get_cache {
49 2     2 1 699 my ( $self, $name ) = @_;
50 2         7 return $self->get_driver($name)->get_cache;
51             }
52              
53 3     3   12 no Moo;
  3         4  
  3         12  
54              
55             1;
56              
57             __END__