lib/Badger/Config/Filesystem.pm

filter: Statementstmt 84.9% 220 / 259 Branchbran 67.5% 54 / 80 Conditioncond 47.5% 49 / 103 MC/DCmcdc 20.3% 12 / 59 Subroutinesub 86.6% 26 / 30 Podpod 55.1% 16 / 29 totaltotal 67.3% 377 / 560 CC 85 SCAR 21.7
CC
85
Cov
73%
config_tree
21.1
nest_tree_binder
16.9
config_file_data
12.0
CRAP 8.7
« blib/lib/Badger/Progress.pm ↑ Summary lib/Badger/Workplace.pm »
1 package Badger::Config::Filesystem;
2  
3 use Badger::Class
4 3 ▶ version => 0.01,
Subroutine BEGIN: called CC=1 SCAR=0.0
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 3 };
23  
24 3 our $EXTENSIONS = [YAML, JSON];
25 3 our $ENCODING = UTF8;
26 3 our $CODECS = { };
27 3 our $STAT_TTL = 0;
28  
29  
30 #-----------------------------------------------------------------------------
31 # Initialisation methods called at object creation time
32 #-----------------------------------------------------------------------------
33  
34 9 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
35 9 my ($self, $config) = @_;
36  
37 # First call Badger::Config base class method to handle any 'items'
38 # definitions and other general initialisation
39 9 $self->init_config($config);
40  
41 # Then our own custom init method
42 9 $self->init_filesystem($config);
43 }
44  
45 9 ▶sub init_filesystem {
Subroutine init_filesystem: called CC=10 SCAR=23.6
Pod documented
46 9 my ($self, $config) = @_;
47 9 my $class = $self->class;
48  
49 9 $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 9 ▶ || $class->any_var(ENCODING);
Condition33%1/3
$config->{'encoding'} || $class->any_var('ENCODING')
l!l&&r!l&&!r
090
MC/DC0%0/2
$config->{'encoding'} || $class->any_var('ENCODING')
$config->{'encoding'} $class->any_var('ENCODING')
Truth table33%1/3
A || B
A= $config->{'encoding'}B= $class->any_var('ENCODING')
ABresult
1X1
011
000
57  
58 my $filespec = $config->{ filespec } ||= {
59 9 ▶ encoding => $encoding
Condition50%1/2
$config->{"filespec"} ||= {"encoding", $encoding}
l!l
09
MC/DC0%0/1
$config->{"filespec"} ||= {"encoding", $encoding}
$config->{"filespec"}
Truth table50%1/2
A
A= $config->{"filespec"}
Aresult
11
00
60 };
61  
62 # now initialise the workplace root directory
63 9 $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 9 );
72 $exts = [
73 9 map { @{ split_to_list($_) } }
74 @$exts
75 ];
76  
77 # Construct a regex to match any of the above
78 9 my $qm_ext = join('|', map { quotemeta $_ } @$exts);
79 9 my $ext_re = qr/.($qm_ext)$/i;
80  
81 9 $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 9 );
91  
92 9 ▶ my $data = $config->{ data } || { };
Condition100%2/2
$config->{'data'} || {}
l!l
45
MC/DC100%1/1
$config->{'data'} || {}
$config->{'data'}
Truth table100%2/2
A
A= $config->{'data'}
Aresult
11
00
93  
94 9 $self->{ data } = $data;
95 9 $self->{ extensions } = $exts;
96 9 $self->{ match_ext } = $ext_re;
97 9 $self->{ codecs } = $codecs;
98 9 $self->{ encoding } = $encoding;
99 9 $self->{ filespec } = $filespec;
100 9 ▶ $self->{ quiet } = $config->{ quiet } || FALSE;
Condition50%1/2
$config->{'quiet'} || 0
l!l
09
MC/DC0%0/1
$config->{'quiet'} || 0
$config->{'quiet'}
Truth table50%1/2
A
A= $config->{'quiet'}
Aresult
11
00
101 9 ▶ $self->{ dir_tree } = $config->{ dir_tree } // TRUE;
Condition50%1/2
$config->{"dir_tree"} // 1
l!l
09
MC/DC0%0/1
$config->{"dir_tree"} // 1
$config->{"dir_tree"}
Truth table50%1/2
A
A= $config->{"dir_tree"}
Aresult
11
00
102 9 ▶ $self->{ stat_ttl } = $config->{ stat_ttl } // $data->{ stat_ttl } // $STAT_TTL;
Condition33%2/6
($config->{"stat_ttl"} // $data->{"stat_ttl"}) // $Badger::Config::Filesystem::STAT_TTL
l!l&&r!l&&!r
090
$config->{"stat_ttl"} // $data->{"stat_ttl"}
l!l&&r!l&&!r
009
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 table25%1/4
A // B // C
A= $config->{"stat_ttl"}B= $data->{"stat_ttl"}C= $Badger::Config::Filesystem::STAT_TTL
ABCresult
1XX1
01X1
0011
0000
103 9 $self->{ not_found } = { };
104  
105 # Add any item schemas
106 $self->items( $config->{ schemas } )
107 9 ▶ if $config->{ schemas };
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"schemas"} 8 1 9
108  
109 # Configuration file allows further data items (and schemas) to be defined
110 $self->init_file( $config->{ file } )
111 9 ▶ if $config->{ file };
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"file"} 5 4 9
112  
113 9 return $self;
114 }
115  
116 5 ▶sub init_file {
Subroutine init_file: called CC=3 SCAR=11.7
Pod undocumented
117 5 my ($self, $file) = @_;
118 5 my $data = $self->get($file);
119  
120 5 ▶ if ($data) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data) { } 5 0 5
elsif (not $self->{'quiet'}) { } 0 0 0
121 # must copy data so as not to damage cached version
122 5 $data = { %$data };
123  
124 5 $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 5 );
134  
135 # anything else is config data
136 5 extend($self->{ data }, $data);
137  
138 5 $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 5 return $self;
145 }
146  
147 0 ▶sub no_config_file {
Subroutine no_config_file: not called CC=1 SCAR=6.9
Pod undocumented
148 0 shift->warn_msg( no_config_file => @_ );
149 }
150  
151  
152 #-----------------------------------------------------------------------------
153 # Redefine head() method in Badger::Config to hook into fetch() to load data
154 #-----------------------------------------------------------------------------
155  
156 53 ▶sub head {
Subroutine head: called CC=2 SCAR=6.9
Pod documented
157 53 my ($self, $name) = @_;
158 53 ▶ return $self->{ data }->{ $name }
Condition100%3/3
$self->{"data"}{$name} // $self->fetch($name)
l!l&&r!l&&!r
11375
MC/DC100%2/2
$self->{"data"}{$name} // $self->fetch($name)
$self->{"data"}{$name} $self->fetch($name)
Truth table100%3/3
A // B
A= $self->{"data"}{$name}B= $self->fetch($name)
ABresult
1X1
011
000
159 // $self->fetch($name);
160 }
161  
162 37 ▶sub tail {
Subroutine tail: called CC=1 SCAR=0.0
Pod documented
163 37 my ($self, $name, $data) = @_;
164 37 return $data;
165 }
166  
167  
168 #-----------------------------------------------------------------------------
169 # Filesystem-specific fetch methods
170 #-----------------------------------------------------------------------------
171  
172 42 ▶sub fetch {
Subroutine fetch: called CC=7 SCAR=19.6
Pod documented
173 42 my ($self, $uri) = @_;
174  
175 42 ▶ return if $self->previously_not_found($uri);
Branch: 1 branch
BranchTrueFalseTotal
if $self->previously_not_found($uri) 0 42 42
176  
177 42 $self->debug("fetch($uri)") if DEBUG or DEBUG_FETCH;
178  
179 42 my $file = $self->config_file($uri);
180 42 my $dir = $self->dir($uri);
181 42 ▶ my $fok = $file && $file->exists;
Condition66%2/3
$file && $file->exists
!ll&&!rl&&r
6036
MC/DC50%1/2
$file && $file->exists
$file $file->exists
Truth table66%2/3
A && B
A= $fileB= $file->exists
ABresult
0X0
100
111
182 42 ▶ my $dok = $dir && $dir->exists;
Condition66%2/3
$dir && $dir->exists
!ll&&!rl&&r
02517
MC/DC50%1/2
$dir && $dir->exists
$dir $dir->exists
Truth table66%2/3
A && B
A= $dirB= $dir->exists
ABresult
0X0
100
111
183  
184 42 ▶ if ($dok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dok) 17 25 42
185 17 $self->debug("Found directory for $uri, loading tree") if DEBUG or DEBUG_FETCH;
186 17 return $self->config_tree($uri, $file, $dir);
187 }
188  
189 25 ▶ if ($fok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fok) 20 5 25
190 20 $self->debug("Found file for $uri, loading file data => ", $file->absolute) if DEBUG or DEBUG_FETCH;
191 20 my $data = $file->try->data;
192 20 ▶ return $self->error_msg( load_fail => $file => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 20 20
193 20 return $self->tail(
194 $uri, $data,
195 $self->item_schema_from_data(
196 $uri, $data
197 )
198 );
199 }
200  
201 5 $self->debug("No file or directory found for $uri") if DEBUG or DEBUG_FETCH;
202 5 $self->{ not_found }->{ $uri } = time();
203 5 return undef;
204 }
205  
206 42 ▶sub previously_not_found {
Subroutine previously_not_found: called CC=4 SCAR=24.2
Pod undocumented
207 42 my ($self, $uri) = @_;
208 42 ▶ my $sttl = $self->{ stat_ttl } || return 0;
Condition50%1/2
$self->{'stat_ttl'} || (return 0)
l!l
042
MC/DC0%0/1
$self->{'stat_ttl'} || (return 0)
$self->{'stat_ttl'}
Truth table50%1/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 17 ▶sub config_tree {
Subroutine config_tree: called CC=16 SCAR=30.5
Pod documented
225 17 my $self = shift;
226 17 my $name = shift;
227 17 ▶ my $file = shift || $self->config_file($name);
Condition66%2/3
shift() || $self->config_file($name)
l!l&&r!l&&!r
1601
MC/DC50%1/2
shift() || $self->config_file($name)
shift() $self->config_file($name)
Truth table66%2/3
A || B
A= shift()B= $self->config_file($name)
ABresult
1X1
011
000
228 17 ▶ my $dir = shift || $self->dir($name);
Condition33%1/3
shift() || $self->dir($name)
l!l&&r!l&&!r
1700
MC/DC0%0/2
shift() || $self->dir($name)
shift() $self->dir($name)
Truth table33%1/3
A || B
A= shift()B= $self->dir($name)
ABresult
1X1
011
000
229 17 my $do_tree = $self->{ dir_tree };
230 17 my $data = undef; #{ };
231 17 my ($file_data, $binder, $more);
232  
233 17 ▶ unless ($file && $file->exists || $dir->exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file and $file->exists or $dir->exists) 0 17 17
Condition66%4/6
$file and $file->exists or $dir->exists
l!l&&r!l&&!r
1610
$file and $file->exists
!ll&&!rl&&r
1016
MC/DC0%0/3
$file and $file->exists or $dir->exists
$file $file->exists $dir->exists
Truth table40%2/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 17 ▶ if ($file && $file->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file and $file->exists) 16 1 17
Condition66%2/3
$file and $file->exists
!ll&&!rl&&r
1016
MC/DC50%1/2
$file and $file->exists
$file $file->exists
Truth table66%2/3
A and B
A= $fileB= $file->exists
ABresult
0X0
100
111
239 16 $file_data = $file->try->data;
240 16 ▶ return $self->error_msg( load_fail => $file => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 16 16
241 16 $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 17 my $schema = $self->item_schema_from_data($name, $file_data);
248  
249 17 $self->debug(
250 "combined schema for $name: ",
251 $self->dump_data($schema)
252 ) if DEBUG;
253  
254 17 ▶ if ($more = $schema->{ tree_type }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($more = $schema->{"tree_type"}) 16 1 17
255 16 $self->debug("schema.tree_type: $more") if DEBUG;
256 16 ▶ if ($more eq NONE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($more eq 'none') { } 0 16 16
elsif ($binder = $self->tree_binder($more)) { } 16 0 16
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 16 $self->debug("schema rules indicate a $more tree tree") if DEBUG;
262 16 $do_tree = TRUE;
263 }
264 else {
265 0 return $self->error_msg( invalid => tree_type => $more );
266 }
267 }
268  
269 17 ▶ if ($do_tree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_tree) { } 17 0 17
270 # merge file data using binder
271 17 ▶ $data ||= { };
Condition50%1/2
$data ||= {}
l!l
017
MC/DC0%0/1
$data ||= {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
272 17 ▶ $binder ||= $self->tree_binder('nest');
Condition66%2/3
$binder ||= $self->tree_binder("nest")
l!l&&r!l&&!r
1610
MC/DC0%0/2
$binder ||= $self->tree_binder("nest")
$binder $self->tree_binder("nest")
Truth table66%2/3
A ||= B
A= $binderB= $self->tree_binder("nest")
ABresult
1X1
011
000
273 17 $binder->($self, $data, [ ], $file_data, $schema);
274  
275 17 ▶ if ($dir->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir->exists) 17 0 17
276 # create a virtual file system rooted on the metadata directory
277 # so that all file paths are resolved relative to it
278 17 my $vfs = VFS->new( root => $dir );
279 17 $self->debug("Reading metadata from dir: ", $dir->name) if DEBUG;
280 17 $self->scan_config_dir($vfs->root, $data, [ ], $schema, $binder);
281 }
282 }
283 else {
284 0 $data = $file_data;
285 }
286  
287 17 $self->debug("$name config: ", $self->dump_data($data)) if DEBUG;
288  
289 17 return $self->tail(
290 $name, $data, $schema
291 );
292 }
293  
294 24 ▶sub scan_config_dir {
Subroutine scan_config_dir: called CC=7 SCAR=19.9
Pod documented
295 24 my ($self, $dir, $data, $path, $schema, $binder) = @_;
296 24 my $files = $dir->files;
297 24 my $dirs = $dir->dirs;
298 24 ▶ $path ||= [ ];
Condition50%1/2
$path ||= []
l!l
240
MC/DC0%0/1
$path ||= []
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
299 24 ▶ $binder ||= $self->tree_binder;
Condition33%1/3
$binder ||= $self->tree_binder
l!l&&r!l&&!r
2400
MC/DC0%0/2
$binder ||= $self->tree_binder
$binder $self->tree_binder
Truth table33%1/3
A ||= B
A= $binderB= $self->tree_binder
ABresult
1X1
011
000
300  
301 24 $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 24 ▶ $data ||= { };
Condition50%1/2
$data ||= {}
l!l
240
MC/DC0%0/1
$data ||= {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
309  
310 24 foreach my $file (@$files) {
311 26 ▶ next unless $file->name =~ $self->{ match_ext };
Branch: 1 branch
BranchTrueFalseTotal
unless $file->name =~ /$self->{"match_ext"}/i 0 26 26
312 26 $self->debug("found file: ", $file->name, ' at ', $file->path) if DEBUG;
313 26 $self->scan_config_file($file, $data, $path, $schema, $binder);
314 }
315 24 foreach my $subdir (@$dirs) {
316 7 $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 7 my $name = $subdir->name;
319 #my $more = $binder ? $data : ($data->{ $name } = { });
320 7 push(@$path, $name);
321 #$self->scan_config_dir($subdir, $more, $path, $schema, $binder);
322 7 $self->scan_config_dir($subdir, $data, $path, $schema, $binder);
323 7 pop(@$path);
324 }
325 }
326  
327 26 ▶sub scan_config_file {
Subroutine scan_config_file: called CC=4 SCAR=14.3
Pod documented
328 26 my ($self, $file, $data, $path, $schema, $binder) = @_;
329 26 my $base = $file->basename;
330 26 my $ext = $file->extension;
331  
332 26 $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 26 $file->codec( $self->codec($ext) );
342 26 $file->encoding( $self->{ encoding } );
343  
344 26 my $meta = $file->try->data;
345 26 ▶ return $self->error_msg( load_fail => $file => $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 26 26
346  
347 26 $self->debug("Metadata: ", $self->dump_data($meta)) if DEBUG;
348  
349 26 ▶ if ($binder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binder) { } 26 0 26
350 26 ▶ $path ||= [ ];
Condition50%1/2
$path ||= []
l!l
260
MC/DC0%0/1
$path ||= []
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
351 26 push(@$path, $base);
352 26 $binder->($self, $data, $path, $meta, $schema);
353 26 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 17 ▶sub tree_binder {
Subroutine tree_binder: called CC=4 SCAR=16.1
Pod documented
368 17 my $self = shift;
369 my $name = shift
370 || $self->{ tree_type }
371 17 ▶ || return $self->error_msg( missing => 'tree_type' );
Condition40%2/5
shift() || $self->{'tree_type'} || (return $self->error_msg('missing', 'tree_type'))
l!l
170
shift() or $self->{"tree_type"}
l!l&&r!l&&!r
1700
MC/DC0%0/2
shift() || $self->{'tree_type'} || (return $self->error_msg('missing', 'tree_type'))
shift() $self->{"tree_type"}
Truth table33%1/3
A or B
A= shift()B= $self->{"tree_type"}
ABresult
1X1
011
000
372  
373 17 ▶ return $self->can("${name}_tree_binder")
Condition50%1/2
$self->can("${name}_tree_binder") || (return $self->decline_msg('invalid', 'binder', $name))
l!l
170
MC/DC0%0/1
$self->can("${name}_tree_binder") || (return $self->decline_msg('invalid', 'binder', $name))
$self->can("${name}_tree_binder")
Truth table50%1/2
A
A= $self->can("${name}_tree_binder")
Aresult
11
00
374 || return $self->decline_msg( invalid => binder => $name );
375 }
376  
377 3 ▶sub nest_tree_binder {
Subroutine nest_tree_binder: called CC=9 SCAR=28.2
Pod documented
378 3 my ($self, $parent, $path, $child, $schema) = @_;
379 3 my $data = $parent;
380 3 my $uri = join('/', @$path);
381 3 my @bits = @$path;
382 3 my $last = pop @bits;
383  
384 3 $self->debug("Adding [$uri] as ", $self->dump_data($child))if DEBUG;
385  
386 3 foreach my $key (@bits) {
387 1 ▶ $data = $data->{ $key } ||= { };
Condition50%1/2
$data->{$key} ||= {}
l!l
01
MC/DC0%0/1
$data->{$key} ||= {}
$data->{$key}
Truth table50%1/2
A
A= $data->{$key}
Aresult
11
00
388 }
389  
390 3 ▶ if ($last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last) { } 2 1 3
391 2 my $tail = $data->{ $last };
392  
393 2 ▶ if ($tail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail) { } 0 2 2
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 2 $self->debug("setting $last in data to $child") if DEBUG;
407 2 $data->{ $last } = $child;
408 }
409 }
410 else {
411 1 $self->debug("No path, simple merge of child into parent") if DEBUG;
412 1 @$data{ keys %$child } = values %$child;
413 }
414  
415 3 $self->debug("New parent: ", $self->dump_data($parent)) if DEBUG;
416 }
417  
418 6 ▶sub flat_tree_binder {
Subroutine flat_tree_binder: called CC=2 SCAR=6.9
Pod documented
419 6 my ($self, $parent, $path, $child, $schema) = @_;
420  
421 6 ▶ while (my ($key, $value) = each %$child) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$child) 12 6 18
422 12 $parent->{ $key } = $value;
423 }
424 }
425  
426 18 ▶sub join_tree_binder {
Subroutine join_tree_binder: called CC=5 SCAR=16.2
Pod documented
427 18 my ($self, $parent, $path, $child, $schema) = @_;
428 18 ▶ my $joint = $schema->{ tree_joint } || $self->{ tree_joint };
Condition33%1/3
$schema->{'tree_joint'} || $self->{'tree_joint'}
l!l&&r!l&&!r
1800
MC/DC0%0/2
$schema->{'tree_joint'} || $self->{'tree_joint'}
$schema->{'tree_joint'} $self->{'tree_joint'}
Truth table33%1/3
A || B
A= $schema->{'tree_joint'}B= $self->{'tree_joint'}
ABresult
1X1
011
000
429 18 my $base = join($joint, @$path);
430  
431 18 $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 18 ▶ while (my ($key, $value) = each %$child) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$child) 38 18 56
440 38 ▶ if ($key =~ s/^\///) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ s[^/][]) { } 6 32 38
elsif (length $base) { } 20 12 32
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 20 $key = join($joint, $base, $key);
447 }
448 38 $parent->{ $key } = $value;
449 }
450 }
451  
452 16 ▶sub uri_tree_binder {
Subroutine uri_tree_binder: called CC=5 SCAR=16.3
Pod documented
453 16 my ($self, $parent, $path, $child, $schema) = @_;
454 16 ▶ my $opt = $schema->{ uri_paths } || $self->{ uri_paths };
Condition33%1/3
$schema->{'uri_paths'} || $self->{'uri_paths'}
l!l&&r!l&&!r
1600
MC/DC0%0/2
$schema->{'uri_paths'} || $self->{'uri_paths'}
$schema->{'uri_paths'} $self->{'uri_paths'}
Truth table33%1/3
A || B
A= $schema->{'uri_paths'}B= $self->{'uri_paths'}
ABresult
1X1
011
000
455 16 my $base = join_uri(@$path);
456  
457 16 $self->debug("uri_paths option: $opt") if DEBUG;
458  
459 16 $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 16 ▶ while (my ($key, $value) = each %$child) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$child) 38 16 54
470 38 ▶ my $uri = $base ? resolve_uri($base, $key) : $key;
Branch: 1 branch
BranchTrueFalseTotal
$base ? : 24 14 38
471 38 ▶ if ($opt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt) 38 0 38
472 38 $uri = $self->fix_uri_path($uri, $opt);
473 }
474 38 $parent->{ $uri } = $value;
475 38 $self->debug(
476 "loaded metadata for [$base] + [$key] = [$uri]"
477 ) if DEBUG;
478 }
479 }
480  
481 38 ▶sub fix_uri_path {
Subroutine fix_uri_path: called CC=6 SCAR=19.4
Pod undocumented
482 38 my ($self, $uri, $option) = @_;
483  
484 38 ▶ $option ||= $self->{ uri_paths } || return $uri;
Condition20%1/5
$option ||= $self->{"uri_paths"} || (return $uri)
l!l&&r!l&&!r
3800
$self->{'uri_paths'} || (return $uri)
l!l
00
MC/DC0%0/2
$option ||= $self->{"uri_paths"} || (return $uri)
$option $self->{'uri_paths'}
Truth table33%1/3
A ||= B
A= $optionB= $self->{'uri_paths'}
ABresult
1X1
011
000
485  
486 38 ▶ if ($option eq 'absolute') {
Branch: 2 branches
BranchTrueFalseTotal
if ($option eq 'absolute') { } 9 29 38
elsif ($option eq 'relative') { } 29 0 29
487 9 $self->debug("setting absolute URI path") if DEBUG;
488 9 ▶ $uri = "/$uri" unless $uri =~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ m[^/] 8 1 9
489 }
490 elsif ($option eq 'relative') {
491 29 $self->debug("setting relative URI path") if DEBUG;
492 29 $uri =~ s/^\///;
493 }
494 else {
495 0 return $self->error_msg( invalid => 'uri_paths option' => $option );
496 }
497  
498 38 return $uri;
499 }
500  
501 #-----------------------------------------------------------------------------
502 # Internal methods
503 #-----------------------------------------------------------------------------
504  
505 45 ▶sub config_file {
Subroutine config_file: called CC=2 SCAR=6.9
Pod documented
506 45 my ($self, $name) = @_;
507  
508 45 ▶ return $self->{ config_file }->{ $name }
Condition100%3/3
$self->{"config_file"}{$name} ||= $self->find_config_file($name)
l!l&&r!l&&!r
14238
MC/DC100%2/2
$self->{"config_file"}{$name} ||= $self->find_config_file($name)
$self->{"config_file"}{$name} $self->find_config_file($name)
Truth table100%3/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 0 ▶sub config_file_data {
Subroutine config_file_data: not called CC=3 SCAR=24.8
Pod documented
513 0 my $self = shift;
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 0 ▶sub config_filespec {
Subroutine config_filespec: not called CC=2 SCAR=17.9
Pod documented
521 0 my $self = shift;
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 31 ▶sub find_config_file {
Subroutine find_config_file: called CC=3 SCAR=11.0
Pod undocumented
530 31 my ($self, $name) = @_;
531 31 my $root = $self->root;
532 31 my $exts = $self->extensions;
533  
534 31 foreach my $ext (@$exts) {
535 39 my $path = $name.DOT.$ext;
536 39 my $file = $self->file($path);
537 39 ▶ if ($file->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->exists) 23 16 39
538 23 $file->codec($self->codec($ext));
539 23 return $file;
540 }
541 }
542 8 return $self->decline_msg(
543 not_found => file => $name
544 );
545 }
546  
547 0 ▶sub write_config_file {
Subroutine write_config_file: not called CC=1 SCAR=6.9
Pod undocumented
548 0 my ($self, $name, $data) = @_;
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 49 ▶sub codec {
Subroutine codec: called CC=2 SCAR=8.1
Pod undocumented
562 49 my ($self, $name) = @_;
563 49 ▶ return $self->codecs->{ $name }
Condition33%1/3
$self->codecs->{$name} || $name
l!l&&r!l&&!r
0490
MC/DC0%0/2
$self->codecs->{$name} || $name
$self->codecs->{$name} $name
Truth table33%1/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 13 ▶sub items {
Subroutine items: called CC=1 SCAR=0.0
Pod undocumented
573 return extend(
574 shift->{ item },
575 @_
576 13 );
577 }
578  
579 37 ▶sub item {
Subroutine item: called CC=2 SCAR=7.0
Pod undocumented
580 37 my ($self, $name) = @_;
581  
582 37 $self->debug_data("looking for $name in items: ", $self->{ item }) if DEBUG;
583  
584 37 ▶ return $self->{ item }->{ $name }
Condition66%2/3
$self->{"item"}{$name} ||= $self->lookup_item($name)
l!l&&r!l&&!r
17020
MC/DC50%1/2
$self->{"item"}{$name} ||= $self->lookup_item($name)
$self->{"item"}{$name} $self->lookup_item($name)
Truth table66%2/3
A ||= B
A= $self->{"item"}{$name}B= $self->lookup_item($name)
ABresult
1X1
011
000
585 ||= $self->lookup_item($name);
586 }
587  
588 20 ▶sub lookup_item {
Subroutine lookup_item: called CC=1 SCAR=0.0
Pod undocumented
589 # hook for subclasses
590 20 return undef;
591 }
592  
593 37 ▶sub item_schema {
Subroutine item_schema: called CC=2 SCAR=7.1
Pod undocumented
594 37 my ($self, $name, $schema) = @_;
595 37 my $data = $self->item($name);
596  
597 37 if (DEBUG) {
598 $self->debug_data("$name item schema data: ", $data);
599 $self->debug_data("$name file schema: ", $schema);
600 }
601  
602 37 ▶ if ($schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema) 0 37 37
603 0 $data = extend({ }, $data, $schema);
604 }
605  
606 # the schema we got may have been for a parent via lookup_item.
607 37 $self->{ item }->{ $name } = $data;
608 37 $self->debug_data("set new item $name data", $data) if DEBUG;
609  
610 37 return $data;
611 }
612  
613 37 ▶sub item_schema_from_data {
Subroutine item_schema_from_data: called CC=4 SCAR=14.0
Pod undocumented
614 37 my ($self, $name, $data) = @_;
615 37 my $more;
616  
617 37 ▶ if ($data && ref $data eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and ref $data eq "HASH") 35 2 37
Condition100%3/3
$data and ref $data eq "HASH"
!ll&&!rl&&r
1135
MC/DC100%2/2
$data and ref $data eq "HASH"
$data ref $data eq "HASH"
Truth table100%3/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 35 ▶ || delete $data->{ schema };
Condition33%1/3
delete $data->{'_schema_'} || delete $data->{'schema'}
l!l&&r!l&&!r
0035
MC/DC0%0/2
delete $data->{'_schema_'} || delete $data->{'schema'}
delete $data->{'_schema_'} delete $data->{'schema'}
Truth table33%1/3
A || B
A= delete $data->{'_schema_'}B= delete $data->{'schema'}
ABresult
1X1
011
000
623 }
624 37 return$self->item_schema($name, $more);
625 }
626  
627  
628  
629 12 ▶sub has_item {
Subroutine has_item: called CC=3 SCAR=11.0
Pod undocumented
630 12 my $self = shift->prototype;
631 12 my $name = shift;
632 12 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 12 ▶ if (defined $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item) { } 8 4 12
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 8 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 4 ▶ if exists $self->{ data }->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"data"}{$name} 2 2 4
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 2 return $self->{ item }->{ $name }
653 = $self->config_file($name);
654 }
655 }
656  
657  
658 3 1;
659  
660 __END__
« blib/lib/Badger/Progress.pm ↑ Summary lib/Badger/Workplace.pm »