lib/Badger/Config/Filesystem.pm

filter: Statementstmt 3.0% 8 / 259 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 103 MC/DCmcdc 0.0% 0 / 59 Subroutinesub 3.3% 1 / 30 Podpod 55.1% 16 / 29 totaltotal 4.4% 25 / 560 CC 85 SCAR 42.7
CC
85
Cov
2%
config_tree
272.0
init_filesystem
110.0
nest_tree_binder
90.0
CRAP 71.6
« blib/lib/Badger/Config/Filesystem.pm ↑ Summary blib/lib/Badger/Date.pm »
1 package Badger::Config::Filesystem;
2  
3 use Badger::Class
4 2 version => 0.01,
5 debug => 0,
6 import => 'class',
7 base => 'Badger::Config Badger::Workplace',
8 utils => 'split_to_list extend VFS join_uri resolve_uri',
9 accessors => 'root filespec encoding codecs extensions quiet',
10 words => 'ENCODING CODECS',
11 constants => 'DOT NONE TRUE FALSE YAML JSON UTF8 ARRAY HASH SCALAR',
12 constant => {
13 ABSOLUTE => 'absolute',
14 RELATIVE => 'relative',
15 # extra debugging flags
16 DEBUG_FETCH => 0,
17 },
18 messages => {
19 load_fail => 'Failed to load data from %s: %s',
20 no_config_file => 'Missing configuration file: %s',
21 merge_mismatch => 'Cannot merge items for %s: %s and %s',
22 2 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 our $EXTENSIONS = [YAML, JSON];
25 2 our $ENCODING = UTF8;
26 2 our $CODECS = { };
27 2 our $STAT_TTL = 0;
28  
29  
30 #-----------------------------------------------------------------------------
31 # Initialisation methods called at object creation time
32 #-----------------------------------------------------------------------------
33  
34 sub init {
35 0 ▶ my ($self, $config) = @_;
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
36  
37 # First call Badger::Config base class method to handle any 'items'
38 # definitions and other general initialisation
39 0 $self->init_config($config);
40  
41 # Then our own custom init method
42 0 $self->init_filesystem($config);
43 }
44  
45 sub init_filesystem {
46 0 ▶ my ($self, $config) = @_;
Subroutine init_filesystem: not called CC=10 SCAR=47.0
Pod documented
47 0 my $class = $self->class;
48  
49 0 $self->debug_data( filesystem_config => $config ) if DEBUG;
50  
51 # The filespec can be specified as a hash of options for file objects
52 # created by the top-level directory object. If unspecified, we construct
53 # it using any encoding option, or falling back on a $ENCODING package
54 # variable. This is then passed to init_workplace().
55 my $encoding = $config->{ encoding }
56 0 ▶ || $class->any_var(ENCODING);
Condition0%0/3
$config->{'encoding'} || $class->any_var('ENCODING')
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{'encoding'} || $class->any_var('ENCODING')
$config->{'encoding'} $class->any_var('ENCODING')
Truth table0%0/3
A || B
A= $config->{'encoding'}B= $class->any_var('ENCODING')
ABresult
1X1
011
000
57  
58 my $filespec = $config->{ filespec } ||= {
59 0 ▶ encoding => $encoding
Condition0%0/2
$config->{"filespec"} ||= {"encoding", $encoding}
l!l
00
MC/DC0%0/1
$config->{"filespec"} ||= {"encoding", $encoding}
$config->{"filespec"}
Truth table0%0/2
A
A= $config->{"filespec"}
Aresult
11
00
60 };
61  
62 # now initialise the workplace root directory
63 0 $self->init_workplace($config);
64  
65 # Configuration files can be in any data format which Badger::Codecs can
66 # handle (e.g. JSON, YAML, etc). The 'extensions' configuration option
67 # and any $EXTENSIONS defined in package variables (for the current class
68 # and all base classes) will be tried in order
69 my $exts = $class->list_vars(
70 EXTENSIONS => $config->{ extensions }
71 0 );
72 $exts = [
73 0 map { @{ split_to_list($_) } }
74 @$exts
75 ];
76  
77 # Construct a regex to match any of the above
78 0 my $qm_ext = join('|', map { quotemeta $_ } @$exts);
79 0 my $ext_re = qr/.($qm_ext)$/i;
80  
81 0 $self->debug(
82 "extensions: ", $self->dump_data($exts), "\n",
83 "extension regex: $ext_re"
84 ) if DEBUG;
85  
86 # The 'codecs' option can provide additional mapping from filename extension
87 # to codec for any that Badger::Codecs can't grok automagically
88 my $codecs = $class->hash_vars(
89 CODECS => $config->{ codecs }
90 0 );
91  
92 0 ▶ my $data = $config->{ data } || { };
Condition0%0/2
$config->{'data'} || {}
l!l
00
MC/DC0%0/1
$config->{'data'} || {}
$config->{'data'}
Truth table0%0/2
A
A= $config->{'data'}
Aresult
11
00
93  
94 0 $self->{ data } = $data;
95 0 $self->{ extensions } = $exts;
96 0 $self->{ match_ext } = $ext_re;
97 0 $self->{ codecs } = $codecs;
98 0 $self->{ encoding } = $encoding;
99 0 $self->{ filespec } = $filespec;
100 0 ▶ $self->{ quiet } = $config->{ quiet } || FALSE;
Condition0%0/2
$config->{'quiet'} || 0
l!l
00
MC/DC0%0/1
$config->{'quiet'} || 0
$config->{'quiet'}
Truth table0%0/2
A
A= $config->{'quiet'}
Aresult
11
00
101 0 ▶ $self->{ dir_tree } = $config->{ dir_tree } // TRUE;
Condition0%0/2
$config->{"dir_tree"} // 1
l!l
00
MC/DC0%0/1
$config->{"dir_tree"} // 1
$config->{"dir_tree"}
Truth table0%0/2
A
A= $config->{"dir_tree"}
Aresult
11
00
102 0 ▶ $self->{ stat_ttl } = $config->{ stat_ttl } // $data->{ stat_ttl } // $STAT_TTL;
Condition0%0/6
($config->{"stat_ttl"} // $data->{"stat_ttl"}) // $Badger::Config::Filesystem::STAT_TTL
l!l&&r!l&&!r
000
$config->{"stat_ttl"} // $data->{"stat_ttl"}
l!l&&r!l&&!r
000
MC/DC0%0/3
($config->{"stat_ttl"} // $data->{"stat_ttl"}) // $Badger::Config::Filesystem::STAT_TTL
$config->{"stat_ttl"} $data->{"stat_ttl"} $Badger::Config::Filesystem::STAT_TTL
Truth table0%0/4
A // B // C
A= $config->{"stat_ttl"}B= $data->{"stat_ttl"}C= $Badger::Config::Filesystem::STAT_TTL
ABCresult
1XX1
01X1
0011
0000
103 0 $self->{ not_found } = { };
104  
105 # Add any item schemas
106 $self->items( $config->{ schemas } )
107 0 ▶ if $config->{ schemas };
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"schemas"} 0 0 0
108  
109 # Configuration file allows further data items (and schemas) to be defined
110 $self->init_file( $config->{ file } )
111 0 ▶ if $config->{ file };
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"file"} 0 0 0
112  
113 0 return $self;
114 }
115  
116 sub init_file {
117 0 ▶ my ($self, $file) = @_;
Subroutine init_file: not called CC=3 SCAR=24.8
Pod undocumented
118 0 my $data = $self->get($file);
119  
120 0 ▶ if ($data) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data) { } 0 0 0
elsif (not $self->{'quiet'}) { } 0 0 0
121 # must copy data so as not to damage cached version
122 0 $data = { %$data };
123  
124 0 $self->debug(
125 "config file data from $file: ",
126 $self->dump_data($data)
127 ) if DEBUG;
128  
129 # file can contain 'items' or 'schemas' (I don't love this, but it'll do for now)
130 $self->items(
131 delete $data->{ items },
132 delete $data->{ schemas }
133 0 );
134  
135 # anything else is config data
136 0 extend($self->{ data }, $data);
137  
138 0 $self->debug("merged data: ", $self->dump_data($self->{ data })) if DEBUG;
139 }
140 elsif (! $self->{ quiet }) {
141 0 return $self->no_config_file($file);
142 }
143  
144 0 return $self;
145 }
146  
147 sub no_config_file {
148 0 ▶ shift->warn_msg( no_config_file => @_ );
Subroutine no_config_file: not called CC=1 SCAR=6.9
Pod undocumented
149 }
150  
151  
152 #-----------------------------------------------------------------------------
153 # Redefine head() method in Badger::Config to hook into fetch() to load data
154 #-----------------------------------------------------------------------------
155  
156 sub head {
157 0 ▶ my ($self, $name) = @_;
Subroutine head: not called CC=2 SCAR=17.9
Pod documented
158 0 ▶ return $self->{ data }->{ $name }
Condition0%0/3
$self->{"data"}{$name} // $self->fetch($name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"data"}{$name} // $self->fetch($name)
$self->{"data"}{$name} $self->fetch($name)
Truth table0%0/3
A // B
A= $self->{"data"}{$name}B= $self->fetch($name)
ABresult
1X1
011
000
159 // $self->fetch($name);
160 }
161  
162 sub tail {
163 0 ▶ my ($self, $name, $data) = @_;
Subroutine tail: not called CC=1 SCAR=6.9
Pod documented
164 0 return $data;
165 }
166  
167  
168 #-----------------------------------------------------------------------------
169 # Filesystem-specific fetch methods
170 #-----------------------------------------------------------------------------
171  
172 sub fetch {
173 0 ▶ my ($self, $uri) = @_;
Subroutine fetch: not called CC=7 SCAR=40.3
Pod documented
174  
175 0 ▶ return if $self->previously_not_found($uri);
Branch: 1 branch
BranchTrueFalseTotal
if $self->previously_not_found($uri) 0 0 0
176  
177 0 $self->debug("fetch($uri)") if DEBUG or DEBUG_FETCH;
178  
179 0 my $file = $self->config_file($uri);
180 0 my $dir = $self->dir($uri);
181 0 ▶ my $fok = $file && $file->exists;
Condition0%0/3
$file && $file->exists
!ll&&!rl&&r
000
MC/DC0%0/2
$file && $file->exists
$file $file->exists
Truth table0%0/3
A && B
A= $fileB= $file->exists
ABresult
0X0
100
111
182 0 ▶ my $dok = $dir && $dir->exists;
Condition0%0/3
$dir && $dir->exists
!ll&&!rl&&r
000
MC/DC0%0/2
$dir && $dir->exists
$dir $dir->exists
Truth table0%0/3
A && B
A= $dirB= $dir->exists
ABresult
0X0
100
111
183  
184 0 ▶ if ($dok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dok) 0 0 0
185 0 $self->debug("Found directory for $uri, loading tree") if DEBUG or DEBUG_FETCH;
186 0 return $self->config_tree($uri, $file, $dir);
187 }
188  
189 0 ▶ if ($fok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fok) 0 0 0
190 0 $self->debug("Found file for $uri, loading file data => ", $file->absolute) if DEBUG or DEBUG_FETCH;
191 0 my $data = $file->try->data;
192 0 ▶ return $self->error_msg( load_fail => $file => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
193 0 return $self->tail(
194 $uri, $data,
195 $self->item_schema_from_data(
196 $uri, $data
197 )
198 );
199 }
200  
201 0 $self->debug("No file or directory found for $uri") if DEBUG or DEBUG_FETCH;
202 0 $self->{ not_found }->{ $uri } = time();
203 0 return undef;
204 }
205  
206 sub previously_not_found {
207 0 ▶ my ($self, $uri) = @_;
Subroutine previously_not_found: not called CC=4 SCAR=30.0
Pod undocumented
208 0 ▶ my $sttl = $self->{ stat_ttl } || return 0;
Condition0%0/2
$self->{'stat_ttl'} || (return 0)
l!l
00
MC/DC0%0/1
$self->{'stat_ttl'} || (return 0)
$self->{'stat_ttl'}
Truth table0%0/2
A
A= $self->{'stat_ttl'}
Aresult
11
00
209 0 ▶ my $when = $self->{ not_found }->{ $uri } || return 0;
Condition0%0/2
$self->{'not_found'}{$uri} || (return 0)
l!l
00
MC/DC0%0/1
$self->{'not_found'}{$uri} || (return 0)
$self->{'not_found'}{$uri}
Truth table0%0/2
A
A= $self->{'not_found'}{$uri}
Aresult
11
00
210 # we maintain the "not_found" status until stat_ttl seconds have elapsed
211 0 ▶ if (time < $when + $sttl) {
Branch: 1 branch
BranchTrueFalseTotal
if (time < $when + $sttl) { } 0 0 0
212 0 $self->debug("$uri NOT FOUND at $when") if DEBUG; # or DEBUG_FETCH;
213 0 return 1
214 }
215 else {
216 0 return 0;
217 }
218 }
219  
220 #-----------------------------------------------------------------------------
221 # Tree walking
222 #-----------------------------------------------------------------------------
223  
224 sub config_tree {
225 0 ▶ my $self = shift;
Subroutine config_tree: not called CC=16 SCAR=56.1
Pod documented
226 0 my $name = shift;
227 0 ▶ my $file = shift || $self->config_file($name);
Condition0%0/3
shift() || $self->config_file($name)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->config_file($name)
shift() $self->config_file($name)
Truth table0%0/3
A || B
A= shift()B= $self->config_file($name)
ABresult
1X1
011
000
228 0 ▶ my $dir = shift || $self->dir($name);
Condition0%0/3
shift() || $self->dir($name)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->dir($name)
shift() $self->dir($name)
Truth table0%0/3
A || B
A= shift()B= $self->dir($name)
ABresult
1X1
011
000
229 0 my $do_tree = $self->{ dir_tree };
230 0 my $data = undef; #{ };
231 0 my ($file_data, $binder, $more);
232  
233 0 ▶ unless ($file && $file->exists || $dir->exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file and $file->exists or $dir->exists) 0 0 0
Condition0%0/6
$file and $file->exists or $dir->exists
l!l&&r!l&&!r
000
$file and $file->exists
!ll&&!rl&&r
000
MC/DC0%0/3
$file and $file->exists or $dir->exists
$file $file->exists $dir->exists
Truth table0%0/5
A and B or C
A= $fileB= $file->existsC= $dir->exists
ABCresult
11X1
0X11
1011
0X00
1000
234 0 return $self->decline_msg( not_found => 'file or directory' => $name );
235 }
236  
237 # start by looking for a data file
238 0 ▶ if ($file && $file->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file and $file->exists) 0 0 0
Condition0%0/3
$file and $file->exists
!ll&&!rl&&r
000
MC/DC0%0/2
$file and $file->exists
$file $file->exists
Truth table0%0/3
A and B
A= $fileB= $file->exists
ABresult
0X0
100
111
239 0 $file_data = $file->try->data;
240 0 ▶ return $self->error_msg( load_fail => $file => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
241 0 $self->debug("Read metadata from file '$file':", $self->dump_data($file_data)) if DEBUG;
242 }
243  
244 # fetch a schema for this data item constructed from the default schema
245 # specification, any named schema for this item, any arguments, then any
246 # local schema defined in the data file
247 0 my $schema = $self->item_schema_from_data($name, $file_data);
248  
249 0 $self->debug(
250 "combined schema for $name: ",
251 $self->dump_data($schema)
252 ) if DEBUG;
253  
254 0 ▶ if ($more = $schema->{ tree_type }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($more = $schema->{"tree_type"}) 0 0 0
255 0 $self->debug("schema.tree_type: $more") if DEBUG;
256 0 ▶ if ($more eq NONE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($more eq 'none') { } 0 0 0
elsif ($binder = $self->tree_binder($more)) { } 0 0 0
257 0 $self->debug("schema rules indicate we shouldn't descend into the tree") if DEBUG;
258 0 $do_tree = FALSE;
259 }
260 elsif ($binder = $self->tree_binder($more)) {
261 0 $self->debug("schema rules indicate a $more tree tree") if DEBUG;
262 0 $do_tree = TRUE;
263 }
264 else {
265 0 return $self->error_msg( invalid => tree_type => $more );
266 }
267 }
268  
269 0 ▶ if ($do_tree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_tree) { } 0 0 0
270 # merge file data using binder
271 0 ▶ $data ||= { };
Condition0%0/2
$data ||= {}
l!l
00
MC/DC0%0/1
$data ||= {}
$data
Truth table0%0/2
A
A= $data
Aresult
11
00
272 0 ▶ $binder ||= $self->tree_binder('nest');
Condition0%0/3
$binder ||= $self->tree_binder("nest")
l!l&&r!l&&!r
000
MC/DC0%0/2
$binder ||= $self->tree_binder("nest")
$binder $self->tree_binder("nest")
Truth table0%0/3
A ||= B
A= $binderB= $self->tree_binder("nest")
ABresult
1X1
011
000
273 0 $binder->($self, $data, [ ], $file_data, $schema);
274  
275 0 ▶ if ($dir->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir->exists) 0 0 0
276 # create a virtual file system rooted on the metadata directory
277 # so that all file paths are resolved relative to it
278 0 my $vfs = VFS->new( root => $dir );
279 0 $self->debug("Reading metadata from dir: ", $dir->name) if DEBUG;
280 0 $self->scan_config_dir($vfs->root, $data, [ ], $schema, $binder);
281 }
282 }
283 else {
284 0 $data = $file_data;
285 }
286  
287 0 $self->debug("$name config: ", $self->dump_data($data)) if DEBUG;
288  
289 0 return $self->tail(
290 $name, $data, $schema
291 );
292 }
293  
294 sub scan_config_dir {
295 0 ▶ my ($self, $dir, $data, $path, $schema, $binder) = @_;
Subroutine scan_config_dir: not called CC=7 SCAR=40.3
Pod documented
296 0 my $files = $dir->files;
297 0 my $dirs = $dir->dirs;
298 0 ▶ $path ||= [ ];
Condition0%0/2
$path ||= []
l!l
00
MC/DC0%0/1
$path ||= []
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
299 0 ▶ $binder ||= $self->tree_binder;
Condition0%0/3
$binder ||= $self->tree_binder
l!l&&r!l&&!r
000
MC/DC0%0/2
$binder ||= $self->tree_binder
$binder $self->tree_binder
Truth table0%0/3
A ||= B
A= $binderB= $self->tree_binder
ABresult
1X1
011
000
300  
301 0 $self->debug(
302 "scan_config_dir($dir, $data, ",
303 $self->dump_data_inline($path), ", ",
304 $self->dump_data_inline($schema), ", ",
305 $binder, ")"
306 ) if DEBUG;
307  
308 0 ▶ $data ||= { };
Condition0%0/2
$data ||= {}
l!l
00
MC/DC0%0/1
$data ||= {}
$data
Truth table0%0/2
A
A= $data
Aresult
11
00
309  
310 0 foreach my $file (@$files) {
311 0 ▶ next unless $file->name =~ $self->{ match_ext };
Branch: 1 branch
BranchTrueFalseTotal
unless $file->name =~ /$self->{"match_ext"}/ 0 0 0
312 0 $self->debug("found file: ", $file->name, ' at ', $file->path) if DEBUG;
313 0 $self->scan_config_file($file, $data, $path, $schema, $binder);
314 }
315 0 foreach my $subdir (@$dirs) {
316 0 $self->debug("found dir: ", $subdir->name, ' at ', $subdir->path) if DEBUG;
317 # if we don't have a data binder then we need to create a sub-hash
318 0 my $name = $subdir->name;
319 #my $more = $binder ? $data : ($data->{ $name } = { });
320 0 push(@$path, $name);
321 #$self->scan_config_dir($subdir, $more, $path, $schema, $binder);
322 0 $self->scan_config_dir($subdir, $data, $path, $schema, $binder);
323 0 pop(@$path);
324 }
325 }
326  
327 sub scan_config_file {
328 0 ▶ my ($self, $file, $data, $path, $schema, $binder) = @_;
Subroutine scan_config_file: not called CC=4 SCAR=30.0
Pod documented
329 0 my $base = $file->basename;
330 0 my $ext = $file->extension;
331  
332 0 $self->debug(
333 "scan_config_file($file, $data, ",
334 $self->dump_data_inline($path), ", ",
335 $self->dump_data_inline($schema), ", ",
336 $binder, ")"
337 ) if DEBUG;
338  
339 # set the codec to match the extension (or any additional mapping)
340 # and set the data encoding
341 0 $file->codec( $self->codec($ext) );
342 0 $file->encoding( $self->{ encoding } );
343  
344 0 my $meta = $file->try->data;
345 0 ▶ return $self->error_msg( load_fail => $file => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
346  
347 0 $self->debug("Metadata: ", $self->dump_data($meta)) if DEBUG;
348  
349 0 ▶ if ($binder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binder) { } 0 0 0
350 0 ▶ $path ||= [ ];
Condition0%0/2
$path ||= []
l!l
00
MC/DC0%0/1
$path ||= []
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
351 0 push(@$path, $base);
352 0 $binder->($self, $data, $path, $meta, $schema);
353 0 pop(@$path);
354 }
355 else {
356 0 $base =~ s[^/][];
357 0 $data->{ $base } = $meta;
358 }
359 }
360  
361  
362 #-----------------------------------------------------------------------------
363 # Binder methods for combining multiple data sources (e.g. files in sub-
364 # directories) into a single tree.
365 #-----------------------------------------------------------------------------
366  
367 sub tree_binder {
368 0 ▶ my $self = shift;
Subroutine tree_binder: not called CC=4 SCAR=30.0
Pod documented
369 my $name = shift
370 || $self->{ tree_type }
371 0 ▶ || return $self->error_msg( missing => 'tree_type' );
Condition0%0/5
shift() || $self->{'tree_type'} || (return $self->error_msg('missing', 'tree_type'))
l!l
00
shift() or $self->{"tree_type"}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->{'tree_type'} || (return $self->error_msg('missing', 'tree_type'))
shift() $self->{"tree_type"}
Truth table0%0/3
A or B
A= shift()B= $self->{"tree_type"}
ABresult
1X1
011
000
372  
373 0 ▶ return $self->can("${name}_tree_binder")
Condition0%0/2
$self->can("${name}_tree_binder") || (return $self->decline_msg('invalid', 'binder', $name))
l!l
00
MC/DC0%0/1
$self->can("${name}_tree_binder") || (return $self->decline_msg('invalid', 'binder', $name))
$self->can("${name}_tree_binder")
Truth table0%0/2
A
A= $self->can("${name}_tree_binder")
Aresult
11
00
374 || return $self->decline_msg( invalid => binder => $name );
375 }
376  
377 sub nest_tree_binder {
378 0 ▶ my ($self, $parent, $path, $child, $schema) = @_;
Subroutine nest_tree_binder: not called CC=9 SCAR=45.0
Pod documented
379 0 my $data = $parent;
380 0 my $uri = join('/', @$path);
381 0 my @bits = @$path;
382 0 my $last = pop @bits;
383  
384 0 $self->debug("Adding [$uri] as ", $self->dump_data($child))if DEBUG;
385  
386 0 foreach my $key (@bits) {
387 0 ▶ $data = $data->{ $key } ||= { };
Condition0%0/2
$data->{$key} ||= {}
l!l
00
MC/DC0%0/1
$data->{$key} ||= {}
$data->{$key}
Truth table0%0/2
A
A= $data->{$key}
Aresult
11
00
388 }
389  
390 0 ▶ if ($last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last) { } 0 0 0
391 0 my $tail = $data->{ $last };
392  
393 0 ▶ if ($tail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail) { } 0 0 0
394 0 ▶ my $tref = ref $tail || SCALAR;
Condition0%0/2
ref $tail || 'SCALAR'
l!l
00
MC/DC0%0/1
ref $tail || 'SCALAR'
ref $tail
Truth table0%0/2
A
A= ref $tail
Aresult
11
00
395 0 ▶ my $cref = ref $child || SCALAR;
Condition0%0/2
ref $child || 'SCALAR'
l!l
00
MC/DC0%0/1
ref $child || 'SCALAR'
ref $child
Truth table0%0/2
A
A= ref $child
Aresult
11
00
396  
397 0 ▶ if ($tref eq HASH && $cref eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tref eq 'HASH' and $cref eq 'HASH') { } 0 0 0
Condition0%0/3
$tref eq "HASH" and $cref eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$tref eq "HASH" and $cref eq "HASH"
$tref eq "HASH" $cref eq "HASH"
Truth table0%0/3
A and B
A= $tref eq "HASH"B= $cref eq "HASH"
ABresult
0X0
100
111
398 0 $self->debug("Merging into $last") if DEBUG;
399 0 @$tail{ keys %$child } = values %$tail;
400 }
401 else {
402 0 return $self->error_msg( merge_mismatch => $uri, $tref, $cref );
403 }
404 }
405 else {
406 0 $self->debug("setting $last in data to $child") if DEBUG;
407 0 $data->{ $last } = $child;
408 }
409 }
410 else {
411 0 $self->debug("No path, simple merge of child into parent") if DEBUG;
412 0 @$data{ keys %$child } = values %$child;
413 }
414  
415 0 $self->debug("New parent: ", $self->dump_data($parent)) if DEBUG;
416 }
417  
418 sub flat_tree_binder {
419 0 ▶ my ($self, $parent, $path, $child, $schema) = @_;
Subroutine flat_tree_binder: not called CC=2 SCAR=17.9
Pod documented
420  
421 0 ▶ while (my ($key, $value) = each %$child) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$child) 0 0 0
422 0 $parent->{ $key } = $value;
423 }
424 }
425  
426 sub join_tree_binder {
427 0 ▶ my ($self, $parent, $path, $child, $schema) = @_;
Subroutine join_tree_binder: not called CC=5 SCAR=34.0
Pod documented
428 0 ▶ my $joint = $schema->{ tree_joint } || $self->{ tree_joint };
Condition0%0/3
$schema->{'tree_joint'} || $self->{'tree_joint'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$schema->{'tree_joint'} || $self->{'tree_joint'}
$schema->{'tree_joint'} $self->{'tree_joint'}
Truth table0%0/3
A || B
A= $schema->{'tree_joint'}B= $self->{'tree_joint'}
ABresult
1X1
011
000
429 0 my $base = join($joint, @$path);
430  
431 0 $self->debug(
432 "join_binder path is set: ",
433 $self->dump_data($path),
434 "\nnew base is $base"
435 ) if DEBUG;
436  
437 # Similar to the above but this joins items with underscores
438 # e.g. an entry "foo" in site/bar.yaml will become "bar_foo"
439 0 ▶ while (my ($key, $value) = each %$child) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$child) 0 0 0
440 0 ▶ if ($key =~ s/^\///) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ s[^/][]) { } 0 0 0
elsif (length $base) { } 0 0 0
441 # if the child item has a leading '/' then we want to put it in
442 # the root so we leave $key unchanged
443 }
444 elsif (length $base) {
445 # otherwise the $key is appended onto $base
446 0 $key = join($joint, $base, $key);
447 }
448 0 $parent->{ $key } = $value;
449 }
450 }
451  
452 sub uri_tree_binder {
453 0 ▶ my ($self, $parent, $path, $child, $schema) = @_;
Subroutine uri_tree_binder: not called CC=5 SCAR=34.0
Pod documented
454 0 ▶ my $opt = $schema->{ uri_paths } || $self->{ uri_paths };
Condition0%0/3
$schema->{'uri_paths'} || $self->{'uri_paths'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$schema->{'uri_paths'} || $self->{'uri_paths'}
$schema->{'uri_paths'} $self->{'uri_paths'}
Truth table0%0/3
A || B
A= $schema->{'uri_paths'}B= $self->{'uri_paths'}
ABresult
1X1
011
000
455 0 my $base = join_uri(@$path);
456  
457 0 $self->debug("uri_paths option: $opt") if DEBUG;
458  
459 0 $self->debug(
460 "uri_binder path is set: ",
461 $self->dump_data($path),
462 "\nnew base is $base"
463 ) if DEBUG;
464  
465 # This resolves base items as URIs relative to the parent
466 # e.g. an entry "foo" in the site/bar.yaml file will be stored in the parent
467 # site as "bar/foo", but an entry "/bam" will be stored as "/bam" because
468 # it's an absolute URI rather than a relative one (relative to the $base)
469 0 ▶ while (my ($key, $value) = each %$child) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$child) 0 0 0
470 0 ▶ my $uri = $base ? resolve_uri($base, $key) : $key;
Branch: 1 branch
BranchTrueFalseTotal
$base ? : 0 0 0
471 0 ▶ if ($opt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt) 0 0 0
472 0 $uri = $self->fix_uri_path($uri, $opt);
473 }
474 0 $parent->{ $uri } = $value;
475 0 $self->debug(
476 "loaded metadata for [$base] + [$key] = [$uri]"
477 ) if DEBUG;
478 }
479 }
480  
481 sub fix_uri_path {
482 0 ▶ my ($self, $uri, $option) = @_;
Subroutine fix_uri_path: not called CC=6 SCAR=37.4
Pod undocumented
483  
484 0 ▶ $option ||= $self->{ uri_paths } || return $uri;
Condition0%0/5
$option ||= $self->{"uri_paths"} || (return $uri)
l!l&&r!l&&!r
000
$self->{'uri_paths'} || (return $uri)
l!l
00
MC/DC0%0/2
$option ||= $self->{"uri_paths"} || (return $uri)
$option $self->{'uri_paths'}
Truth table0%0/3
A ||= B
A= $optionB= $self->{'uri_paths'}
ABresult
1X1
011
000
485  
486 0 ▶ if ($option eq 'absolute') {
Branch: 2 branches
BranchTrueFalseTotal
if ($option eq 'absolute') { } 0 0 0
elsif ($option eq 'relative') { } 0 0 0
487 0 $self->debug("setting absolute URI path") if DEBUG;
488 0 ▶ $uri = "/$uri" unless $uri =~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ m[^/] 0 0 0
489 }
490 elsif ($option eq 'relative') {
491 0 $self->debug("setting relative URI path") if DEBUG;
492 0 $uri =~ s/^\///;
493 }
494 else {
495 0 return $self->error_msg( invalid => 'uri_paths option' => $option );
496 }
497  
498 0 return $uri;
499 }
500  
501 #-----------------------------------------------------------------------------
502 # Internal methods
503 #-----------------------------------------------------------------------------
504  
505 sub config_file {
506 0 ▶ my ($self, $name) = @_;
Subroutine config_file: not called CC=2 SCAR=17.9
Pod documented
507  
508 0 ▶ return $self->{ config_file }->{ $name }
Condition0%0/3
$self->{"config_file"}{$name} ||= $self->find_config_file($name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"config_file"}{$name} ||= $self->find_config_file($name)
$self->{"config_file"}{$name} $self->find_config_file($name)
Truth table0%0/3
A ||= B
A= $self->{"config_file"}{$name}B= $self->find_config_file($name)
ABresult
1X1
011
000
509 ||= $self->find_config_file($name);
510 }
511  
512 sub config_file_data {
513 0 ▶ my $self = shift;
Subroutine config_file_data: not called CC=3 SCAR=24.8
Pod documented
514 0 ▶ my $file = $self->config_file(@_) || return;
Condition0%0/2
$self->config_file(@_) || (return)
l!l
00
MC/DC0%0/1
$self->config_file(@_) || (return)
$self->config_file(@_)
Truth table0%0/2
A
A= $self->config_file(@_)
Aresult
11
00
515 0 my $data = $file->try->data;
516 0 ▶ return $self->error_msg( load_fail => $file => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
517 0 return $data;
518 }
519  
520 sub config_filespec {
521 0 ▶ my $self = shift;
Subroutine config_filespec: not called CC=2 SCAR=17.9
Pod documented
522 0 my $defaults = $self->{ filespec };
523  
524 return @_
525 0 ▶ ? extend({ }, $defaults, @_)
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
526 : { %$defaults };
527 }
528  
529 sub find_config_file {
530 0 ▶ my ($self, $name) = @_;
Subroutine find_config_file: not called CC=3 SCAR=24.8
Pod undocumented
531 0 my $root = $self->root;
532 0 my $exts = $self->extensions;
533  
534 0 foreach my $ext (@$exts) {
535 0 my $path = $name.DOT.$ext;
536 0 my $file = $self->file($path);
537 0 ▶ if ($file->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->exists) 0 0 0
538 0 $file->codec($self->codec($ext));
539 0 return $file;
540 }
541 }
542 0 return $self->decline_msg(
543 not_found => file => $name
544 );
545 }
546  
547 sub write_config_file {
548 0 ▶ my ($self, $name, $data) = @_;
Subroutine write_config_file: not called CC=1 SCAR=6.9
Pod undocumented
549 0 my $root = $self->root;
550 0 my $exts = $self->extensions;
551 0 my $ext = $exts->[0];
552 0 my $path = $name.DOT.$ext;
553 0 my $file = $self->file($path);
554  
555 0 $file->codec($self->codec($ext));
556 0 $file->data($data);
557 0 return $file;
558 }
559  
560  
561 sub codec {
562 0 ▶ my ($self, $name) = @_;
Subroutine codec: not called CC=2 SCAR=17.9
Pod undocumented
563 0 ▶ return $self->codecs->{ $name }
Condition0%0/3
$self->codecs->{$name} || $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->codecs->{$name} || $name
$self->codecs->{$name} $name
Truth table0%0/3
A || B
A= $self->codecs->{$name}B= $name
ABresult
1X1
011
000
564 || $name;
565 }
566  
567  
568 #-----------------------------------------------------------------------------
569 # item schema management
570 #-----------------------------------------------------------------------------
571  
572 sub items {
573 return extend(
574 shift->{ item },
575 @_
576 0 ▶ );
Subroutine items: not called CC=1 SCAR=6.9
Pod undocumented
577 }
578  
579 sub item {
580 0 ▶ my ($self, $name) = @_;
Subroutine item: not called CC=2 SCAR=17.9
Pod undocumented
581  
582 0 $self->debug_data("looking for $name in items: ", $self->{ item }) if DEBUG;
583  
584 0 ▶ return $self->{ item }->{ $name }
Condition0%0/3
$self->{"item"}{$name} ||= $self->lookup_item($name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"item"}{$name} ||= $self->lookup_item($name)
$self->{"item"}{$name} $self->lookup_item($name)
Truth table0%0/3
A ||= B
A= $self->{"item"}{$name}B= $self->lookup_item($name)
ABresult
1X1
011
000
585 ||= $self->lookup_item($name);
586 }
587  
588 sub lookup_item {
589 # hook for subclasses
590 0 ▶ return undef;
Subroutine lookup_item: not called CC=1 SCAR=6.9
Pod undocumented
591 }
592  
593 sub item_schema {
594 0 ▶ my ($self, $name, $schema) = @_;
Subroutine item_schema: not called CC=2 SCAR=17.9
Pod undocumented
595 0 my $data = $self->item($name);
596  
597 0 if (DEBUG) {
598 $self->debug_data("$name item schema data: ", $data);
599 $self->debug_data("$name file schema: ", $schema);
600 }
601  
602 0 ▶ if ($schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema) 0 0 0
603 0 $data = extend({ }, $data, $schema);
604 }
605  
606 # the schema we got may have been for a parent via lookup_item.
607 0 $self->{ item }->{ $name } = $data;
608 0 $self->debug_data("set new item $name data", $data) if DEBUG;
609  
610 0 return $data;
611 }
612  
613 sub item_schema_from_data {
614 0 ▶ my ($self, $name, $data) = @_;
Subroutine item_schema_from_data: not called CC=4 SCAR=30.0
Pod undocumented
615 0 my $more;
616  
617 0 ▶ if ($data && ref $data eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and ref $data eq "HASH") 0 0 0
Condition0%0/3
$data and ref $data eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$data and ref $data eq "HASH"
$data ref $data eq "HASH"
Truth table0%0/3
A and B
A= $dataB= ref $data eq "HASH"
ABresult
0X0
100
111
618 # In the event that someone needs to store a 'schema' item in the *real*
619 # configuration data, we look for '_schema_' first and delete that,
620 # leaving 'schema' untouched
621 $more = delete $data->{_schema_}
622 0 ▶ || delete $data->{ schema };
Condition0%0/3
delete $data->{'_schema_'} || delete $data->{'schema'}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $data->{'_schema_'} || delete $data->{'schema'}
delete $data->{'_schema_'} delete $data->{'schema'}
Truth table0%0/3
A || B
A= delete $data->{'_schema_'}B= delete $data->{'schema'}
ABresult
1X1
011
000
623 }
624 0 return$self->item_schema($name, $more);
625 }
626  
627  
628  
629 sub has_item {
630 0 ▶ my $self = shift->prototype;
Subroutine has_item: not called CC=3 SCAR=24.8
Pod undocumented
631 0 my $name = shift;
632 0 my $item = $self->{ item }->{ $name };
633  
634 # This is all the same as in the base class up to the final test which
635 # looks for $self->config_file($name) as a last-ditch attempt
636  
637 0 ▶ if (defined $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item) { } 0 0 0
638 # A 1/0 entry in the item tells us if an item categorically does or
639 # doesn't exist in the config data set (or allowable set - it might
640 # be a valid configuration option that simply hasn't been set yet)
641 0 return $item;
642 }
643 else {
644 # Otherwise the existence (or not) of an item in the data set is
645 # enough to satisfy us one way or another
646 return 1
647 0 ▶ if exists $self->{ data }->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"data"}{$name} 0 0 0
648  
649 # Special case for B::C::Filesystem which looks to see if there's a
650 # matching config file. We cache the existence in $self->{ item }
651 # so we know if it's there (or not) for next time
652 0 return $self->{ item }->{ $name }
653 = $self->config_file($name);
654 }
655 }
656  
657  
658 2 1;
659  
660 __END__
« blib/lib/Badger/Config/Filesystem.pm ↑ Summary blib/lib/Badger/Date.pm »