Catmandu/Env.pm

filter: Statementstmt 91.7% 134 / 146 Branchbran 65.9% 29 / 44 Conditioncond 68.9% 20 / 29 MC/DCmcdc 38.8% 7 / 18 Subroutinesub 94.4% 34 / 36 Podpod 0.0% 0 / 9 totaltotal 79.4% 224 / 282 CC 34 SCAR 19.9
CC
34
Cov
84%
_search_up
20.0
_named_package
16.4
BUILD
8.4
CRAP 7.3
« Catmandu/Fix/sort_field.pm ↑ Summary Catmandu/Paged.pm »
1 package Catmandu::Env;
2  
3 154 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 154 our $VERSION = '1.2025';
6  
7 154 ▶use Catmandu::Util qw(require_package use_lib read_yaml read_json :is :check);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 154 ▶use Catmandu::Fix;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 154 ▶use Config::Onion;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 154 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 154 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 154 require Catmandu;
13 154 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 154 with 'Catmandu::Logger';
16  
17 0 ▶sub _search_up {
Subroutine _search_up: not called CC=4 SCAR=30.0
18 0 my $dir = $_[0];
19 0 my @dirs = grep length, File::Spec->splitdir(Catmandu->default_load_path);
20 0 ▶ for (; @dirs; pop @dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 0 0 0
21 0 my $path = File::Spec->catdir(File::Spec->rootdir, @dirs);
22 0 ▶ opendir my $dh, $path or last;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $path or last 0 0 0
23 return $path
24 0 ▶ if grep {-r File::Spec->catfile($path, $_)}
Branch: 1 branch
BranchTrueFalseTotal
if grep {-r "File::Spec"->catfile($path, $_);} grep(/^catmandu.+(?:yaml|yml|json|pl)$/, readdir $dh) 0 0 0
25 grep /^catmandu.+(?:yaml|yml|json|pl)$/, readdir $dh;
26 }
27 0 Catmandu->default_load_path;
28 }
29  
30 has load_paths => (
31 is => 'ro',
32 0 ▶ default => sub {[]},
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 66 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
34 [
35 66 map {File::Spec->canonpath($_)}
36 66 ▶ map {$_ eq ':up' ? _search_up($_) : $_} split /,/,
Branch: 2 branches
BranchTrueFalseTotal
$_ eq ':up' ? : 0 66 66
ref $_[0] ? : 51 15 66
37 join ',',
38 66 ref $_[0] ? @{$_[0]} : $_[0]
39 ];
40 },
41 154 );
42  
43 66 ▶has config => (is => 'rwp', default => sub {+{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
44  
45 66 ▶has stores => (is => 'ro', default => sub {+{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
46 66 ▶has validators => (is => 'ro', default => sub {+{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
47 66 ▶has fixers => (is => 'ro', default => sub {+{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
48  
49 66 ▶has default_store => (is => 'ro', default => sub {'default'});
Subroutine __ANON__: called CC=1 SCAR=0.0
50 66 ▶has default_importer => (is => 'ro', default => sub {'default'});
Subroutine __ANON__: called CC=1 SCAR=0.0
51 66 ▶has default_exporter => (is => 'ro', default => sub {'default'});
Subroutine __ANON__: called CC=1 SCAR=0.0
52 66 ▶has default_validator => (is => 'ro', default => sub {'default'});
Subroutine __ANON__: called CC=1 SCAR=0.0
53 66 ▶has default_fixer => (is => 'ro', default => sub {'default'});
Subroutine __ANON__: called CC=1 SCAR=0.0
54  
55 154 has default_store_package => (is => 'ro');
56 66 ▶has default_importer_package => (is => 'ro', default => sub {'JSON'});
Subroutine __ANON__: called CC=1 SCAR=0.0
57 66 ▶has default_exporter_package => (is => 'ro', default => sub {'JSON'});
Subroutine __ANON__: called CC=1 SCAR=0.0
58 154 has default_validator_package => (is => 'ro');
59  
60 66 ▶has store_namespace => (is => 'ro', default => sub {'Catmandu::Store'});
Subroutine __ANON__: called CC=1 SCAR=0.0
61 66 ▶has importer_namespace => (is => 'ro', default => sub {'Catmandu::Importer'});
Subroutine __ANON__: called CC=1 SCAR=0.0
62 66 ▶has exporter_namespace => (is => 'ro', default => sub {'Catmandu::Exporter'});
Subroutine __ANON__: called CC=1 SCAR=0.0
63 has validator_namespace =>
64 66 ▶ (is => 'ro', default => sub {'Catmandu::Validator'});
Subroutine __ANON__: called CC=1 SCAR=0.0
65  
66 66 ▶sub BUILD {
Subroutine BUILD: called CC=8 SCAR=21.3
Pod undocumented
67 66 my ($self) = @_;
68  
69 66 my @config_dirs = @{$self->load_paths};
70 66 my @lib_dirs;
71  
72 66 for my $dir (@config_dirs) {
73 66 ▶ if (!-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 66 66
74 0 Catmandu::Error->throw("load path $dir doesn't exist");
75 }
76  
77 66 my $lib_dir = File::Spec->catdir($dir, 'lib');
78  
79 66 ▶ if (-d $lib_dir && -r $lib_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $lib_dir and -r $lib_dir) 66 0 66
Condition33%1/3
-d $lib_dir and -r $lib_dir
!ll&&!rl&&r
0066
MC/DC0%0/2
-d $lib_dir and -r $lib_dir
-d $lib_dir -r $lib_dir
Truth table33%1/3
A and B
A= -d $lib_dirB= -r $lib_dir
ABresult
0X0
100
111
80 66 push @lib_dirs, $lib_dir;
81 }
82 }
83  
84 66 ▶ if (@config_dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@config_dirs) 66 0 66
85 my @globs = map {
86 66 my $dir = $_;
87 66 map {File::Spec->catfile($dir, "catmandu*.$_")}
88 qw(yaml yml json pl)
89 } reverse @config_dirs;
90  
91 66 my $config = Config::Onion->new(prefix_key => '_prefix');
92 66 $config->load_glob(@globs);
93  
94 66 ▶ if ($self->log->is_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->log->is_debug) 13 53 66
95 154 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 13 $self->log->debug(Dumper($config->get));
97 }
98 66 $self->_set_config($config->get);
99 }
100  
101 66 ▶ if (@lib_dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lib_dirs) 66 0 66
102 66 lib->import(@lib_dirs);
103 }
104 }
105  
106 2 ▶sub load_path {
Subroutine load_path: called CC=1 SCAR=0.0
Pod undocumented
107 2 $_[0]->load_paths->[0];
108 }
109  
110 5 ▶sub roots {
Subroutine roots: called CC=1 SCAR=0.0
Pod undocumented
111 5 goto &load_paths;
112 }
113  
114 2 ▶sub root {
Subroutine root: called CC=1 SCAR=0.0
Pod undocumented
115 2 goto &load_path;
116 }
117  
118 12 ▶sub fixer {
Subroutine fixer: called CC=7 SCAR=20.3
Pod undocumented
119 12 my $self = shift;
120  
121 # it's already a fixer
122 12 ▶ if (is_instance($_[0], 'Catmandu::Fix')) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_instance($_[0], "Catmandu::Fix")) 0 12 12
123 0 return $_[0];
124 }
125  
126 # an array ref of fixes
127 12 ▶ if (is_array_ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array_ref($_[0])) 2 10 12
128 2 return Catmandu::Fix->new(fixes => $_[0]);
129 }
130  
131 # a single fix
132 10 ▶ if (is_able($_[0], 'fix')) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_able($_[0], "fix")) 0 10 10
133 0 return Catmandu::Fix->new(fixes => [$_[0]]);
134 }
135  
136 # try to load from config
137 10 ▶ my $key = $_[0] || $self->default_fixer;
Condition66%2/3
$_[0] || $self->default_fixer
l!l&&r!l&&!r
820
MC/DC0%0/2
$_[0] || $self->default_fixer
$_[0] $self->default_fixer
Truth table66%2/3
A || B
A= $_[0]B= $self->default_fixer
ABresult
1X1
011
000
138  
139 10 my $fixers = $self->fixers;
140  
141 10 ▶ $fixers->{$key} || do {
Branch: 1 branch
BranchTrueFalseTotal
$fixers->{$key} or do { if (my $fixes = $self->config->{"fixer"}{$key}) { return $fixers->{$key} = "Catmandu::Fix"->new("fixes", $fixes); } ; return "Catmandu::Fix"->new("fixes", [@_]) } 0 10 10
142 10 ▶ if (my $fixes = $self->config->{fixer}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fixes = $self->config->{"fixer"}{$key}) 3 7 10
143 3 return $fixers->{$key} = Catmandu::Fix->new(fixes => $fixes);
144 }
145 7 return Catmandu::Fix->new(fixes => [@_]);
146 };
147 }
148  
149 29 ▶sub store {
Subroutine store: called CC=1 SCAR=0.0
Pod undocumented
150 29 my $self = shift;
151 29 $self->_named_package('store', $self->store_namespace,
152 $self->default_store, $self->default_store_package,
153 $self->stores, @_);
154 }
155  
156 41 ▶sub importer {
Subroutine importer: called CC=1 SCAR=0.0
Pod undocumented
157 41 my $self = shift;
158 41 $self->_named_package('importer', $self->importer_namespace,
159 $self->default_importer, $self->default_importer_package,
160 undef, @_);
161 }
162  
163 55 ▶sub exporter {
Subroutine exporter: called CC=1 SCAR=0.0
Pod undocumented
164 55 my $self = shift;
165 55 $self->_named_package('exporter', $self->exporter_namespace,
166 $self->default_exporter, $self->default_exporter_package,
167 undef, @_);
168 }
169  
170 2 ▶sub validator {
Subroutine validator: called CC=1 SCAR=0.0
Pod undocumented
171 2 my $self = shift;
172 2 $self->_named_package(
173 'validator', $self->validator_namespace,
174 $self->default_validator, $self->default_validator_package,
175 $self->validators, @_
176 );
177 }
178  
179 127 ▶sub _named_package {
Subroutine _named_package: called CC=16 SCAR=27.9
180 127 my $self = shift;
181 127 my $type = shift;
182 127 my $ns = shift;
183 127 my $default_name = shift;
184 127 my $default_package = shift;
185 127 my $cache = shift;
186 127 my $name = shift;
187 127 ▶ my $key = $name || $default_name;
Condition66%2/3
$name || $default_name
l!l&&r!l&&!r
11980
MC/DC0%0/2
$name || $default_name
$name $default_name
Truth table66%2/3
A || B
A= $nameB= $default_name
ABresult
1X1
011
000
188  
189 127 ▶ return $name if is_instance($name) && index(ref($name), $ns) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if is_instance($name) and index(ref $name, $ns) == 0 1 126 127
Condition66%2/3
is_instance($name) and index(ref $name, $ns) == 0
!ll&&!rl&&r
12601
MC/DC50%1/2
is_instance($name) and index(ref $name, $ns) == 0
is_instance($name) index(ref $name, $ns) == 0
Truth table66%2/3
A and B
A= is_instance($name)B= index(ref $name, $ns) == 0
ABresult
0X0
100
111
190  
191 # return cached instance if no arguments are given
192 126 ▶ if ($cache && !@_ and my $instance = $cache->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache and not @_ and my $instance = $cache->{$key}) 12 114 126
Condition100%6/6
$cache and not @_ and my $instance = $cache->{$key}
!ll&&!rl&&r
1041012
$cache and not @_
!ll&&!rl&&r
95922
MC/DC100%3/3
$cache and not @_ and my $instance = $cache->{$key}
$cache not @_ my $instance = $cache->{$key}
Truth table100%4/4
A and not(B) and C
A= $cacheB= not @_C= my $instance = $cache->{$key}
ABCresult
0XX0
10X0
1100
1111
193 12 return $instance;
194 }
195  
196 114 ▶ if (exists $self->config->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->config->{$type}) 114 0 114
197 114 ▶ if (my $c = $self->config->{$type}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $self->config->{$type}{$key}) 25 89 114
198 25 check_hash_ref($c);
199 25 ▶ check_string(my $package = $c->{package} || $default_package);
Condition33%1/3
$c->{'package'} || $default_package
l!l&&r!l&&!r
2500
MC/DC0%0/2
$c->{'package'} || $default_package
$c->{'package'} $default_package
Truth table33%1/3
A || B
A= $c->{'package'}B= $default_package
ABresult
1X1
011
000
200 25 ▶ my $opts = check_hash_ref($c->{options} || {});
Condition100%2/2
$c->{'options'} || {}
l!l
1015
MC/DC100%1/1
$c->{'options'} || {}
$c->{'options'}
Truth table100%2/2
A
A= $c->{'options'}
Aresult
11
00
201 25 ▶ if (@_ > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 1 24 25
elsif (@_ == 1) { } 6 18 24
202 1 $opts = {%$opts, @_};
203 }
204 elsif (@_ == 1) {
205 6 $opts = {%$opts, %{$_[0]}};
206 }
207 25 my $instance = require_package($package, $ns)->new($opts);
208  
209 # cache this instance if no arguments are given
210 25 ▶ if ($cache && !@_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache and not @_) 10 15 25
Condition100%3/3
$cache and not @_
!ll&&!rl&&r
8710
MC/DC100%2/2
$cache and not @_
$cache not @_
Truth table100%3/3
A and not(B)
A= $cacheB= not @_
ABresult
0X0
100
111
211 10 $cache->{$key} = $instance;
212 }
213  
214 25 return $instance;
215 }
216 }
217  
218 89 ▶ check_string(my $package = $name || $default_package);
Condition33%1/3
$name || $default_package
l!l&&r!l&&!r
8900
MC/DC0%0/2
$name || $default_package
$name $default_package
Truth table33%1/3
A || B
A= $nameB= $default_package
ABresult
1X1
011
000
219 89 require_package($package, $ns)->new(@_);
220 }
221  
222 154 1;
223  
224 __END__
« Catmandu/Fix/sort_field.pm ↑ Summary Catmandu/Paged.pm »