| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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 |
|
10
|
|
|
|
|
|
|
# |
|
11
|
|
|
|
|
|
|
#======================================================================== |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package Badger::Class::Config; |
|
14
|
|
|
|
|
|
|
|
|
15
|
7
|
|
|
7
|
|
1019
|
use Carp; |
|
|
7
|
|
|
|
|
12
|
|
|
|
7
|
|
|
|
|
479
|
|
|
16
|
7
|
|
|
7
|
|
40
|
use Badger::Debug ':dump'; |
|
|
7
|
|
|
|
|
13
|
|
|
|
7
|
|
|
|
|
40
|
|
|
17
|
7
|
|
|
7
|
|
2452
|
use Badger::Config::Schema; |
|
|
7
|
|
|
|
|
17
|
|
|
|
7
|
|
|
|
|
387
|
|
|
18
|
|
|
|
|
|
|
use Badger::Class |
|
19
|
7
|
|
|
|
|
67
|
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
|
|
|
7
|
|
43
|
}; |
|
|
7
|
|
|
|
|
14
|
|
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub export { |
|
38
|
20
|
|
|
20
|
1
|
34
|
my $class = shift; |
|
39
|
20
|
|
|
|
|
25
|
my $target = shift; |
|
40
|
20
|
|
|
|
|
23
|
$class->debug("export to $target: ", join(', ', @_)) if DEBUG; |
|
41
|
20
|
100
|
|
|
|
60
|
my $params = @_ == 1 ? shift : { @_ }; |
|
42
|
20
|
|
|
|
|
48
|
my $schema = $class->schema($target, $params); |
|
43
|
20
|
|
|
|
|
74
|
my $items = $schema->items; |
|
44
|
|
|
|
|
|
|
|
|
45
|
20
|
|
|
|
|
24
|
$class->debug( |
|
46
|
|
|
|
|
|
|
"exporting CONFIG_SCHEMA to $target: $schema" |
|
47
|
|
|
|
|
|
|
) if DEBUG; |
|
48
|
|
|
|
|
|
|
|
|
49
|
20
|
|
|
|
|
94
|
$class->export_symbol( |
|
50
|
|
|
|
|
|
|
$target, |
|
51
|
|
|
|
|
|
|
CONFIG_SCHEMA, |
|
52
|
|
|
|
|
|
|
\$schema |
|
53
|
|
|
|
|
|
|
); |
|
54
|
|
|
|
|
|
|
|
|
55
|
20
|
|
|
|
|
29
|
$class->debug( |
|
56
|
|
|
|
|
|
|
"export CONFIG_ITEMS to $target: ", |
|
57
|
|
|
|
|
|
|
$class->dump_data($items) |
|
58
|
|
|
|
|
|
|
) if DEBUG; |
|
59
|
|
|
|
|
|
|
|
|
60
|
20
|
|
|
|
|
56
|
$class->export_symbol( |
|
61
|
|
|
|
|
|
|
$target, |
|
62
|
|
|
|
|
|
|
CONFIG_ITEMS, |
|
63
|
|
|
|
|
|
|
\$items, |
|
64
|
|
|
|
|
|
|
); |
|
65
|
|
|
|
|
|
|
|
|
66
|
20
|
|
|
|
|
169
|
$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
|
|
|
20
|
1
|
26
|
my $class = shift; |
|
75
|
20
|
|
|
|
|
23
|
my $target = shift; |
|
76
|
20
|
100
|
|
|
|
63
|
my $config = @_ == 1 ? (ref $_[0] eq ARRAY ? [@{$_[0]}] : shift) : [ @_ ]; |
|
|
4
|
50
|
|
|
|
13
|
|
|
77
|
|
|
|
|
|
|
|
|
78
|
20
|
|
|
|
|
23
|
$class->debug("Generating schema from config: ", $class->dump_data($config)) |
|
79
|
|
|
|
|
|
|
if DEBUG; |
|
80
|
|
|
|
|
|
|
|
|
81
|
20
|
100
|
|
|
|
135
|
$config = [ split(DELIMITER, $config) ] |
|
82
|
|
|
|
|
|
|
unless ref $config; |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# inherit any other items define in base classes |
|
85
|
20
|
|
|
|
|
48
|
my $items = class($target)->list_vars(CONFIG_ITEMS); |
|
86
|
|
|
|
|
|
|
|
|
87
|
20
|
|
|
|
|
122
|
$class->SCHEMA->new( |
|
88
|
|
|
|
|
|
|
class => $target, |
|
89
|
|
|
|
|
|
|
schema => $config, |
|
90
|
|
|
|
|
|
|
fallback => $class, |
|
91
|
|
|
|
|
|
|
extend => $items, |
|
92
|
|
|
|
|
|
|
); |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub fallback { |
|
96
|
41
|
|
|
41
|
1
|
71
|
my ($self, $name, $type, $data) = @_; |
|
97
|
41
|
|
50
|
|
|
192
|
my $code = $self->can('configure_' . $type) || return; |
|
98
|
41
|
|
|
|
|
148
|
return [ $code, $data ]; |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
|
103
|
|
|
|
|
|
|
# this method is mixed into the target module |
|
104
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub configure { |
|
107
|
38
|
|
|
38
|
1
|
94
|
my ($self, $config, $target) = @_; |
|
108
|
38
|
|
|
|
|
85
|
my $class = class($self); |
|
109
|
38
|
|
|
|
|
121
|
my $schema = $class->any_var(CONFIG_SCHEMA); |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# if a specific $target isn't defined then we default to updating $self |
|
112
|
38
|
100
|
66
|
|
|
225
|
$schema->configure($config, $target || $self, $self) |
|
113
|
|
|
|
|
|
|
|| return $self->error($schema->reason->info); |
|
114
|
|
|
|
|
|
|
|
|
115
|
37
|
|
|
|
|
80
|
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
|
|
|
1
|
1
|
4
|
my ($class, $name, $config, $target, $var) = @_; |
|
136
|
1
|
|
|
|
|
4
|
my $value = class($class)->var($var); |
|
137
|
|
|
|
|
|
|
|
|
138
|
1
|
|
|
|
|
2
|
$class->debug( |
|
139
|
|
|
|
|
|
|
"Looking for \$$var package variable in $class to set $name: ", |
|
140
|
|
|
|
|
|
|
defined $value ? $value : '' |
|
141
|
|
|
|
|
|
|
) if DEBUG; |
|
142
|
|
|
|
|
|
|
|
|
143
|
1
|
50
|
|
|
|
16
|
return defined $value |
|
144
|
|
|
|
|
|
|
? (VALUE => $value) |
|
145
|
|
|
|
|
|
|
: (NOTHING); |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub configure_class { |
|
149
|
89
|
|
|
89
|
1
|
156
|
my ($class, $name, $config, $target, $var) = @_; |
|
150
|
89
|
|
|
|
|
174
|
my $value = class($class)->any_var_in( split(':', $var) ); |
|
151
|
|
|
|
|
|
|
|
|
152
|
89
|
|
|
|
|
147
|
$class->debug( |
|
153
|
|
|
|
|
|
|
"Looking for \$$var class variable in $class to set $name: ", |
|
154
|
|
|
|
|
|
|
defined $value ? $value : '' |
|
155
|
|
|
|
|
|
|
) if DEBUG; |
|
156
|
|
|
|
|
|
|
|
|
157
|
89
|
100
|
|
|
|
285
|
return defined $value |
|
158
|
|
|
|
|
|
|
? (VALUE => $value) |
|
159
|
|
|
|
|
|
|
: (NOTHING); |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub configure_env { |
|
163
|
7
|
|
|
7
|
1
|
13
|
my ($class, $name, $config, $target, $var) = @_; |
|
164
|
7
|
|
|
|
|
13
|
my $value = $ENV{ $var }; |
|
165
|
|
|
|
|
|
|
|
|
166
|
7
|
|
|
|
|
8
|
$class->debug( |
|
167
|
|
|
|
|
|
|
"Looking for $var environment variable to set $name: ", |
|
168
|
|
|
|
|
|
|
defined $value ? $value : '' |
|
169
|
|
|
|
|
|
|
) if DEBUG; |
|
170
|
|
|
|
|
|
|
|
|
171
|
7
|
100
|
|
|
|
22
|
return defined $value |
|
172
|
|
|
|
|
|
|
? (VALUE => $value) |
|
173
|
|
|
|
|
|
|
: (NOTHING); |
|
174
|
|
|
|
|
|
|
} |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub configure_method { |
|
177
|
7
|
|
|
7
|
1
|
18
|
my ($class, $name, $config, $target, $method) = @_; |
|
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
|
7
|
|
50
|
|
|
81
|
my $code = $class->can($method) |
|
183
|
|
|
|
|
|
|
|| return CLASS->error_msg( bad_method => class($class), $name, $method ); |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# call the code and do the usual shuffle |
|
186
|
7
|
|
|
|
|
25
|
my $value = $code->($class); |
|
187
|
|
|
|
|
|
|
|
|
188
|
7
|
|
|
|
|
12
|
$class->debug( |
|
189
|
|
|
|
|
|
|
"Called $method() method to set $name: ", |
|
190
|
|
|
|
|
|
|
defined $value ? $value : '' |
|
191
|
|
|
|
|
|
|
) if DEBUG; |
|
192
|
|
|
|
|
|
|
|
|
193
|
7
|
100
|
|
|
|
31
|
return defined $value |
|
194
|
|
|
|
|
|
|
? (VALUE => $value) |
|
195
|
|
|
|
|
|
|
: (NOTHING); |
|
196
|
|
|
|
|
|
|
} |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub configure_target { |
|
199
|
3
|
|
|
3
|
1
|
6
|
my ($class, $name, $config, $target, $var) = @_; |
|
200
|
|
|
|
|
|
|
|
|
201
|
3
|
|
|
|
|
7
|
my $value = $target->{ $var }; |
|
202
|
|
|
|
|
|
|
|
|
203
|
3
|
|
|
|
|
4
|
$class->debug( |
|
204
|
|
|
|
|
|
|
"Looking for $var in $class target $target to set $name: ", |
|
205
|
|
|
|
|
|
|
defined $value ? $value : '' |
|
206
|
|
|
|
|
|
|
) if DEBUG; |
|
207
|
|
|
|
|
|
|
|
|
208
|
3
|
50
|
|
|
|
10
|
return defined $value |
|
209
|
|
|
|
|
|
|
? (VALUE => $value) |
|
210
|
|
|
|
|
|
|
: (NOTHING); |
|
211
|
|
|
|
|
|
|
} |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
1; |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
__END__ |