Class: Config::Options
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Config::Options
show all
- Includes:
- Validation::Validate, Enumerable
- Defined in:
- lib/config/options.rb
Constant Summary
collapse
- SETTINGS_RESERVED_NAMES =
Some keywords that don’t play nicely with OpenStruct
%w[select collect test count zip].freeze
Instance Method Summary
collapse
#validate!
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
171
172
173
174
175
176
|
# File 'lib/config/options.rb', line 171
def method_missing(method_name, *args)
if Config.fail_on_missing && method_name !~ /.*(?==\z)/m
raise KeyError, "key not found: #{method_name.inspect}" unless key?(method_name)
end
super
end
|
Instance Method Details
#[](param) ⇒ Object
An alternative mechanism for property access. This let’s you do foo along with foo.bar.
148
149
150
151
|
# File 'lib/config/options.rb', line 148
def [](param)
return super if SETTINGS_RESERVED_NAMES.include?(param)
send("#{param}")
end
|
#[]=(param, value) ⇒ Object
153
154
155
|
# File 'lib/config/options.rb', line 153
def []=(param, value)
send("#{param}=", value)
end
|
#add_source!(source) ⇒ Object
17
18
19
20
21
22
23
24
|
# File 'lib/config/options.rb', line 17
def add_source!(source)
source = (Sources::YAMLSource.new(source)) if source.is_a?(String)
source = (Sources::HashSource.new(source)) if source.is_a?(Hash)
@config_sources ||= []
@config_sources << source
end
|
#each(*args, &block) ⇒ Object
120
121
122
|
# File 'lib/config/options.rb', line 120
def each(*args, &block)
marshal_dump.each(*args, &block)
end
|
#empty? ⇒ Boolean
13
14
15
|
# File 'lib/config/options.rb', line 13
def empty?
marshal_dump.empty?
end
|
#has_key?(key) ⇒ Boolean
167
168
169
|
# File 'lib/config/options.rb', line 167
def has_key?(key)
table.has_key?(key)
end
|
#key?(key) ⇒ Boolean
163
164
165
|
# File 'lib/config/options.rb', line 163
def key?(key)
table.key?(key)
end
|
#keys ⇒ Object
9
10
11
|
# File 'lib/config/options.rb', line 9
def keys
marshal_dump.keys
end
|
#merge!(hash) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/config/options.rb', line 129
def merge!(hash)
current = to_hash
DeepMerge.deep_merge!(
hash.dup,
current,
preserve_unmergeables: false,
knockout_prefix: Config.knockout_prefix,
overwrite_arrays: Config.overwrite_arrays,
merge_nil_values: Config.merge_nil_values
)
marshal_load(__convert(current).marshal_dump)
self
end
|
#prepend_source!(source) ⇒ Object
26
27
28
29
30
31
32
|
# File 'lib/config/options.rb', line 26
def prepend_source!(source)
source = (Sources::YAMLSource.new(source)) if source.is_a?(String)
source = (Sources::HashSource.new(source)) if source.is_a?(Hash)
@config_sources ||= []
@config_sources.unshift(source)
end
|
#reload! ⇒ Object
Also known as:
load!
look through all our sources and rebuild the configuration
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/config/options.rb', line 71
def reload!
conf = {}
@config_sources.each do |source|
source_conf = source.load
if conf.empty?
conf = source_conf
else
DeepMerge.deep_merge!(
source_conf,
conf,
preserve_unmergeables: false,
knockout_prefix: Config.knockout_prefix,
overwrite_arrays: Config.overwrite_arrays,
merge_nil_values: Config.merge_nil_values
)
end
end
marshal_load(__convert(conf).marshal_dump)
reload_env! if Config.use_env
validate! if RUBY_VERSION >= '2.1'
self
end
|
#reload_env! ⇒ Object
Also known as:
load_env!
34
35
36
37
38
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
65
66
|
# File 'lib/config/options.rb', line 34
def reload_env!
return self if ENV.nil? || ENV.empty?
hash = Hash.new
ENV.each do |variable, value|
separator = Config.env_separator
prefix = (Config.env_prefix || Config.const_name).to_s.split(separator)
keys = variable.to_s.split(separator)
next if keys.shift(prefix.size) != prefix
keys.map! { |key|
case Config.env_converter
when :downcase then
key.downcase.to_sym
when nil then
key.to_sym
else
raise "Invalid ENV variables name converter: #{Config.env_converter}"
end
}
leaf = keys[0...-1].inject(hash) { |h, key|
h[key] ||= {}
}
leaf[keys.last] = Config.env_parse_values ? __value(value) : value
end
merge!(hash)
end
|
#reload_from_files(*files) ⇒ Object
#respond_to_missing?(*args) ⇒ Boolean
178
179
180
|
# File 'lib/config/options.rb', line 178
def respond_to_missing?(*args)
super
end
|
#to_hash ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/config/options.rb', line 106
def to_hash
result = {}
marshal_dump.each do |k, v|
if v.instance_of? Config::Options
result[k] = v.to_hash
elsif v.instance_of? Array
result[k] = descend_array(v)
else
result[k] = v
end
end
result
end
|
#to_json(*args) ⇒ Object
124
125
126
127
|
# File 'lib/config/options.rb', line 124
def to_json(*args)
require "json" unless defined?(JSON)
to_hash.to_json(*args)
end
|