lib/Badger/Config/Item.pm

filter: Statementstmt 68.3% 80 / 117 Branchbran 53.7% 29 / 54 Conditioncond 27.6% 13 / 47 MC/DCmcdc 3.8% 1 / 26 Subroutinesub 50.0% 6 / 12 Podpod 0.0% 0 / 10 totaltotal 48.5% 129 / 266 CC 49 SCAR 33.5
CC
49
Cov
56%
args
90.0
summary
30.0
set
27.1
CRAP 28.6
« blib/lib/Badger/Base.pm ↑ Summary blib/lib/Badger/Debug.pm »
1 package Badger::Config::Item;
2  
3 7 ▶use Badger::Debug ':dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 use Badger::Class
5 7 version => 0.01,
6 debug => 0,
7 base => 'Badger::Base',
8 import => 'class CLASS',
9 utils => 'blessed',
10 accessors => 'name arity',
11 constants => 'DELIMITER ARRAY HASH',
12 constant => {
13 ARITY_ITEM => 1,
14 ARITY_LIST => 2,
15 ARITY_HASH => 3,
16 },
17 alias => {
18 init => \&init_item,
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 no_value => 'No value specified for the %s configuration item',
26 no_key_value => 'No value specified for the <2> key of the <1> configuration item',
27 7 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 7 our $ARITY = {
30 '$' => ARITY_ITEM,
31 '@' => ARITY_LIST,
32 '%' => ARITY_HASH,
33 };
34  
35 sub init_item {
36 59 ▶ my ($self, $config) = @_;
Subroutine init_item: called CC=17 SCAR=30.1
Pod undocumented
37 59 my ($name, @aka, $alias, $fallback, $test);
38  
39 59 ▶ my $fall = delete $config->{ fallback_provider } || $self;
Condition33%1/3
delete $config->{'fallback_provider'} || $self
l!l&&r!l&&!r
5900
MC/DC0%0/2
delete $config->{'fallback_provider'} || $self
delete $config->{'fallback_provider'} $self
Truth table33%1/3
A || B
A= delete $config->{'fallback_provider'}B= $self
ABresult
1X1
011
000
40  
41 59 $self->debug("Generating config item: ", $self->dump_data($config))
42 if DEBUG;
43  
44 $name = $config->{ name }
45 59 ▶ || return $self->error_msg( missing => 'name' );
Condition50%1/2
$config->{'name'} || (return $self->error_msg('missing', 'name'))
l!l
590
MC/DC0%0/1
$config->{'name'} || (return $self->error_msg('missing', 'name'))
$config->{'name'}
Truth table50%1/2
A
A= $config->{'name'}
Aresult
11
00
46  
47 # A '!' at the end of the name indicates it's mandatory.
48 # A '=value' at the end indicates a default value.
49 59 ▶ $self->{ required } = ($name =~ s/!$//) ? 1 : $config->{ required };
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/!$// ? : 2 57 59
50 59 ▶ $self->{ default } = ($name =~ s/=(\w\S*)$//) ? $1 : $config->{ default };
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/=(\w\S*)$// ? : 11 48 59
51  
52 # Alternately, '=$XXX', '=@XXX' or '=%XXX' can be used to indicate that
53 # the options takes one 'XXX' argument, multiple 'XXX' arguments or key
54 # values/pairs where the values are 'XXX' arguments
55 59 ▶ if ($name =~ s/=([\$\@\%])(.+)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/=([\$\@\%])(.+)$//) 0 59 59
56 0 $self->debug("config item: $name [$1] [$2]") if DEBUG;
57 0 $config->{ arity } = $ARITY->{ $1 };
58 0 $config->{ args } = $2;
59 }
60  
61  
62 # name can be 'name|alias1|alias2|...'
63 59 ($name, @aka) = split(/\|/, $name);
64  
65 # alias can be specified as hash ref or string
66 59 ▶ $alias = $config->{ alias } || { };
Condition50%1/2
$config->{'alias'} || {}
l!l
059
MC/DC0%0/1
$config->{'alias'} || {}
$config->{'alias'}
Truth table50%1/2
A
A= $config->{'alias'}
Aresult
11
00
67 59 ▶ $alias = [ split(DELIMITER, $alias) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $alias 0 59 59
68 unless ref $alias;
69 59 ▶ $alias = { map { $_ => $name } @$alias }
Branch: 1 branch
BranchTrueFalseTotal
if ref $alias eq "ARRAY" 0 59 59
70 if ref $alias eq ARRAY;
71 59 ▶ return $self->error_msg( invalid => alias => $alias )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $alias eq "HASH" 0 59 59
72 unless ref $alias eq HASH;
73  
74 # aliases, and more generally, fallbacks, can be specified as a list ref
75 # or string which we split
76 59 $self->debug("fallback: ", $self->dump_data($config->{ fallback })) if DEBUG;
77  
78 59 ▶ $fallback = $config->{ fallback } || [ ];
Condition100%2/2
$config->{'fallback'} || []
l!l
752
MC/DC100%1/1
$config->{'fallback'} || []
$config->{'fallback'}
Truth table100%2/2
A
A= $config->{'fallback'}
Aresult
11
00
79 59 ▶ $fallback = [ split(DELIMITER, $fallback) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fallback eq "ARRAY" 1 58 59
80 unless ref $fallback eq ARRAY;
81 59 push(@$fallback, @aka);
82  
83 59 $self->debug("fallbacks: ", $self->dump_data($fallback)) if DEBUG;
84  
85 59 foreach my $item (@$fallback) {
86 62 ▶ unless ($item =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item =~ /:/) 22 40 62
87 22 $alias->{ $item } = $name;
88 22 next;
89 }
90 40 my ($type, $data) = split(/:/, $item, 2);
91 40 ▶ $item = $fall->fallback($name, $type, $data)
Condition50%1/2
$fall->fallback($name, $type, $data) || (return $self->error_msg('bad_type', $name, $type))
l!l
400
MC/DC0%0/1
$fall->fallback($name, $type, $data) || (return $self->error_msg('bad_type', $name, $type))
$fall->fallback($name, $type, $data)
Truth table50%1/2
A
A= $fall->fallback($name, $type, $data)
Aresult
11
00
92 || return $self->error_msg( bad_type => $name, $type );
93 }
94  
95 # add any aliases specified as part of the name and bind them
96 # back into the field info hash
97 59 $self->{ fallback } = $fallback;
98  
99 # this is getting way too large... but I just want to get things working
100 # before I start paring things down
101 59 $self->{ name } = $name;
102 59 $self->{ alias } = $alias;
103 59 ▶ $self->{ message } = $config->{ message } || $config->{ error };
Condition33%1/3
$config->{'message'} || $config->{'error'}
l!l&&r!l&&!r
0059
MC/DC0%0/2
$config->{'message'} || $config->{'error'}
$config->{'message'} $config->{'error'}
Truth table33%1/3
A || B
A= $config->{'message'}B= $config->{'error'}
ABresult
1X1
011
000
104 59 $self->{ action } = $config->{ action };
105 59 $self->{ method } = $config->{ method };
106 59 $self->{ about } = $config->{ about };
107 59 $self->{ args } = $config->{ args };
108 59 ▶ $self->{ arity } = $config->{ arity } || 0;
Condition50%1/2
$config->{'arity'} || 0
l!l
059
MC/DC0%0/1
$config->{'arity'} || 0
$config->{'arity'}
Truth table50%1/2
A
A= $config->{'arity'}
Aresult
11
00
109  
110 59 $self->debug(
111 "Configured configuration item: ", $self->dump
112 ) if DEBUG;
113  
114 59 return $self;
115 }
116  
117  
118 sub fallback {
119 0 ▶ shift->not_implemented;
Subroutine fallback: not called CC=1 SCAR=6.9
Pod undocumented
120 }
121  
122 sub names {
123 71 ▶ my $self = shift;
Subroutine names: called CC=2 SCAR=7.0
Pod undocumented
124 71 my @names = ($self->{ name }, keys %{ $self->{ alias } });
125 return wantarray
126 ? @names
127 71 ▶ : \@names;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 71 0 71
128 }
129  
130  
131 sub configure {
132 267 ▶ my ($self, $config, $target, $class) = @_;
Subroutine configure: called CC=11 SCAR=24.2
Pod undocumented
133 267 my ($name, $alias, $code, @args, $ok, $value);
134  
135 267 ▶ $class ||= $target;
Condition33%1/3
$class ||= $target
l!l&&r!l&&!r
26700
MC/DC0%0/2
$class ||= $target
$class $target
Truth table33%1/3
A ||= B
A= $classB= $target
ABresult
1X1
011
000
136  
137 267 $self->debug("configure(", CLASS->dump_data_inline($config), ')') if DEBUG;
138 267 $self->debug("item is ", $self->dump_data($self)) if DEBUG;
139 # $self->debug("items: ", CLASS->dump_data($items)) if DEBUG;
140  
141 267 $name = $self->{ name };
142  
143 # TODO: abstract out action calls.
144  
145 267 ▶ FALLBACK: foreach $alias ($name, @{ $self->{ fallback } || [ ] }) {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fallback"} or [] 0 267 267
146 495 ▶ next unless defined $alias;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alias 0 495 495
147  
148 495 ▶ if (ref $alias eq ARRAY) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $alias eq 'ARRAY') { } 102 393 495
elsif (defined $config->{$alias}) { } 71 322 393
149 102 ($code, @args) = @$alias;
150 #$self->todo('calling code');
151 102 ($ok, $value) = $code->($class, $name, $config, $target, @args);
152 102 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 92 10 102
153 92 return $self->set($target, $name, $value, $class);
154 }
155 }
156 elsif (defined $config->{ $alias }) {
157 71 $self->debug("Found value for $name ($alias): $config->{ $alias }\n") if DEBUG;
158 71 return $self->set($target, $name, $config->{ $alias }, $class);
159 }
160 else {
161 322 $self->debug("Nothing found for $alias to set $name\n") if DEBUG;
162 }
163 }
164  
165 104 ▶ if (defined $self->{ default }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"default"}) 13 91 104
166 13 $self->debug("setting to default value: $self->{ default }\n") if DEBUG;
167 13 return $self->set($target, $name, $self->{ default }, $class);
168 }
169  
170 91 ▶ if ($self->{ required }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"required"}) 1 90 91
171 1 $self->debug("$name is required, throwing error\n") if DEBUG;
172 1 ▶ return $self->error_msg( $self->{ message } || missing => $name );
Condition50%1/2
$self->{'message'} || 'missing'
l!l
01
MC/DC0%0/1
$self->{'message'} || 'missing'
$self->{'message'}
Truth table50%1/2
A
A= $self->{'message'}
Aresult
11
00
173 }
174  
175 90 return $self;
176 }
177  
178  
179 sub set {
180 176 ▶ my ($self, $target, $name, $value, $object) = @_;
Subroutine set: called CC=10 SCAR=33.0
Pod undocumented
181 176 my $method;
182  
183 176 ▶ $object ||= $target;
Condition33%1/3
$object ||= $target
l!l&&r!l&&!r
17600
MC/DC0%0/2
$object ||= $target
$object $target
Truth table33%1/3
A ||= B
A= $objectB= $target
ABresult
1X1
011
000
184  
185 176 $self->debug("set($target, $name, $value)") if DEBUG;
186  
187 176 ▶ if ($self->{ arity } == ARITY_LIST) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'arity'} == 2) { } 0 176 176
elsif ($self->{'arity'} == 3) { } 0 176 176
188 0 ▶ my $list = $target->{ $name } ||= [ ];
Condition0%0/2
$target->{$name} ||= []
l!l
00
MC/DC0%0/1
$target->{$name} ||= []
$target->{$name}
Truth table0%0/2
A
A= $target->{$name}
Aresult
11
00
189 0 push(@$list, $value);
190 }
191 elsif ($self->{ arity } == ARITY_HASH) {
192 0 ▶ return $self->error_msg( invalid => 'key/value pair' => $value)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 0 0 0
193 unless ref $value eq ARRAY;
194  
195 0 ▶ my $hash = $target->{ $name } ||= { };
Condition0%0/2
$target->{$name} ||= {}
l!l
00
MC/DC0%0/1
$target->{$name} ||= {}
$target->{$name}
Truth table0%0/2
A
A= $target->{$name}
Aresult
11
00
196 0 $hash->{ $value->[0] } = $value->[1];
197 }
198 else {
199 176 $target->{ $name } = $value;
200 }
201  
202 176 ▶ $self->{ action }->($self, $name, $value) if $self->{ action };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"action"} 0 176 176
203  
204 176 ▶ if (blessed($object) && ($method = $self->{ method })) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($object) and $method = $self->{"method"}) 0 176 176
Condition66%2/3
builtin::blessed($object) and $method = $self->{"method"}
!ll&&!rl&&r
91670
MC/DC0%0/2
builtin::blessed($object) and $method = $self->{"method"}
builtin::blessed($object) $method = $self->{"method"}
Truth table66%2/3
A and B
A= builtin::blessed($object)B= $method = $self->{"method"}
ABresult
0X0
100
111
205 0 $self->debug("calling method $method on object $object\n") if DEBUG;
206 0 $object->$method($name, $value);
207 }
208  
209 176 return $self;
210 }
211  
212  
213 # this is being replaced by Badger::Config::Reader::Args
214  
215 sub args {
216 0 ▶ my $self = shift;
Subroutine args: not called CC=9 SCAR=45.0
Pod undocumented
217 0 my $args = shift;
218 0 my $value;
219  
220 0 ▶ if ($self->{ args }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'args'}) { } 0 0 0
221 0 $self->debug("looking for $self->{ name } arg in ", $self->dump_data($args)) if DEBUG;
222  
223 return $self->error_msg( no_value => $self->{ name } )
224 0 ▶ unless @$args && defined $args->[0] && $args->[0] !~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args and defined $args->[0] and not $args->[0] =~ /^-/ 0 0 0
Condition0%0/6
@$args and defined $args->[0] and not $args->[0] =~ /^-/
!ll&&!rl&&r
000
@$args and defined $args->[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@$args and defined $args->[0] and not $args->[0] =~ /^-/
@$args defined $args->[0] not $args->[0] =~ /^-/
Truth table0%0/4
A and B and not(C)
A= @$argsB= defined $args->[0]C= not $args->[0] =~ /^-/
ABCresult
0XX0
10X0
1100
1111
225  
226 0 $value = shift @$args;
227  
228 0 ▶ if ($self->{ arity } == ARITY_HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"arity"} == 3) 0 0 0
229 0 my $key = $value;
230 0 ▶ return $self->error_msg( no_key_value => $self->{ name }, $key )
Branch: 1 branch
BranchTrueFalseTotal
unless @$args and defined $args->[0] and not $args->[0] =~ /^-/ 0 0 0
Condition0%0/6
@$args and defined $args->[0] and not $args->[0] =~ /^-/
!ll&&!rl&&r
000
@$args and defined $args->[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@$args and defined $args->[0] and not $args->[0] =~ /^-/
@$args defined $args->[0] not $args->[0] =~ /^-/
Truth table0%0/4
A and B and not(C)
A= @$argsB= defined $args->[0]C= not $args->[0] =~ /^-/
ABCresult
0XX0
10X0
1100
1111
231 unless @$args && defined $args->[0] && $args->[0] !~ /^-/;
232 0 $value = shift @$args;
233 0 $value = [ $key, $value ];
234 }
235 }
236 else {
237 0 $value = 1;
238 }
239 # this is all the wrong way around - quick hack
240 0 return $self->configure({ $self->{ name } => $value }, @_);
241 }
242  
243 # temporary method providing access to args value
244 sub has_args {
245 0 ▶ shift->{ args };
Subroutine has_args: not called CC=1 SCAR=6.9
Pod undocumented
246 }
247  
248 sub hash_arity {
249 0 ▶ shift->{ arity } == ARITY_HASH;
Subroutine hash_arity: not called CC=1 SCAR=6.9
Pod undocumented
250 }
251  
252 sub list_arity {
253 0 ▶ shift->{ arity } == ARITY_LIST;
Subroutine list_arity: not called CC=1 SCAR=6.9
Pod undocumented
254 }
255  
256 sub summary {
257 0 ▶ my ($self, $reporter) = @_;
Subroutine summary: not called CC=5 SCAR=34.0
Pod undocumented
258 0 my $name = $self->{ name };
259 0 ▶ my $args = $self->{ args } || '';
Condition0%0/2
$self->{'args'} || ''
l!l
00
MC/DC0%0/1
$self->{'args'} || ''
$self->{'args'}
Truth table0%0/2
A
A= $self->{'args'}
Aresult
11
00
260 0 ▶ my $about = $self->{ about } || '';
Condition0%0/2
$self->{'about'} || ''
l!l
00
MC/DC0%0/1
$self->{'about'} || ''
$self->{'about'}
Truth table0%0/2
A
A= $self->{'about'}
Aresult
11
00
261 0 ▶ if (length $args) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $args) 0 0 0
262 0 $args =~ s/\s+/> </g;
263 0 $args = " <$args>";
264 }
265 0 ▶ return $reporter
Branch: 1 branch
BranchTrueFalseTotal
$reporter ? : 0 0 0
266 ? $reporter->option( $name.$args, $about )
267 : sprintf('--%-20s %s', $name.$args, $about);
268 }
269  
270  
271 7 1;
« blib/lib/Badger/Base.pm ↑ Summary blib/lib/Badger/Debug.pm »