| 1 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package CHI::Config; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | our $VERSION = '0.001001'; # TRIAL | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: Define CHI configuration outside your code | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Moo qw( has around ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | has '_constructor_caller' => ( | |||||||||||||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||||||||||||
| 18 | required => 1, | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | has '_config_paths' => ( | |||||||||||||||||||||||||||
| 22 | init_arg => 'config_paths', | ||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||
| 24 | lazy => 1, | ||||||||||||||||||||||||||||
| 25 | builder => '_build_config_paths', | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 3 | has '_config_files' => ( | |||||||||||||||||||||||||||
| 29 | init_arg => 'config_files', | ||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||
| 31 | lazy => 1, | ||||||||||||||||||||||||||||
| 32 | builder => '_build_config_files', | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | has '_config' => ( | |||||||||||||||||||||||||||
| 36 | init_arg => undef, | ||||||||||||||||||||||||||||
| 37 | is => 'ro', | ||||||||||||||||||||||||||||
| 38 | lazy => 1, | ||||||||||||||||||||||||||||
| 39 | builder => '_build_config', | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3 | has '_defaults' => ( | |||||||||||||||||||||||||||
| 43 | init_arg => 'defaults', | ||||||||||||||||||||||||||||
| 44 | is => ro =>, | ||||||||||||||||||||||||||||
| 45 | lazy => 1, | ||||||||||||||||||||||||||||
| 46 | builder => '_build_defaults', | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | has '_drivers' => ( | |||||||||||||||||||||||||||
| 50 | is => 'ro', | ||||||||||||||||||||||||||||
| 51 | init_arg => undef, | ||||||||||||||||||||||||||||
| 52 | lazy => 1, | ||||||||||||||||||||||||||||
| 53 | builder => '_build_drivers', | ||||||||||||||||||||||||||||
| 54 | handles => { | ||||||||||||||||||||||||||||
| 55 | '_add_driver' => 'add_driver', | ||||||||||||||||||||||||||||
| 56 | '_get_driver' => 'get_driver', | ||||||||||||||||||||||||||||
| 57 | 'get_cache' => 'get_cache', | ||||||||||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=3 SCAR=13.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 2 | my ( undef, @args ) = @_; | |||||||||||||||||||||||||||
| 63 | 2 | my (@caller) = caller 1; # +1 for Moo | |||||||||||||||||||||||||||
| 64 | 2 | ▶ | if ( 1 == @args and ref $args[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
1 == @args and ref $args[0]
MC/DC0%0/2
1 == @args and ref $args[0]
1 == @args
ref $args[0]
Truth table33%1/3
A and B
A= 1 == @argsB= ref $args[0]
| |||||||||||||||||||||||||||||
| 65 | 0 | $args[0]->{_constructor_caller} = $caller[1]; | |||||||||||||||||||||||||||
| 66 | 0 | return $args[0]; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 2 | return { @args, _constructor_caller => $caller[1] }; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 73 | ## no critic (Variables::ProhibitPackageVars) | ||||||||||||||||||||||||||||
| 74 | 2 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; | |||||||||||||||||||||||||||
| 75 | 2 | $self->_load_config; | |||||||||||||||||||||||||||
| 76 | 2 | $self->_load_defaults; | |||||||||||||||||||||||||||
| 77 | 2 | return; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub _build_config_paths { | ||||||||||||||||||||||||||
|
Subroutine
_build_config_paths: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 81 | 0 | require File::HomeDir; | |||||||||||||||||||||||||||
| 82 | my (@scan_paths) = ( | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | ( exists $ENV{CHI_CONFIG_DIR} ? $ENV{CHI_CONFIG_DIR} . '/config' : () ), # From ENV | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | './chi_config', # ./ | ||||||||||||||||||||||||||||
| 85 | File::HomeDir->my_home . '/.chi/config', # ~/.chi/ | ||||||||||||||||||||||||||||
| 86 | '/etc/chi/config', # /etc/chi/ | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | 0 | return \@scan_paths; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2 | ▶ | sub _build_config_files { return [] } | ||||||||||||||||||||||||||
|
Subroutine
_build_config_files: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 2 | ▶ | sub _build_config { | ||||||||||||||||||||||||||
|
Subroutine
_build_config: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 94 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 95 | 2 | require Config::Any; | |||||||||||||||||||||||||||
| 96 | 2 | my %extras = ( use_ext => 1 ); | |||||||||||||||||||||||||||
| 97 | 2 | ▶ | return Config::Any->load_files( { files => $self->_config_files, %extras } ) if @{ $self->_config_files }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 2 | return Config::Any->load_stems( { stems => $self->_config_paths, %extras } ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub _build_defaults { | ||||||||||||||||||||||||||
|
Subroutine
_build_defaults: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 102 | 0 | return []; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 2 | ▶ | sub _build_drivers { | ||||||||||||||||||||||||||
|
Subroutine
_build_drivers: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 106 | 2 | require CHI::Config::DriverPool; | |||||||||||||||||||||||||||
| 107 | 2 | return CHI::Config::DriverPool->new(); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 2 | ▶ | sub _load_version { | ||||||||||||||||||||||||||
|
Subroutine
_load_version: called CC=7 SCAR=25.3
| |||||||||||||||||||||||||||||
| 111 | 2 | my ( undef, %entry ) = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 2 | my $spec = '0.1.0'; | |||||||||||||||||||||||||||
| 114 | 2 | my $entry_msg = "$entry{file} ( entry #$entry{entry_no} )"; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | ▶ | if ( $entry{spec} and $entry{spec} ne $spec ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$entry{"spec"} and $entry{"spec"} ne $spec
MC/DC0%0/2
$entry{"spec"} and $entry{"spec"} ne $spec
$entry{"spec"}
$entry{"spec"} ne $spec
Truth table33%1/3
A and B
A= $entry{"spec"}B= $entry{"spec"} ne $spec
| |||||||||||||||||||||||||||||
| 117 | 0 | croak "Spec version required by $entry_msg is ==$entry{spec}, this is $spec"; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 2 | ▶ | if ( $entry{min} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 2 | require version; | |||||||||||||||||||||||||||
| 121 | 2 | my $min = version->parse( $entry{min} ); | |||||||||||||||||||||||||||
| 122 | 2 | ▶ | if ( $min > $VERSION ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | croak "Minimum version required by $entry_msg is $min, we have $VERSION"; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 2 | ▶ | if ( $entry{max} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | require version; | |||||||||||||||||||||||||||
| 128 | 0 | my $max = version->parse( $entry{max} ); | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ( $max < $VERSION ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | croak "Maximum version required by $entry_msg is $max, we have $VERSION"; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 2 | return; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 10 | ▶ | sub _load_entry { | ||||||||||||||||||||||||||
|
Subroutine
_load_entry: called CC=5 SCAR=16.5
| |||||||||||||||||||||||||||||
| 137 | 10 | my ( $self, %entry ) = @_; | |||||||||||||||||||||||||||
| 138 | 10 | my $context = sprintf q[%s ( entry #%s )], $entry{file}, $entry{entry_no}; | |||||||||||||||||||||||||||
| 139 | 10 | ▶ | my $name = ( $entry{name} ? q[ named ] . $entry{name} : q[ ] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 10 | ▶ | croak "No type specified for entry$name in $context" unless defined $entry{type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 10 | ▶ | return $self->_load_version(%entry) if 'version' eq $entry{type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 8 | ▶ | return $self->_add_driver(%entry) if 'driver' eq $entry{type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | croak "Unknown type $entry{type} in $context"; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 6 | ▶ | sub _load_array { | ||||||||||||||||||||||||||
|
Subroutine
_load_array: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 147 | 6 | my ( $self, $array, $file ) = @_; | |||||||||||||||||||||||||||
| 148 | 6 | ▶ | if ( not 'ARRAY' eq ref $array ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | return croak "Payload in $file should be an ARRAY of HASH"; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 6 | my $entry_no = 1; | |||||||||||||||||||||||||||
| 152 | 6 | for my $entry ( @{$array} ) { | |||||||||||||||||||||||||||
| 153 | 10 | ▶ | if ( not 'HASH' eq ref $entry ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | return croak "Entry $entry_no in $file is not a HASH"; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | $self->_load_entry( | ||||||||||||||||||||||||||||
| 157 | 10 | %{$entry}, | |||||||||||||||||||||||||||
| 158 | file => $file, # | ||||||||||||||||||||||||||||
| 159 | entry_no => $entry_no, # | ||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||
| 161 | 10 | $entry_no++; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 6 | return; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | ▶ | sub _load_config { | ||||||||||||||||||||||||||
|
Subroutine
_load_config: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 167 | 2 | my ( $self, ) = @_; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # Load from config first. | ||||||||||||||||||||||||||||
| 170 | 2 | for my $result ( @{ $self->_config } ) { | |||||||||||||||||||||||||||
| 171 | 4 | for my $file ( keys %{$result} ) { | |||||||||||||||||||||||||||
| 172 | 4 | $self->_load_array( $result->{$file}, $file ); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 2 | return; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 2 | ▶ | sub _load_defaults { | ||||||||||||||||||||||||||
|
Subroutine
_load_defaults: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 179 | 2 | my ( $self, ) = @_; | |||||||||||||||||||||||||||
| 180 | 2 | return $self->_load_array( $self->_defaults, $self->_constructor_caller ); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 3 | ▶ | no Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 3 | 1; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | __END__ | ||||||||||||||||||||||||||||