Pickles/Config.pm

filter: Statementstmt 100% 114 / 114 Branchbran 78.5% 22 / 28 Conditioncond 80.0% 8 / 10 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 17 / 17 Podpod 57.1% 4 / 7 totaltotal 92.8% 168 / 181 CC 23 SCAR 14.1
CC
23
Cov
95%
get_config_files
7.0
setup_home
6.1
load_files
6.0
CRAP 4.1
« Pickles/Plugin/FillInForm.pm ↑ Summary Pickles/View/MicroTemplate.pm »
1 package Pickles::Config;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Plack::Util::Accessor qw(appname home);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Pickles::Util qw(env_value);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
10 11 my $class = shift;
11 11 my %args = @_;
12 11 my $self = bless {}, $class;
13 11 $self->{appname} = Pickles::Util::appname( $class );
14 11 $self->setup_home( $args{home} );
15 11 $self->{ACTION_PREFIX} = '';
16 11 ▶ $self->load_files( $args{files} || [] );
Condition100%2/2
$args{'files'} || []
l!l
29
MC/DC100%1/1
$args{'files'} || []
$args{'files'}
Truth table100%2/2
A
A= $args{'files'}
Aresult
11
00
17 11 $self;
18 }
19  
20 8 ▶sub construct {
Subroutine construct: called CC=1 SCAR=0.0
Pod documented
21 8 my $class = shift;
22 8 my $self = $class->new;
23 8 my $files = $self->get_config_files;
24 8 $self->load_files( $files );
25 8 $self;
26 }
27  
28 8 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
29 8 my( $self, $key, $default ) = @_;
30 8 ▶ return defined $self->{$key} ? $self->{$key} : $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$key} ? : 7 1 8
31 }
32  
33 11 ▶sub setup_home {
Subroutine setup_home: called CC=6 SCAR=18.0
Pod undocumented
34 11 my( $self, $home ) = @_;
35 my $dir =
36 11 ▶ $home || env_value( 'HOME', $self->appname ) || $ENV{'PICKLES_HOME'};
Condition66%4/6
$home || env_value('HOME', $self->appname) || $ENV{'PICKLES_HOME'}
l!l&&r!l&&!r
209
$home or env_value("HOME", $self->appname)
l!l&&r!l&&!r
209
MC/DC33%1/3
$home || env_value('HOME', $self->appname) || $ENV{'PICKLES_HOME'}
$home env_value("HOME", $self->appname) $ENV{'PICKLES_HOME'}
Truth table50%2/4
A or B || C
A= $homeB= env_value("HOME", $self->appname)C= $ENV{'PICKLES_HOME'}
ABCresult
1XX1
01X1
0011
0000
37 11 ▶ if ( $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 2 9 11
38 2 $self->{home} = dir( $dir );
39 }
40 else {
41 9 my $class = ref $self;
42 9 (my $file = "$class.pm") =~ s|::|/|g;
43 9 ▶ if (my $inc_path = $INC{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $inc_path = $INC{$file}) 9 0 9
44 9 (my $path = $inc_path) =~ s/$file$//;
45 9 my $home = dir($path)->absolute->cleanup;
46 9 ▶ $home = $home->parent while $home =~ /b?lib$/;
Branch: 1 branch
BranchTrueFalseTotal
$home =~ /b?lib$/ and do { $home = $home->parent } 9 9 18
47 9 $self->{home} = $home;
48 }
49 }
50 }
51  
52 19 ▶sub load_files {
Subroutine load_files: called CC=6 SCAR=17.9
Pod undocumented
53 19 my( $self, $files ) = @_;
54 19 my %config;
55  
56 # In 5.8.8 at least, putting $self in an evaled code produces
57 # extra warnings (and possibly break the behavior of __path_to)
58 # so we create a private closure, and plant the closure into
59 # the generated packes
60 19 $self->{__FILES} = [];
61  
62 19 ▶ my $path_to = sub { $self->path_to(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
63 1 ▶ my $load_file = sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
64 1 my $file = $path_to->( @_ );
65 1 delete $INC{$file};
66 1 my $subconf = require $file;
67 # death context should be at the calling config file level
68 1 ▶ Carp::croak("Could not parse $file: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
69 1 ▶ Carp::croak("Could not do $file: $!") if ! defined $subconf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subconf 0 1 1
70 1 ▶ Carp::croak("Could not run $file") if ! $subconf;
Branch: 1 branch
BranchTrueFalseTotal
unless $subconf 0 1 1
71  
72 1 push @{$self->{__FILES}}, $file;
73 1 return $subconf;
74 19 };
75  
76 19 for my $file( @{$files} ) {
77 # only do this if the file exists
78 14 ▶ next unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 14 14
79  
80 14 my $pkg = $file;
81 14 $pkg =~ s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg;
82  
83 14 my $fqname = sprintf '%s::%s', ref $self, $pkg;
84 { # XXX This is where we plant that closure
85 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
86 6 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 14 *{"$fqname\::__path_to"} = $path_to;
88 14 *{"$fqname\::load_file"} = $load_file;
89 }
90  
91 14 my $config_pkg = sprintf <<'SANDBOX', $fqname;
92 package %s;
93 {
94 my $conf = require $file or die $!;
95 $conf;
96 }
97 SANDBOX
98 14 delete $INC{$file};
99 14 ▶ my $conf = eval $config_pkg || +{};
Condition100%2/2
eval $config_pkg || {}
l!l
122
MC/DC100%1/1
eval $config_pkg || {}
eval $config_pkg
Truth table100%2/2
A
A= eval $config_pkg
Aresult
11
00
100 14 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 12 14
101 2 warn "Error while trying to read config file $file: $@";
102 }
103 %config = (
104 %config,
105 14 %{$conf},
106 );
107 }
108 19 push @{$self->{__FILES}}, @$files;
109 19 $self->{__TIME} = time;
110 19 for my $key( keys %config ) {
111 28 $self->{$key} = $config{$key};
112 }
113 19 \%config;
114 }
115  
116 8 ▶sub get_config_files {
Subroutine get_config_files: called CC=7 SCAR=19.5
Pod undocumented
117 8 my $self = shift;
118 8 my @files;
119  
120 8 ▶ if ( my $config_file = env_value('CONFIG', $self->appname) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config_file = env_value('CONFIG', $self->appname)) { } 2 6 8
121 2 push @files, $self->path_to( $config_file );
122 }
123 else {
124 6 my @base_files = ( File::Spec->catfile('etc', 'config.pl'), 'config.pl' );
125 6 foreach my $f (@base_files) {
126 12 my $base = $self->path_to($f);
127 12 ▶ push @files, $base if -e $base;
Branch: 1 branch
BranchTrueFalseTotal
if -e $base 6 6 12
128 }
129 }
130
131 8 ▶ if ( my $env = env_value('ENV', $self->appname) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $env = env_value("ENV", $self->appname)) 3 5 8
132 3 my @env_files;
133 3 for my $file( @files ) {
134 3 my ($v, $d, $fname) = File::Spec->splitpath( $file );
135 3 ▶ $fname =~ s/(\.[^\.]+)?$/$1 ? "_%s$1" : "%s"/e;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 3 0 3
136 3 my $template = File::Spec->catpath( $v, $d, $fname );
137 3 my $filename = sprintf $template, $env;
138 3 push @env_files, $self->path_to( $filename );
139  
140 }
141 3 push @files, @env_files;
142 }
143 8 return \@files;
144 }
145  
146 42 ▶sub path_to {
Subroutine path_to: called CC=2 SCAR=6.9
Pod documented
147 42 my( $self, @path ) = @_;
148 42 ▶ if ( File::Spec->file_name_is_absolute( $path[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->file_name_is_absolute($path[0])) 6 36 42
149 6 return File::Spec->catfile( @path );
150 }
151 36 file( $self->home, @path )->stringify;
152 }
153  
154 6 1;
155  
156 __END__
« Pickles/Plugin/FillInForm.pm ↑ Summary Pickles/View/MicroTemplate.pm »