lib/Badger/Class/Config.pm

filter: Statementstmt 100% 61 / 61 Branchbran 85.0% 17 / 20 Conditioncond 57.1% 4 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 9 / 9 totaltotal 91.2% 104 / 114 CC 14 SCAR 8.7
CC
14
Cov
93%
schema
4.0
configure_method
3.0
configure
3.0
CRAP 2.4
« lib/Badger/Codec/URI.pm ↑ Summary lib/Badger/Filesystem/File.pm »
1 #========================================================================
2 #
3 # Badger::Class::Config
4 #
5 # DESCRIPTION
6 # Class mixin module for adding code onto a class for configuration.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 #========================================================================
12  
13 package Badger::Class::Config;
14  
15 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Badger::Debug ':dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Badger::Config::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 use Badger::Class
19 7 version => 0.01,
20 debug => 0,
21 base => 'Badger::Exporter Badger::Base',
22 import => 'class CLASS',
23 words => 'CONFIG_SCHEMA CONFIG_ITEMS',
24 constants => 'HASH ARRAY DELIMITER',
25 constant => {
26 SCHEMA => 'Badger::Config::Schema',
27 CONFIG_METHOD => 'configure',
28 VALUE => 1,
29 NOTHING => 0,
30 },
31 messages => {
32 bad_type => 'Invalid type prefix specified for %s: %s',
33 bad_method => 'Missing method for the %s %s configuration item: %s',
34 7 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36  
37 sub export {
38 20 ▶ my $class = shift;
Subroutine export: called CC=2 SCAR=6.9
Pod documented
39 20 my $target = shift;
40 20 $class->debug("export to $target: ", join(', ', @_)) if DEBUG;
41 20 ▶ my $params = @_ == 1 ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 13 7 20
42 20 my $schema = $class->schema($target, $params);
43 20 my $items = $schema->items;
44  
45 20 $class->debug(
46 "exporting CONFIG_SCHEMA to $target: $schema"
47 ) if DEBUG;
48  
49 20 $class->export_symbol(
50 $target,
51 CONFIG_SCHEMA,
52 \$schema
53 );
54  
55 20 $class->debug(
56 "export CONFIG_ITEMS to $target: ",
57 $class->dump_data($items)
58 ) if DEBUG;
59  
60 20 $class->export_symbol(
61 $target,
62 CONFIG_ITEMS,
63 \$items,
64 );
65  
66 20 $class->export_symbol(
67 $target,
68 CONFIG_METHOD,
69 $class->can(CONFIG_METHOD) # subclass might redefine method
70 );
71 }
72  
73 sub schema {
74 20 ▶ my $class = shift;
Subroutine schema: called CC=4 SCAR=13.9
Pod documented
75 20 my $target = shift;
76 20 ▶ my $config = @_ == 1 ? (ref $_[0] eq ARRAY ? [@{$_[0]}] : shift) : [ @_ ];
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 ? : 20 0 20
ref $_[0] eq 'ARRAY' ? : 4 16 20
77  
78 20 $class->debug("Generating schema from config: ", $class->dump_data($config))
79 if DEBUG;
80  
81 20 ▶ $config = [ split(DELIMITER, $config) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config 8 12 20
82 unless ref $config;
83  
84 # inherit any other items define in base classes
85 20 my $items = class($target)->list_vars(CONFIG_ITEMS);
86  
87 20 $class->SCHEMA->new(
88 class => $target,
89 schema => $config,
90 fallback => $class,
91 extend => $items,
92 );
93 }
94  
95 sub fallback {
96 40 ▶ my ($self, $name, $type, $data) = @_;
Subroutine fallback: called CC=2 SCAR=7.1
Pod documented
97 40 ▶ my $code = $self->can('configure_' . $type) || return;
Condition50%1/2
$self->can('configure_' . $type) || (return)
l!l
400
MC/DC0%0/1
$self->can('configure_' . $type) || (return)
$self->can('configure_' . $type)
Truth table50%1/2
A
A= $self->can('configure_' . $type)
Aresult
11
00
98 40 return [ $code, $data ];
99 }
100  
101  
102 #-----------------------------------------------------------------------
103 # this method is mixed into the target module
104 #-----------------------------------------------------------------------
105  
106 sub configure {
107 37 ▶ my ($self, $config, $target) = @_;
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
108 37 my $class = class($self);
109 37 my $schema = $class->any_var(CONFIG_SCHEMA);
110  
111 # if a specific $target isn't defined then we default to updating $self
112 37 ▶ $schema->configure($config, $target || $self, $self)
Branch: 1 branch
BranchTrueFalseTotal
$schema->configure($config, $target || $self, $self) or return $self->error($schema->reason->info) 1 36 37
Condition66%2/3
$target || $self
l!l&&r!l&&!r
1360
MC/DC0%0/2
$target || $self
$target $self
Truth table66%2/3
A || B
A= $targetB= $self
ABresult
1X1
011
000
113 || return $self->error($schema->reason->info);
114  
115 36 return $self;
116 }
117  
118  
119 #-----------------------------------------------------------------------
120 # These handlers implement the various fallback types for providing
121 # configuration data. The schema() method maps fallacks specified as
122 # 'pkg:FOO' and 'class:BAR', for example, to the configure_pkg() and
123 # configure_class() handlers, passing the token following the colon as
124 # an argument. They are called as code refs, but the class of the
125 # object that they're configuring is passed as the first argument, $class.
126 # So they look like class methods, but they're not exported into the
127 # object's namespace. The $target is usually the object that's being
128 # configured, e.g. when $self->configure($config) is called, but it might
129 # also be a bare hash, e.g. $target = { }; $self->configure($config, $target)
130 #-----------------------------------------------------------------------
131  
132 # TODO: move these into Badger::Config::Item or somewhere else
133  
134 sub configure_pkg {
135 1 ▶ my ($class, $name, $config, $target, $var) = @_;
Subroutine configure_pkg: called CC=2 SCAR=7.0
Pod documented
136 1 my $value = class($class)->var($var);
137  
138 1 $class->debug(
139 "Looking for \$$var package variable in $class to set $name: ",
140 defined $value ? $value : '<undef>'
141 ) if DEBUG;
142  
143 1 ▶ return defined $value
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 1 1
144 ? (VALUE => $value)
145 : (NOTHING);
146 }
147  
148 sub configure_class {
149 85 ▶ my ($class, $name, $config, $target, $var) = @_;
Subroutine configure_class: called CC=2 SCAR=6.9
Pod documented
150 85 my $value = class($class)->any_var_in( split(':', $var) );
151  
152 85 $class->debug(
153 "Looking for \$$var class variable in $class to set $name: ",
154 defined $value ? $value : '<undef>'
155 ) if DEBUG;
156  
157 85 ▶ return defined $value
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 78 7 85
158 ? (VALUE => $value)
159 : (NOTHING);
160 }
161  
162 sub configure_env {
163 7 ▶ my ($class, $name, $config, $target, $var) = @_;
Subroutine configure_env: called CC=2 SCAR=6.9
Pod documented
164 7 my $value = $ENV{ $var };
165  
166 7 $class->debug(
167 "Looking for $var environment variable to set $name: ",
168 defined $value ? $value : '<undef>'
169 ) if DEBUG;
170  
171 7 ▶ return defined $value
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 6 1 7
172 ? (VALUE => $value)
173 : (NOTHING);
174 }
175  
176 sub configure_method {
177 6 ▶ my ($class, $name, $config, $target, $method) = @_;
Subroutine configure_method: called CC=3 SCAR=11.0
Pod documented
178  
179 # see if the object has the required method - note we must call
180 # error_msg against CLASS (Badger::Class::Config) to use the 'bad_method'
181 # message defined above.
182 6 ▶ my $code = $class->can($method)
Condition50%1/2
$class->can($method) || (return CLASS()->error_msg('bad_method', Badger::Class::Config::class($class), $name, $method))
l!l
60
MC/DC0%0/1
$class->can($method) || (return CLASS()->error_msg('bad_method', Badger::Class::Config::class($class), $name, $method))
$class->can($method)
Truth table50%1/2
A
A= $class->can($method)
Aresult
11
00
183 || return CLASS->error_msg( bad_method => class($class), $name, $method );
184  
185 # call the code and do the usual shuffle
186 6 my $value = $code->($class);
187  
188 6 $class->debug(
189 "Called $method() method to set $name: ",
190 defined $value ? $value : '<undef>'
191 ) if DEBUG;
192  
193 6 ▶ return defined $value
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 5 1 6
194 ? (VALUE => $value)
195 : (NOTHING);
196 }
197  
198 sub configure_target {
199 3 ▶ my ($class, $name, $config, $target, $var) = @_;
Subroutine configure_target: called CC=2 SCAR=7.0
Pod documented
200  
201 3 my $value = $target->{ $var };
202  
203 3 $class->debug(
204 "Looking for $var in $class target $target to set $name: ",
205 defined $value ? $value : '<undef>'
206 ) if DEBUG;
207  
208 3 ▶ return defined $value
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 3 0 3
209 ? (VALUE => $value)
210 : (NOTHING);
211 }
212  
213  
214  
215 7 1;
216  
217 __END__
« lib/Badger/Codec/URI.pm ↑ Summary lib/Badger/Filesystem/File.pm »