| 1 | 6 | ▶ | use strictures 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | package Mojito::Role::Config; | ||||||||||||||||||||||||||||
| 3 | 6 | $Mojito::Role::Config::VERSION = '0.25'; | |||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use MooX::Types::MooseLike::Base qw(HashRef); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Dir::Self; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Path::Class qw(file); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Data::Dumper::Concise; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 6 | has 'config' => ( | |||||||||||||||||||||||||||
| 11 | is => 'rw', | ||||||||||||||||||||||||||||
| 12 | isa => HashRef, | ||||||||||||||||||||||||||||
| 13 | lazy => 1, | ||||||||||||||||||||||||||||
| 14 | builder => '_build_config', | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head2 _build_config | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Construct the configuration file. | ||||||||||||||||||||||||||||
| 20 | Config file is looked for in three locations: | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | ENV | ||||||||||||||||||||||||||||
| 23 | lib/Mojito/conf/mojito_local.conf | ||||||||||||||||||||||||||||
| 24 | lib/Mojito/conf/mojito.conf | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | The values will be merged with the precedent order being: | ||||||||||||||||||||||||||||
| 27 | ENV over | ||||||||||||||||||||||||||||
| 28 | mojito_local.conf over | ||||||||||||||||||||||||||||
| 29 | mojito.conf | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 7 | ▶ | sub _build_config { | ||||||||||||||||||||||||||
|
Subroutine
_build_config: called CC=6 SCAR=18.6
| |||||||||||||||||||||||||||||
| 34 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 7 | ▶ | warn "BUILD CONFIG" if $ENV{MOJITO_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 7 | my $conf_file = file(__DIR__ . '/../conf/mojito.conf'); | |||||||||||||||||||||||||||
| 38 | 7 | $conf_file->cleanup; | |||||||||||||||||||||||||||
| 39 | 7 | ▶ | $conf_file->resolve if (-e $conf_file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 7 | my $local_conf_file = file(__DIR__ . '/../conf/mojito_local.conf'); | |||||||||||||||||||||||||||
| 42 | 7 | $local_conf_file->cleanup; | |||||||||||||||||||||||||||
| 43 | 7 | ▶ | $local_conf_file->resolve if (-e $local_conf_file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 7 | my $env_conf_file = $ENV{MOJITO_CONFIG}; | |||||||||||||||||||||||||||
| 46 | 7 | ▶ | warn "ENV CONFIG: $ENV{MOJITO_CONFIG}" if ($ENV{MOJITO_DEBUG} and $ENV{MOJITO_CONFIG}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ENV{"MOJITO_DEBUG"} and $ENV{"MOJITO_CONFIG"}
MC/DC0%0/2
$ENV{"MOJITO_DEBUG"} and $ENV{"MOJITO_CONFIG"}
$ENV{"MOJITO_DEBUG"}
$ENV{"MOJITO_CONFIG"}
Truth table33%1/3
A and B
A= $ENV{"MOJITO_DEBUG"}B= $ENV{"MOJITO_CONFIG"}
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 7 | my $conf = $self->read_config($conf_file); | |||||||||||||||||||||||||||
| 49 | 7 | my $local_conf = $self->read_config($local_conf_file); | |||||||||||||||||||||||||||
| 50 | 7 | my $env_conf = $self->read_config($env_conf_file); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # The merge happens in pairs | ||||||||||||||||||||||||||||
| 53 | 7 | my $merged_conf = $self->merge_hash($local_conf, $conf); | |||||||||||||||||||||||||||
| 54 | 7 | $merged_conf = $self->merge_hash($env_conf, $merged_conf); | |||||||||||||||||||||||||||
| 55 | 7 | return $merged_conf; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head2 read_config | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Args: a configuration file name | ||||||||||||||||||||||||||||
| 61 | Returns: a HashRef of configuration values | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 21 | ▶ | sub read_config { | ||||||||||||||||||||||||||
|
Subroutine
read_config: called CC=7 SCAR=22.7
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 21 | my ($self, $conf_file) = @_; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 21 | my $config = {}; | |||||||||||||||||||||||||||
| 69 | 21 | ▶ | if ( $conf_file && -r $conf_file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$conf_file and -r $conf_file
MC/DC100%2/2
$conf_file and -r $conf_file
$conf_file
-r $conf_file
Truth table100%3/3
A and B
A= $conf_fileB= -r $conf_file
| |||||||||||||||||||||||||||||
| 70 | 7 | ▶ | if ( not $config = do $conf_file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | die qq/Can't do config file "$conf_file" EXCEPTION: $@/ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | die qq/Can't do config file "$conf_file" UNDEFINED: $!/ if not defined $config; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Let's add in the version number. | ||||||||||||||||||||||||||||
| 77 | 21 | ▶ | $config->{VERSION} = $Mojito::Role::Config::VERSION || 'development version'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$Mojito::Role::Config::VERSION || 'development version'
MC/DC0%0/1
$Mojito::Role::Config::VERSION || 'development version'
$Mojito::Role::Config::VERSION
Truth table50%1/2
A
A= $Mojito::Role::Config::VERSION
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 21 | return $config; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head2 merge_hash | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Args: ($hash_ref_dominant, $hash_ref_subordinate) | ||||||||||||||||||||||||||||
| 85 | Returns: HashRef of the two merged with the dominant values | ||||||||||||||||||||||||||||
| 86 | chosen when they exist otherwise the subordinate values are used. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 14 | ▶ | sub merge_hash { | ||||||||||||||||||||||||||
|
Subroutine
merge_hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 14 | my ($self, $precedent, $subordinate) = @_; | |||||||||||||||||||||||||||
| 92 | 14 | my @not = grep !exists $precedent->{$_}, keys %{$subordinate}; | |||||||||||||||||||||||||||
| 93 | 14 | @{$precedent}{@not} = @{$subordinate}{@not}; | |||||||||||||||||||||||||||
| 94 | 14 | return $precedent; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 6 | 1; | |||||||||||||||||||||||||||