blib/lib/Catmandu/Env.pm

filter: Statementstmt 86.3% 133 / 154 Branchbran 62.0% 36 / 58 Conditioncond 55.5% 20 / 36 MC/DCmcdc 13.0% 3 / 23 Subroutinesub 90.9% 30 / 33 Podpod 0.0% 0 / 9 totaltotal 70.9% 222 / 313 CC 44 SCAR 20.3
CC
44
Cov
76%
_search_up
20.0
exporter
14.5
importer
12.9
CRAP 7.6
« blib/lib/Catmandu/Fix/Has.pm ↑ Summary blib/lib/Catmandu/Paged.pm »
1 package Catmandu::Env;
2  
3 119 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 119 our $VERSION = '1.0201_02';
6  
7 119 ▶use Catmandu::Util qw(require_package use_lib read_yaml read_json :is :check);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 119 ▶use Catmandu::Fix;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 119 ▶use Config::Onion;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 119 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 119 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 119 require Catmandu;
13 119 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 119 with 'Catmandu::Logger';
16  
17 sub _search_up {
18 0 ▶ my $dir = $_[0];
Subroutine _search_up: not called CC=4 SCAR=30.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 coerce => sub {
34 [
35 53 map {File::Spec->canonpath($_)}
36 53 ▶ map {$_ eq ':up' ? _search_up($_) : $_} split /,/,
Branch: 2 branches
BranchTrueFalseTotal
$_ eq ':up' ? : 0 53 53
ref $_[0] ? : 42 11 53
37 join ',',
38 53 ▶ ref $_[0] ? @{$_[0]} : $_[0]
Subroutine __ANON__: called CC=3 SCAR=11.0
39 ];
40 },
41 119 );
42  
43 53 ▶has config => (is => 'rwp', default => sub {+{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
44  
45 53 ▶has stores => (is => 'ro', default => sub {+{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
46 53 ▶has fixers => (is => 'ro', default => sub {+{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
47  
48 53 ▶has default_store => (is => 'ro', default => sub {'default'});
Subroutine __ANON__: called CC=1 SCAR=0.0
49 53 ▶has default_fixer => (is => 'ro', default => sub {'default'});
Subroutine __ANON__: called CC=1 SCAR=0.0
50 53 ▶has default_importer => (is => 'ro', default => sub {'default'});
Subroutine __ANON__: called CC=1 SCAR=0.0
51 53 ▶has default_exporter => (is => 'ro', default => sub {'default'});
Subroutine __ANON__: called CC=1 SCAR=0.0
52 53 ▶has default_importer_package => (is => 'ro', default => sub {'JSON'});
Subroutine __ANON__: called CC=1 SCAR=0.0
53 53 ▶has default_exporter_package => (is => 'ro', default => sub {'JSON'});
Subroutine __ANON__: called CC=1 SCAR=0.0
54  
55 53 ▶has store_namespace => (is => 'ro', default => sub {'Catmandu::Store'});
Subroutine __ANON__: called CC=1 SCAR=0.0
56 53 ▶has fixes_namespace => (is => 'ro', default => sub {'Catmandu::Fix'})
Subroutine __ANON__: called CC=1 SCAR=0.0
57 119 ; # TODO unused
58  
59 53 ▶has importer_namespace => (is => 'ro', default => sub {'Catmandu::Importer'});
Subroutine __ANON__: called CC=1 SCAR=0.0
60 53 ▶has exporter_namespace => (is => 'ro', default => sub {'Catmandu::Exporter'});
Subroutine __ANON__: called CC=1 SCAR=0.0
61  
62 sub default_config_extensions {
63 0 ▶ [qw(yaml yml json pl)];
Subroutine default_config_extensions: not called CC=1 SCAR=6.9
Pod undocumented
64 }
65  
66 sub BUILD {
67 53 ▶ my ($self) = @_;
Subroutine BUILD: called CC=8 SCAR=21.3
Pod undocumented
68  
69 53 my @config_dirs = @{$self->load_paths};
70 53 my @lib_dirs;
71  
72 53 for my $dir (@config_dirs) {
73 53 ▶ if (!-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 53 53
74 0 Catmandu::Error->throw("load path $dir doesn't exist");
75 }
76  
77 53 my $lib_dir = File::Spec->catdir($dir, 'lib');
78  
79 53 ▶ if (-d $lib_dir && -r $lib_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $lib_dir and -r $lib_dir) 53 0 53
Condition33%1/3
-d $lib_dir and -r $lib_dir
!ll&&!rl&&r
0053
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 53 push @lib_dirs, $lib_dir;
81 }
82 }
83  
84 53 ▶ if (@config_dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@config_dirs) 53 0 53
85 my @globs = map {
86 53 my $dir = $_;
87 53 map {File::Spec->catfile($dir, "catmandu*.$_")}
88 qw(yaml yml json pl)
89 } reverse @config_dirs;
90  
91 53 my $config = Config::Onion->new(prefix_key => '_prefix');
92 53 $config->load_glob(@globs);
93  
94 53 ▶ if ($self->log->is_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->log->is_debug) 12 41 53
95 119 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 12 $self->log->debug(Dumper($config->get));
97 }
98 53 $self->_set_config($config->get);
99 }
100  
101 53 ▶ if (@lib_dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lib_dirs) 53 0 53
102 53 lib->import(@lib_dirs);
103 }
104 }
105  
106 sub load_path {
107 2 ▶ $_[0]->load_paths->[0];
Subroutine load_path: called CC=1 SCAR=0.0
Pod undocumented
108 }
109  
110 sub roots {
111 1 ▶ goto &load_paths;
Subroutine roots: called CC=1 SCAR=0.0
Pod undocumented
112 }
113  
114 sub root {
115 2 ▶ goto &load_path;
Subroutine root: called CC=1 SCAR=0.0
Pod undocumented
116 }
117  
118 sub store {
119 16 ▶ my $self = shift;
Subroutine store: called CC=8 SCAR=21.1
Pod undocumented
120 16 my $name = shift;
121  
122 16 my $stores = $self->stores;
123  
124 16 ▶ my $key = $name || $self->default_store;
Condition66%2/3
$name || $self->default_store
l!l&&r!l&&!r
1420
MC/DC0%0/2
$name || $self->default_store
$name $self->default_store
Truth table66%2/3
A || B
A= $nameB= $self->default_store
ABresult
1X1
011
000
125  
126 16 ▶ $stores->{$key} || do {
Branch: 1 branch
BranchTrueFalseTotal
$stores->{$key} or do { my $ns = $self->store_namespace; if (my $c = $self->config->{"store"}{$key}) { check_hash_ref($c); check_string(my $package = $c->{"package"}); my $opts = $c->{"options"} || {}; if (@_ > 1) { $opts = {%$opts, @_}; } elsif (@_ == 1) { $opts = {%$opts, %{$_[0];}}; } ; return $stores->{$key} = require_package($package, $ns)->new($opts); } ; if ($name) { return require_package($name, $ns)->new(@_); } ; "Catmandu::BadArg"->throw('unknown store ' . $self->default_store) } 10 6 16
127 10 my $ns = $self->store_namespace;
128 10 ▶ if (my $c = $self->config->{store}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $self->config->{"store"}{$key}) 9 1 10
129 9 check_hash_ref($c);
130 9 check_string(my $package = $c->{package});
131 9 ▶ my $opts = $c->{options} || {};
Condition100%2/2
$c->{'options'} || {}
l!l
45
MC/DC100%1/1
$c->{'options'} || {}
$c->{'options'}
Truth table100%2/2
A
A= $c->{'options'}
Aresult
11
00
132 9 ▶ if (@_ > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 0 9 9
elsif (@_ == 1) { } 3 6 9
133 0 $opts = {%$opts, @_};
134 }
135 elsif (@_ == 1) {
136 3 $opts = {%$opts, %{$_[0]}};
137 }
138 9 return $stores->{$key}
139 = require_package($package, $ns)->new($opts);
140 }
141 1 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 1 0 1
142 1 return require_package($name, $ns)->new(@_);
143 }
144 0 Catmandu::BadArg->throw("unknown store " . $self->default_store);
145 }
146 }
147  
148 sub fixer {
149 18 ▶ my $self = shift;
Subroutine fixer: called CC=7 SCAR=20.3
Pod undocumented
150  
151 # it's already a fixer
152 18 ▶ if (is_instance($_[0], 'Catmandu::Fix')) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_instance($_[0], "Catmandu::Fix")) 0 18 18
153 0 return $_[0];
154 }
155  
156 # an array ref of fixes
157 18 ▶ if (is_array_ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($_[0])) 9 9 18
158 9 return Catmandu::Fix->new(fixes => $_[0]);
159 }
160  
161 # a single fix instance
162 9 ▶ if (is_able($_[0], 'fix')) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_able($_[0], "fix")) 0 9 9
163 0 return Catmandu::Fix->new(fixes => [$_[0]]);
164 }
165  
166 # try to laod from config
167 9 ▶ my $key = $_[0] || $self->default_fixer;
Condition66%2/3
$_[0] || $self->default_fixer
l!l&&r!l&&!r
720
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
168  
169 9 my $fixers = $self->fixers;
170  
171 9 ▶ $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", [@_]) } 9 0 9
172 9 ▶ if (my $fixes = $self->config->{fixer}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fixes = $self->config->{"fixer"}{$key}) 3 6 9
173 3 return $fixers->{$key} = Catmandu::Fix->new(fixes => $fixes);
174 }
175 6 return Catmandu::Fix->new(fixes => [@_]);
176 }
177 }
178  
179 sub importer {
180 26 ▶ my $self = shift;
Subroutine importer: called CC=9 SCAR=25.6
Pod undocumented
181 26 my $name = shift;
182 26 my $ns = $self->importer_namespace;
183 26 ▶ if (exists $self->config->{importer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->config->{"importer"}) 26 0 26
184 26 ▶ if (my $c
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $self->config->{"importer"}{$name or $self->default_importer}) 4 22 26
Condition66%2/3
$name || $self->default_importer
l!l&&r!l&&!r
2420
MC/DC0%0/2
$name || $self->default_importer
$name $self->default_importer
Truth table66%2/3
A || B
A= $nameB= $self->default_importer
ABresult
1X1
011
000
185 = $self->config->{importer}{$name || $self->default_importer})
186 {
187 4 check_hash_ref($c);
188 4 ▶ my $package = $c->{package} || $self->default_importer_package;
Condition33%1/3
$c->{'package'} || $self->default_importer_package
l!l&&r!l&&!r
400
MC/DC0%0/2
$c->{'package'} || $self->default_importer_package
$c->{'package'} $self->default_importer_package
Truth table33%1/3
A || B
A= $c->{'package'}B= $self->default_importer_package
ABresult
1X1
011
000
189 4 ▶ my $opts = $c->{options} || {};
Condition50%1/2
$c->{'options'} || {}
l!l
04
MC/DC0%0/1
$c->{'options'} || {}
$c->{'options'}
Truth table50%1/2
A
A= $c->{'options'}
Aresult
11
00
190 4 ▶ if (@_ > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 0 4 4
elsif (@_ == 1) { } 0 4 4
191 0 $opts = {%$opts, @_};
192 }
193 elsif (@_ == 1) {
194 0 $opts = {%$opts, %{$_[0]}};
195 }
196 4 return require_package($package, $ns)->new($opts);
197 }
198 }
199 22 ▶ require_package($name || $self->default_importer_package, $ns)->new(@_);
Condition33%1/3
$name || $self->default_importer_package
l!l&&r!l&&!r
2200
MC/DC0%0/2
$name || $self->default_importer_package
$name $self->default_importer_package
Truth table33%1/3
A || B
A= $nameB= $self->default_importer_package
ABresult
1X1
011
000
200 }
201  
202 sub exporter {
203 40 ▶ my $self = shift;
Subroutine exporter: called CC=11 SCAR=26.8
Pod undocumented
204 40 my $name = shift;
205  
206 40 ▶ return $name
Branch: 1 branch
BranchTrueFalseTotal
if is_invocant($name) and ref($name) =~ /^Catmandu::Exporter/ 1 39 40
Condition100%3/3
is_invocant($name) and ref($name) =~ /^Catmandu::Exporter/
!ll&&!rl&&r
16231
MC/DC100%2/2
is_invocant($name) and ref($name) =~ /^Catmandu::Exporter/
is_invocant($name) ref($name) =~ /^Catmandu::Exporter/
Truth table100%3/3
A and B
A= is_invocant($name)B= ref($name) =~ /^Catmandu::Exporter/
ABresult
0X0
100
111
207 if (is_invocant($name) && ref($name) =~ /^Catmandu::Exporter/);
208  
209 39 my $ns = $self->exporter_namespace;
210 39 ▶ if (exists $self->config->{exporter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->config->{"exporter"}) 39 0 39
211 39 ▶ if (my $c
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $self->config->{"exporter"}{$name or $self->default_exporter}) 4 35 39
Condition66%2/3
$name || $self->default_exporter
l!l&&r!l&&!r
3720
MC/DC0%0/2
$name || $self->default_exporter
$name $self->default_exporter
Truth table66%2/3
A || B
A= $nameB= $self->default_exporter
ABresult
1X1
011
000
212 = $self->config->{exporter}{$name || $self->default_exporter})
213 {
214 4 check_hash_ref($c);
215 4 ▶ my $package = $c->{package} || $self->default_exporter_package;
Condition33%1/3
$c->{'package'} || $self->default_exporter_package
l!l&&r!l&&!r
400
MC/DC0%0/2
$c->{'package'} || $self->default_exporter_package
$c->{'package'} $self->default_exporter_package
Truth table33%1/3
A || B
A= $c->{'package'}B= $self->default_exporter_package
ABresult
1X1
011
000
216 4 ▶ my $opts = $c->{options} || {};
Condition50%1/2
$c->{'options'} || {}
l!l
04
MC/DC0%0/1
$c->{'options'} || {}
$c->{'options'}
Truth table50%1/2
A
A= $c->{'options'}
Aresult
11
00
217 4 ▶ if (@_ > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 0 4 4
elsif (@_ == 1) { } 0 4 4
218 0 $opts = {%$opts, @_};
219 }
220 elsif (@_ == 1) {
221 0 $opts = {%$opts, %{$_[0]}};
222 }
223 4 return require_package($package, $ns)->new($opts);
224 }
225 }
226 35 ▶ require_package($name || $self->default_exporter_package, $ns)->new(@_);
Condition33%1/3
$name || $self->default_exporter_package
l!l&&r!l&&!r
3500
MC/DC0%0/2
$name || $self->default_exporter_package
$name $self->default_exporter_package
Truth table33%1/3
A || B
A= $nameB= $self->default_exporter_package
ABresult
1X1
011
000
227 }
228  
229 119 1;
230  
231 __END__
232  
233 =pod
234  
235 =head1 NAME
236  
237 Catmandu::Env - A catmandu configuration file loader
238  
239 =head1 SYNOPSIS
240  
241 use Catmandu::Env;
242  
243 my $env = Catmandu::Env->new(load_paths => [ '/etc/catmandu '] );
244 my $env = Catmandu::Env->new(load_paths => [ ':up'] );
245  
246 my $store = $env->store('mongodb');
247 my $importer = $env->importer('loc');
248 my $exporter = $env->exporter('europeana');
249 my $fixer = $env->fixer('my_fixes');
250 my $conf = $env->config;
251  
252 =head1 DESCRIPTION
253  
254 This class loads the catmandu.*.pl, catmandu.*.json, catmandu.*.yml and catmandu.*.yaml file from
255 all provided load_paths. Programmers are advised *not* to use this class directly
256 but use the equivalent functionality provided in the Catmandu package:
257  
258 Catmandu->load('/etc/catmandu');
259 Catmandu->load(':up');
260  
261 my $store = Catmandu->store('mongodb');
262 my $importer = Catmandu->importer('loc');
263 my $exporter = Catmandu->exporter('europeana');
264 my $fixer = Catmandu->fixer('my_fixes');
265 my $conf = Catmandu->config;
266  
267 =head1 SEE ALSO
268  
269 L<Catmandu>
270  
271 =cut
« blib/lib/Catmandu/Fix/Has.pm ↑ Summary blib/lib/Catmandu/Paged.pm »