Class: Anyway::Config
Overview
Base config class
Provides attr_config method to describe
configuration parameters and set defaults
Defined Under Namespace
Classes: BlockCallback, Error, NamedCallback, ValidationError
Constant Summary
collapse
- PARAM_NAME =
/^[a-z_](\w+)?$/
- RESERVED_NAMES =
List of names that couldn't be used as config names
(the class instance methods we use)
%i[
config_name
env_prefix
as_env
values
class
clear
deconstruct_keys
dig
dup
initialize
load
load_from_sources
option_parser
pretty_print
raise_validation_error
reload
resolve_config_path
tap
to_h
to_source_trace
write_config_attr
__type_caster__
].freeze
RBSGenerator::TYPE_TO_CLASS
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
to_rbs
for
describe_options, extend_options, flag_options, ignore_options, option_parser_descriptors, option_parser_extensions, option_parser_options
included
included, #option_parser, #parse_options!
Constructor Details
#initialize(overrides) ⇒ void
#initialize(arg0) ⇒ void
Instantiate config instance.
Example:
my_config = Anyway::Config.new()
# provide some values explicitly
my_config = Anyway::Config.new({some: :value})
337
338
339
340
341
342
343
344
345
346
|
# File 'lib/anyway/config.rb', line 337
def initialize(overrides = nil)
@config_name = self.class.config_name
raise ArgumentError, "Config name is missing" unless @config_name
@env_prefix = self.class.env_prefix
@values = {}
load(overrides)
end
|
Class Attribute Details
.configuration_sources ⇒ Array[Symbol]?
263
264
265
266
267
268
|
# File 'lib/anyway/config.rb', line 263
def configuration_sources
return @configuration_sources if instance_variable_defined?(:@configuration_sources)
return @configuration_sources = superclass.configuration_sources.dup if superclass.respond_to?(:configuration_sources)
@configuration_sources = nil
end
|
Instance Attribute Details
#config_name ⇒ String
Returns the value of attribute config_name.
326
327
328
|
# File 'lib/anyway/config.rb', line 326
def config_name
@config_name
end
|
#env_prefix ⇒ String
Returns the value of attribute env_prefix.
326
327
328
|
# File 'lib/anyway/config.rb', line 326
def env_prefix
@env_prefix
end
|
#values ⇒ Hash[untyped, untyped]
Returns the value of attribute values.
456
457
458
|
# File 'lib/anyway/config.rb', line 456
def values
@values
end
|
Class Method Details
.attr_config(*args, **hargs) ⇒ void
This method returns an undefined value.
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/anyway/config.rb', line 81
def attr_config(*args, **hargs)
new_defaults = hargs.deep_dup
new_defaults.stringify_keys!
defaults.merge! new_defaults
new_keys = ((args + new_defaults.keys) - config_attributes)
validate_param_names! new_keys.map(&:to_s)
new_keys.map!(&:to_sym)
unless (reserved_names = (new_keys & RESERVED_NAMES)).empty?
raise ArgumentError, "Can not use the following reserved names as config attrubutes: " \
"#{reserved_names.sort.join(", ")}"
end
config_attributes.push(*new_keys)
define_config_accessor(*new_keys)
new_defaults.each do |key, val|
next unless val.is_a?(TrueClass) || val.is_a?(FalseClass)
alias_method :"#{key}?", :"#{key}"
end
end
|
.coerce_types(mapping) ⇒ void
This method returns an undefined value.
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/anyway/config.rb', line 215
def coerce_types(mapping)
Utils.deep_merge!(coercion_mapping, mapping)
mapping.each do |key, val|
type = val.is_a?(::Hash) ? val[:type] : val
next if type != :boolean
alias_method :"#{key}?", :"#{key}"
end
end
|
.coercion_mapping ⇒ Hash[untyped, untyped]?
226
227
228
229
230
231
232
233
234
|
# File 'lib/anyway/config.rb', line 226
def coercion_mapping
return @coercion_mapping if instance_variable_defined?(:@coercion_mapping)
@coercion_mapping = if superclass < Anyway::Config
superclass.coercion_mapping.deep_dup
else
{}
end
end
|
.config_attributes ⇒ Array[Symbol]?
120
121
122
123
124
125
126
127
128
|
# File 'lib/anyway/config.rb', line 120
def config_attributes
return @config_attributes if instance_variable_defined?(:@config_attributes)
@config_attributes = if superclass < Anyway::Config
superclass.config_attributes.dup
else
[]
end
end
|
.config_name(val = nil) ⇒ String?
170
171
172
173
174
175
176
|
# File 'lib/anyway/config.rb', line 170
def config_name(val = nil)
return (@explicit_config_name = val.to_s) unless val.nil?
return @config_name if instance_variable_defined?(:@config_name)
@config_name = explicit_config_name || build_config_name
end
|
.defaults ⇒ Hash[String, untyped]
110
111
112
113
114
115
116
117
118
|
# File 'lib/anyway/config.rb', line 110
def defaults
return @defaults if instance_variable_defined?(:@defaults)
@defaults = if superclass < Anyway::Config
superclass.defaults.deep_dup
else
new_empty_config
end
end
|
.disable_auto_cast! ⇒ void
This method returns an undefined value.
259
260
261
|
# File 'lib/anyway/config.rb', line 259
def disable_auto_cast!
@fallback_type_caster = ::Anyway::NoCast
end
|
.env_prefix(val = nil) ⇒ String
189
190
191
192
193
194
195
196
197
198
199
|
# File 'lib/anyway/config.rb', line 189
def env_prefix(val = nil)
return (@env_prefix = val.to_s.upcase) unless val.nil?
return @env_prefix if instance_variable_defined?(:@env_prefix)
@env_prefix = if superclass < Anyway::Config && superclass.explicit_config_name?
superclass.env_prefix
else
config_name.upcase
end
end
|
.explicit_config_name ⇒ Object
178
179
180
181
182
183
184
185
|
# File 'lib/anyway/config.rb', line 178
def explicit_config_name
return @explicit_config_name if instance_variable_defined?(:@explicit_config_name)
@explicit_config_name =
if superclass.respond_to?(:explicit_config_name)
superclass.explicit_config_name
end
end
|
.explicit_config_name? ⇒ Boolean
187
|
# File 'lib/anyway/config.rb', line 187
def explicit_config_name?() = !explicit_config_name.nil?
|
.fallback_type_caster(val = nil) ⇒ Object
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/anyway/config.rb', line 247
def fallback_type_caster(val = nil)
return (@fallback_type_caster = val) unless val.nil?
return @fallback_type_caster if instance_variable_defined?(:@fallback_type_caster)
@fallback_type_caster = if superclass < Anyway::Config
superclass.fallback_type_caster.deep_dup
else
::Anyway::AutoCast
end
end
|
.load_callbacks ⇒ Object
160
161
162
163
164
165
166
167
168
|
# File 'lib/anyway/config.rb', line 160
def load_callbacks
return @load_callbacks if instance_variable_defined?(:@load_callbacks)
@load_callbacks = if superclass <= Anyway::Config
superclass.load_callbacks.dup
else
[]
end
end
|
.loader_options(val = nil) ⇒ Object
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/anyway/config.rb', line 201
def loader_options(val = nil)
return (@loader_options = val) unless val.nil?
return @loader_options if instance_variable_defined?(:@loader_options)
@loader_options = if superclass < Anyway::Config
superclass.loader_options
else
{}
end
end
|
.new_empty_config ⇒ Object
.on_load(*names, &block) ⇒ void
This method returns an undefined value.
150
151
152
153
154
155
156
157
158
|
# File 'lib/anyway/config.rb', line 150
def on_load(*names, &block)
raise ArgumentError, "Either methods or block should be specified, not both" if block && !names.empty?
if block
load_callbacks << BlockCallback.new(block)
else
load_callbacks.push(*names.map { NamedCallback.new(it) })
end
end
|
.required(*names, env: nil, **nested) ⇒ void
This method returns an undefined value.
130
131
132
133
134
135
136
137
138
|
# File 'lib/anyway/config.rb', line 130
def required(*names, env: nil, **nested)
unknown_names = names + nested.keys - config_attributes
raise ArgumentError, "Unknown config param: #{unknown_names.join(",")}" if unknown_names.any?
return unless Settings.matching_env?(env)
required_attributes.push(*names)
required_attributes.push(*nested.flatten_names)
end
|
.required_attributes ⇒ Array[Symbol]
140
141
142
143
144
145
146
147
148
|
# File 'lib/anyway/config.rb', line 140
def required_attributes
return @required_attributes if instance_variable_defined?(:@required_attributes)
@required_attributes = if superclass < Anyway::Config
superclass.required_attributes.dup
else
[]
end
end
|
.type_caster(val = nil) ⇒ Object
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/anyway/config.rb', line 236
def type_caster(val = nil)
return @type_caster unless val.nil?
@type_caster ||=
if coercion_mapping.empty?
fallback_type_caster
else
::Anyway::TypeCaster.new(coercion_mapping, fallback: fallback_type_caster)
end
end
|
Instance Method Details
#as_env ⇒ Hash[String, String]
450
451
452
|
# File 'lib/anyway/config.rb', line 450
def as_env
Env.from_hash(to_h, prefix: env_prefix)
end
|
#clear ⇒ void
This method returns an undefined value.
354
355
356
357
358
|
# File 'lib/anyway/config.rb', line 354
def clear
values.clear
@__trace__ = nil
self
end
|
#deconstruct_keys(keys) ⇒ Hash[untyped, untyped]
427
|
# File 'lib/anyway/config.rb', line 427
def deconstruct_keys(keys) = values.deconstruct_keys(keys)
|
#dig ⇒ Object
410
|
# File 'lib/anyway/config.rb', line 410
def dig(*) = values.dig(*)
|
414
415
416
417
418
419
420
421
|
# File 'lib/anyway/config.rb', line 414
def dup
self.class.allocate.tap do |new_config|
%i[config_name env_prefix __trace__].each do |ivar|
new_config.instance_variable_set(:"@#{ivar}", send(ivar).dup)
end
new_config.instance_variable_set(:@values, values.deep_dup)
end
end
|
#flatten_hash ⇒ Hash[String, String]
110
|
# File 'sig/anyway_config.rbs', line 110
def flatten_hash: (Hash[untyped, untyped], String, Hash[String, String]) -> Hash[String, String]
|
#inspect ⇒ String
431
432
433
434
|
# File 'lib/anyway/config.rb', line 431
def inspect
"#<#{self.class}:0x#{vm_object_id.rjust(16, "0")} config_name=\"#{config_name}\" env_prefix=\"#{env_prefix}\" " \
"values=#{values.inspect}>"
end
|
#load(overrides) ⇒ Config
#load(arg0) ⇒ Config
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
# File 'lib/anyway/config.rb', line 360
def load(overrides = nil)
base_config = self.class.defaults.deep_dup
trace = Tracing.capture do
Tracing.trace!(:defaults) { base_config }
config_path = resolve_config_path(config_name, env_prefix)
load_from_sources(
base_config,
name: config_name,
env_prefix:,
config_path:,
**self.class.loader_options
)
if overrides
Tracing.trace!(:load) { overrides }
Utils.deep_merge!(base_config, overrides)
end
end
base_config.each do |key, val|
write_config_attr(key.to_sym, val)
end
trace&.keep_if { |key| self.class.config_attributes.include?(key.to_sym) }
self.class.load_callbacks.each { it.apply_to(self) }
@__trace__ = trace
self
end
|
#load_from_sources(base_config, **opts) ⇒ Object
400
401
402
403
404
405
406
407
408
|
# File 'lib/anyway/config.rb', line 400
def load_from_sources(base_config, **opts)
source_filter = self.class.configuration_sources
Anyway.loaders.each do |(id, loader)|
next if source_filter && !source_filter.include?(id)
Utils.deep_merge!(base_config, loader.call(**opts))
end
base_config
end
|
#pretty_print(q) ⇒ Object
436
437
438
439
440
441
442
443
444
445
446
447
448
|
# File 'lib/anyway/config.rb', line 436
def pretty_print(q)
q.object_group self do
q.nest(1) do
q.breakable
q.text "config_name=#{config_name.inspect}"
q.breakable
q.text "env_prefix=#{env_prefix.inspect}"
q.breakable
q.text "values:"
q.pp __trace__
end
end
end
|
#raise_validation_error(msg) ⇒ void
This method returns an undefined value.
478
479
480
|
# File 'lib/anyway/config.rb', line 478
def raise_validation_error(msg)
raise ValidationError, msg
end
|
#reload(overrides = nil) ⇒ Config
348
349
350
351
352
|
# File 'lib/anyway/config.rb', line 348
def reload(overrides = nil)
clear
load(overrides)
self
end
|
#resolve_config_path(name, env_prefix) ⇒ Object
#to_h ⇒ Hash[untyped, untyped]
412
|
# File 'lib/anyway/config.rb', line 412
def to_h() = values.deep_dup.deep_freeze
|
#to_source_trace ⇒ Hash[String, untyped]
429
|
# File 'lib/anyway/config.rb', line 429
def to_source_trace() = __trace__&.to_h
|