blib/lib/Mojolicious/Plugin/YamlConfig.pm

filter: Statementstmt 91.6% 33 / 36 Branchbran 50.0% 6 / 12 Conditioncond 38.4% 5 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 68.0% 51 / 75 CC 12 SCAR 20.0
CC
12
Cov
72%
parse
10.9
register
5.8
BEGIN
1.0
CRAP 7.4
↑ Summary
1 # vim: ai et sw=4
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package Mojolicious::Plugin::YamlConfig;
5  
6 1 ▶use base 'Mojolicious::Plugin::JSONConfig';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.2.4';
9  
10 3 ▶sub register {
Subroutine register: called CC=5 SCAR=17.6
Pod documented
11 3 my ( $self, $app, $conf ) = @_;
12 3 ▶ $conf ||= {};
Condition50%1/2
$conf ||= {}
l!l
30
MC/DC0%0/1
$conf ||= {}
$conf
Truth table50%1/2
A
A= $conf
Aresult
11
00
13 3 $conf->{ext} = 'yaml';
14 3 ▶ $conf->{class} ||= $ENV{MOJO_YAML} || 'YAML::Tiny';
Condition40%2/5
$conf->{"class"} ||= $ENV{"MOJO_YAML"} || "YAML::Tiny"
l!l&&r!l&&!r
030
$ENV{'MOJO_YAML'} || 'YAML::Tiny'
l!l
03
MC/DC0%0/2
$conf->{"class"} ||= $ENV{"MOJO_YAML"} || "YAML::Tiny"
$conf->{"class"} $ENV{'MOJO_YAML'}
Truth table0%0/3
A ||= B
A= $conf->{"class"}B= $ENV{'MOJO_YAML'}
ABresult
1X1
011
000
15 3 $self->{class} = $conf->{class};
16 3 my @supported = qw(YAML YAML::XS YAML::Tiny YAML::Old YAML::PP);
17 3 ▶ unless ( grep { $conf->{class} eq $_ } @supported) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$conf->{"class"} eq $_;} @supported) 0 3 3
18 0 warn("$conf->{class} is not supported, use at your own risk");
19 }
20 3 return $self->SUPER::register( $app, $conf );
21 }
22  
23 1 ▶sub parse {
Subroutine parse: called CC=8 SCAR=23.9
Pod documented
24 1 my ($self, $content, $file, $conf, $app) = @_;
25  
26 1 my $class = $self->{class};
27 1 ▶ eval "require $class; 1" || die($@);
Branch: 1 branch
BranchTrueFalseTotal
eval "require $class; 1" or die $@ 1 0 1
28  
29 1 my ($config,$error);
30  
31 # Render
32 1 $content = $self->render($content, $file, $conf, $app);
33  
34 1 my @broken = qw(YAML YAML::Old YAML::Tiny YAML::PP);
35 1 ▶ unless (grep { $class eq $_ } @broken) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$class eq $_;} @broken) 0 1 1
36 # they are broken *sigh*
37 0 $content = Encode::encode('UTF-8', $content);
38 }
39  
40 1 $config = eval $class.'::Load($content)';
41 1 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
42 0 $error = $@;
43 }
44  
45 1 ▶ die qq/Couldn't parse config "$file": $error/ if !$config && $error;
Branch: 1 branch
BranchTrueFalseTotal
if not $config and $error 0 1 1
Condition33%1/3
not $config and $error
!ll&&!rl&&r
100
MC/DC0%0/2
not $config and $error
not $config $error
Truth table33%1/3
not(A) and B
A= not $configB= $error
ABresult
0X0
100
111
46 1 ▶ die qq/Invalid config "$file"./ if !$config || ref $config ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not $config or ref $config ne "HASH" 0 1 1
Condition33%1/3
not $config or ref $config ne "HASH"
l!l&&r!l&&!r
001
MC/DC0%0/2
not $config or ref $config ne "HASH"
not $config ref $config ne "HASH"
Truth table33%1/3
not(A) or B
A= not $configB= ref $config ne "HASH"
ABresult
1X1
011
000
47  
48 1 return $config;
49 }
50  
51 1 1;
52  
53 __END__
↑ Summary