lib/Dancer2/Core/Role/HasConfig.pm

filter: Statementstmt 100% 67 / 67 Branchbran 92.8% 13 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 20 / 20 Podpod 100% 3 / 3 totaltotal 99.0% 103 / 104 CC 9 SCAR 6.2
CC
9
Cov
99%
__ANON__
4.0
_compile_config_entry
3.0
__ANON__
2.1
CRAP 1.9
« lib/Dancer2/Core/Factory.pm ↑ Summary lib/Dancer2/Core/MIME.pm »
1 # ABSTRACT: Role that represents the config of Dancer2 App
2 package Dancer2::Core::Role::HasConfig;
3 185 $Dancer2::Core::Role::HasConfig::VERSION = '2.3.0';
4 185 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 185 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 185 ▶use Hash::Merge::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 185 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 185 ▶use Module::Runtime qw{ require_module use_module };
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 185 ▶use Dancer2::Core::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 185 ▶use Dancer2::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 185 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 185 ▶use Dancer2::ConfigUtils 'normalize_config_entry';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 185 has config => (
17 is => 'ro',
18 isa => HashRef,
19 lazy => 1,
20 builder => '_build_config',
21 );
22  
23 has local_triggers => (
24 is => 'ro',
25 isa => HashRef,
26 2 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 185 );
28  
29 has global_triggers => (
30 is => 'ro',
31 isa => HashRef,
32 297 ▶ default => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
33 my $triggers = {
34 1 ▶ traces => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
35 1 my ( $self, $traces ) = @_;
36 # Carp is already a dependency
37 1 ▶ $Carp::Verbose = $traces ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$traces ? : 1 0 1
38 },
39 297 };
40  
41 297 my $runner_config;
42 {
43 185 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 297 ▶ $runner_config = defined $Dancer2::runner
Branch: 1 branch
BranchTrueFalseTotal
defined $Dancer2::runner ? : 282 15 297
45 ? Dancer2->runner->config
46 : {};
47 }
48  
49 297 for my $global ( keys %$runner_config ) {
50 1974 ▶ next if exists $triggers->{$global};
Branch: 1 branch
BranchTrueFalseTotal
if exists $triggers->{$global} 282 1692 1974
51 3 ▶ $triggers->{$global} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 3 my ($self, $value) = @_;
53 3 Dancer2->runner->config->{$global} = $value;
54 }
55 1692 }
56  
57 297 return $triggers;
58 },
59 185 );
60  
61 238 ▶sub _set_config_entries {
Subroutine _set_config_entries: called CC=2 SCAR=6.9
62 238 my ( $self, @args ) = @_;
63 238 my $no = scalar @args;
64 238 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 244 237 481
65 244 $self->_set_config_entry( shift(@args), shift(@args) );
66 }
67 237 return $no;
68 }
69  
70 244 ▶sub _set_config_entry {
Subroutine _set_config_entry: called CC=1 SCAR=0.0
71 244 my ( $self, $name, $value ) = @_;
72  
73 244 $value = normalize_config_entry( $name, $value );
74 244 $value = $self->_compile_config_entry( $name, $value, $self->config );
75 243 $self->config->{$name} = $value;
76 }
77  
78 3 ▶sub settings { shift->config }
Subroutine settings: called CC=1 SCAR=0.0
Pod documented
79  
80 707 ▶sub setting {
Subroutine setting: called CC=2 SCAR=6.9
Pod documented
81 707 my $self = shift;
82 707 my @args = @_;
83  
84 return ( scalar @args == 1 )
85 707 ▶ ? $self->settings->{ $args[0] }
Branch: 1 branch
BranchTrueFalseTotal
scalar @args == 1 ? : 469 238 707
86 : $self->_set_config_entries(@args);
87 }
88  
89 4 ▶sub has_setting {
Subroutine has_setting: called CC=1 SCAR=0.0
Pod documented
90 4 my ( $self, $name ) = @_;
91 4 return exists $self->config->{$name};
92 }
93  
94 # private
95  
96 244 ▶sub _compile_config_entry {
Subroutine _compile_config_entry: called CC=3 SCAR=11.0
97 244 my ( $self, $name, $value, $config ) = @_;
98  
99 my $trigger = exists $self->local_triggers->{$name} ?
100 $self->local_triggers->{$name} :
101 244 ▶ $self->global_triggers->{$name};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->local_triggers->{$name} ? : 156 88 244
102  
103 244 ▶ defined $trigger or return $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $trigger or return $value 160 84 244
104  
105 160 return $trigger->( $self, $value, $config );
106 }
107  
108 185 1;
109  
110 __END__
« lib/Dancer2/Core/Factory.pm ↑ Summary lib/Dancer2/Core/MIME.pm »