lib/Badger/Config/Schema.pm

filter: Statementstmt 92.7% 64 / 69 Branchbran 80.0% 16 / 20 Conditioncond 50.0% 7 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 77.7% 7 / 9 Podpod 16.6% 1 / 6 totaltotal 74.8% 95 / 127 CC 18 SCAR 20.8
CC
18
Cov
84%
init_schema
13.1
item
6.0
configure
4.0
CRAP 8.0
« lib/Badger/Hub.pm ↑ Summary lib/Badger/Filesystem/Visitor.pm »
1 package Badger::Config::Schema;
2  
3 7 ▶use Badger::Debug ':dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Badger::Config::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use Badger::Class
6 7 version => 0.01,
7 debug => 0,
8 base => 'Badger::Base',
9 import => 'class CLASS',
10 words => 'CONFIG_SCHEMA',
11 utils => 'is_object',
12 # accessors => 'items',
13 constants => 'HASH ARRAY DELIMITER',
14 constant => {
15 CONFIG_METHOD => 'configure',
16 CONFIG_ITEM => 'Badger::Config::Item',
17 VALUE => 1,
18 NOTHING => 0,
19 },
20 messages => {
21 bad_type => 'Invalid type prefix specified for %s: %s',
22 bad_method => 'Missing method for the %s %s configuration item: %s',
23 dup_item => 'Duplicate specification for scheme item: %s',
24 bad_fallback => 'Invalid fallback item specified for %s: %s',
25 7 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 sub init {
28 21 ▶ my ($self, $config) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
29 21 $self->init_schema($config);
30 21 return $self;
31 }
32  
33 sub init_schema {
34 21 ▶ my ($self, $config) = @_;
Subroutine init_schema: called CC=13 SCAR=25.8
Pod undocumented
35 21 my $iclass = $self->CONFIG_ITEM;
36 21 my ($name, $info, @aka, $fallback, $test, $item);
37  
38 21 ▶ my $fall = $config->{ fallback } || $self;
Condition66%2/3
$config->{'fallback'} || $self
l!l&&r!l&&!r
2010
MC/DC0%0/2
$config->{'fallback'} || $self
$config->{'fallback'} $self
Truth table66%2/3
A || B
A= $config->{'fallback'}B= $self
ABresult
1X1
011
000
39 21 my $list = $self->{ items } = [ ];
40 21 my $hash = $self->{ item } = { };
41  
42 21 my $schema = $config->{ schema };
43 21 my $extend = $config->{ extend };
44  
45 21 $self->debug("fallback is $fall") if DEBUG;
46 # allow target class to be specified so we can resolve things like
47 # package variables later
48 # $self->{ class } = $config->{ class } || $config->{ target };
49  
50 # $self->debug("extending on from ", $self->dump_data($extend));
51  
52 21 $self->debug("Generating schema from config: ", $self->dump_data($config))
53 if DEBUG;
54  
55 # We allow a scheme to be specified as a list reference in case the
56 # order of evaluation is important. For convenience, we also accept
57 # a hash ref for a schema specification where the order isn't important.
58 # The values in the hash array can themselves be hash references or
59 # simple values which we assume is the default value.
60 $schema = [
61 map {
62 21 ▶ my $k = $_;
Branch: 1 branch
BranchTrueFalseTotal
if ref $schema eq "HASH" 8 13 21
63 6 my $v = $schema->{ $k };
64 6 ▶ ref $v eq HASH
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'HASH' ? : 6 0 6
65 ? { name => $k, %$v }
66 : { name => $k, default => $v }
67 }
68 keys %$schema
69 ] if ref $schema eq HASH;
70  
71 21 ▶ $schema = [ @$schema, $extend ? @$extend : () ];
Branch: 1 branch
BranchTrueFalseTotal
$extend ? : 20 1 21
72  
73 21 $self->debug("Canonical schema config: ", $self->dump_data($schema))
74 if DEBUG;
75  
76 21 ▶ while (@$schema) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$schema) 74 21 95
77 74 $name = shift @$schema;
78 74 $item = undef;
79 74 $info = undef;
80  
81 # TODO: not sure about this - we change the name....
82 # skip anything we've already done
83  
84 74 $self->debug("schema item: $name\n") if DEBUG;
85  
86 74 ▶ if (ref $name eq HASH) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $name eq 'HASH') { } 7 67 74
elsif (&is_object('Badger::Config::Item', $name)) { } 15 52 67
87 7 $info = $name;
88 $name = $info->{ name }
89 7 ▶ || return $self->error("Invalid hash (no name): ", $self->dump_data($info));
Condition50%1/2
$info->{'name'} || (return $self->error('Invalid hash (no name): ', $self->dump_data($info)))
l!l
70
MC/DC0%0/1
$info->{'name'} || (return $self->error('Invalid hash (no name): ', $self->dump_data($info)))
$info->{'name'}
Truth table50%1/2
A
A= $info->{'name'}
Aresult
11
00
90 }
91 elsif (is_object(CONFIG_ITEM, $name)) {
92 15 $item = $name;
93 15 $name = $item->name;
94 }
95 else {
96 52 $info = { name => $name };
97 }
98 74 $self->debug("name: $name info: $info") if DEBUG;
99  
100 74 ▶ $info->{ fallback_provider } ||= $fall;
Condition33%1/3
$info->{"fallback_provider"} ||= $fall
l!l&&r!l&&!r
0740
MC/DC0%0/2
$info->{"fallback_provider"} ||= $fall
$info->{"fallback_provider"} $fall
Truth table33%1/3
A ||= B
A= $info->{"fallback_provider"}B= $fall
ABresult
1X1
011
000
101  
102 74 ▶ $item ||= $self->CONFIG_ITEM->new($info);
Condition66%2/3
$item ||= $self->CONFIG_ITEM->new($info)
l!l&&r!l&&!r
15590
MC/DC0%0/2
$item ||= $self->CONFIG_ITEM->new($info)
$item $self->CONFIG_ITEM->new($info)
Truth table66%2/3
A ||= B
A= $itemB= $self->CONFIG_ITEM->new($info)
ABresult
1X1
011
000
103 74 $name = $item->name;
104  
105 74 ▶ next if $hash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{$name} 3 71 74
106  
107 71 $self->debug("generated item: $item") if DEBUG;
108  
109 71 foreach my $alias ($item->names) {
110 # return $self->error_msg( dup_item => $name )
111 # if $hash->{ $name };
112 97 $hash->{ $alias } = $item;
113 }
114 71 $self->debug("adding $name => $item to schema") if DEBUG;
115 71 push(@$list, $item);
116 }
117  
118 21 $self->debug("created schema: ", $self->dump_data($self->{ items }))
119 if DEBUG;
120  
121 21 return $self;
122 }
123  
124  
125 sub fallback {
126 0 ▶ my ($self, $name, $type, $data) = @_;
Subroutine fallback: not called CC=1 SCAR=6.9
Pod undocumented
127 0 return $self->error_msg( bad_fallback => $name, $type );
128 }
129  
130  
131 sub configure {
132 37 ▶ my ($self, $config, $target, $class) = @_;
Subroutine configure: called CC=4 SCAR=14.0
Pod undocumented
133 37 my $items = $self->{ items };
134 37 my ($element, $name, $alias, $code, @args, $ok, $value);
135  
136 37 ▶ $class ||= $target;
Condition33%1/3
$class ||= $target
l!l&&r!l&&!r
3700
MC/DC0%0/2
$class ||= $target
$class $target
Truth table33%1/3
A ||= B
A= $classB= $target
ABresult
1X1
011
000
137  
138 37 $self->debug("configure(", CLASS->dump_data_inline($config), ')') if DEBUG;
139 37 $self->debug("configure element: ", CLASS->dump_data($items)) if DEBUG;
140  
141 37 ELEMENT: foreach $element (@$items) {
142 267 ▶ $element->try->configure($config, $target, $class)
Branch: 1 branch
BranchTrueFalseTotal
$element->try->configure($config, $target, $class) or return $self->decline($element->reason) 1 266 267
143 || return $self->decline($element->reason);
144 }
145  
146 36 return $self;
147 }
148  
149  
150 sub item {
151 0 ▶ my $self = shift;
Subroutine item: not called CC=2 SCAR=17.9
Pod undocumented
152 0 my $item = $self->{ item };
153 return @_
154 0 ▶ ? $item->{ $_[0] }
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
155 : $item;
156 }
157  
158 sub items {
159 20 ▶ my $self = shift;
Subroutine items: called CC=2 SCAR=7.1
Pod undocumented
160 20 my $items = $self->{ items };
161 return wantarray
162 20 ▶ ? @$items
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 20 20
163 : $items;
164  
165  
166 }
167  
168 7 1;
« lib/Badger/Hub.pm ↑ Summary lib/Badger/Filesystem/Visitor.pm »