Class: StandardConfig::ConfigProvider
- Inherits:
-
Object
- Object
- StandardConfig::ConfigProvider
show all
- Defined in:
- lib/standard_config/config_provider.rb
Instance Method Summary
collapse
Constructor Details
#initialize(scope_name, resolver_proc, schema = nil) ⇒ ConfigProvider
Returns a new instance of ConfigProvider.
5
6
7
8
9
|
# File 'lib/standard_config/config_provider.rb', line 5
def initialize(scope_name, resolver_proc, schema = nil)
@scope_name = scope_name
@resolver_proc = resolver_proc
@schema = schema
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/standard_config/config_provider.rb', line 11
def method_missing(method_name, *args)
if method_name.to_s.end_with?("=")
field_name = method_name.to_s.chomp("=").to_sym
validate_field!(field_name)
config_object = @resolver_proc.call
if config_object.respond_to?(method_name)
config_object.send(method_name, args.first)
elsif config_object.respond_to?(:[]=)
value = args.first
config_object[field_name] = value
begin
config_object[field_name.to_s] = value
rescue StandardError
end
else
raise NoMethodError, "Configuration object doesn't support setting #{field_name}"
end
else
get_field(method_name)
end
end
|
Instance Method Details
#get_field(field_name) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/standard_config/config_provider.rb', line 39
def get_field(field_name)
validate_field!(field_name)
config_object = @resolver_proc.call
raw_value = if config_object.respond_to?(field_name)
config_object.send(field_name)
elsif config_object.respond_to?(:[])
config_object[field_name] || config_object[field_name.to_s]
else
nil
end
field_def = @schema&.field_definition(@scope_name, field_name)
return raw_value unless field_def
casted = @schema&.cast_value(raw_value, field_def.type) || raw_value
if casted.is_a?(Array)
casted.dup
elsif casted.is_a?(Hash)
casted.dup
else
casted
end
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
66
67
68
69
|
# File 'lib/standard_config/config_provider.rb', line 66
def respond_to_missing?(method_name, include_private = false)
field_name = method_name.to_s.end_with?("=") ? method_name.to_s.chomp("=").to_sym : method_name.to_sym
@schema&.valid_field?(@scope_name, field_name) || super
end
|