class Config::Options
Constants
- SETTINGS_RESERVED_NAMES
Some keywords that don't play nicely with OpenStruct
Public Instance Methods
[](param)
click to toggle source
An alternative mechanism for property access. This let's you do foo along with foo.bar.
Calls superclass method
# File lib/config/options.rb, line 138 def [](param) return super if SETTINGS_RESERVED_NAMES.include?(param) send("#{param}") end
[]=(param, value)
click to toggle source
# File lib/config/options.rb, line 143 def []=(param, value) send("#{param}=", value) end
add_source!(source, namespace=nil)
click to toggle source
# File lib/config/options.rb, line 17 def add_source!(source, namespace=nil) # handle yaml file paths source = (Sources::YAMLSource.new(source, namespace)) if source.is_a?(String) source = (Sources::HashSource.new(source, namespace)) if source.is_a?(Hash) @config_sources ||= [] @config_sources << source end
each(*args, &block)
click to toggle source
# File lib/config/options.rb, line 114 def each(*args, &block) marshal_dump.each(*args, &block) end
empty?()
click to toggle source
# File lib/config/options.rb, line 13 def empty? marshal_dump.empty? end
keys()
click to toggle source
# File lib/config/options.rb, line 9 def keys marshal_dump.keys end
merge!(hash)
click to toggle source
# File lib/config/options.rb, line 123 def merge!(hash) current = to_hash DeepMerge.deep_merge!(hash.dup, current, preserve_unmergeables: false, overwrite_arrays: Config.overwrite_arrays) marshal_load(__convert(current).marshal_dump) self end
prepend_source!(source)
click to toggle source
# 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!()
click to toggle source
look through all our sources and rebuild the configuration
# File lib/config/options.rb, line 68 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) end end # swap out the contents of the OStruct with a hash (need to recursively convert) marshal_load(__convert(conf).marshal_dump) reload_env! if Config.use_env validate! if RUBY_VERSION >= '2.1' self end
Also aliased as: load!
reload_env!()
click to toggle source
# 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| keys = variable.to_s.split(Config.env_separator) next if keys.shift != (Config.env_prefix || Config.const_name) 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
Also aliased as: load_env!
reload_from_files(*files)
click to toggle source
# File lib/config/options.rb, line 95 def reload_from_files(*files) Config.load_and_set_settings(files) reload! end
to_hash()
click to toggle source
# File lib/config/options.rb, line 100 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)
click to toggle source
# File lib/config/options.rb, line 118 def to_json(*args) require "json" unless defined?(JSON) to_hash.to_json(*args) end
Protected Instance Methods
__value(v)
click to toggle source
Try to convert string to a correct type
# File lib/config/options.rb, line 187 def __value(v) Integer(v) rescue Float(v) rescue v end
descend_array(array)
click to toggle source
# File lib/config/options.rb, line 155 def descend_array(array) array.map do |value| if value.instance_of? Config::Options value.to_hash elsif value.instance_of? Array descend_array(value) else value end end end