Class: Config::Options

Inherits:
OpenStruct
  • Object
show all
Includes:
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}

Instance Method Summary collapse

Instance Method Details

#[](param) ⇒ Object

An alternative mechanism for property access. This let’s you do foo along with foo.bar.



114
115
116
117
# File 'lib/config/options.rb', line 114

def [](param)
  return super if SETTINGS_RESERVED_NAMES.include?(param)
  send("#{param}")
end

#[]=(param, value) ⇒ Object



119
120
121
# File 'lib/config/options.rb', line 119

def []=(param, value)
  send("#{param}=", value)
end

#add_source!(source) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/config/options.rb', line 15

def add_source!(source)
  # handle yaml file paths
  source = (Sources::YAMLSource.new(source)) if source.is_a?(String)

  @config_sources ||= []
  @config_sources << source
end

#each(*args, &block) ⇒ Object



93
94
95
# File 'lib/config/options.rb', line 93

def each(*args, &block)
  marshal_dump.each(*args, &block)
end

#empty?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/config/options.rb', line 11

def empty?
  marshal_dump.empty?
end

#keysObject



7
8
9
# File 'lib/config/options.rb', line 7

def keys
  marshal_dump.keys
end

#merge!(hash) ⇒ Object



102
103
104
105
106
107
# File 'lib/config/options.rb', line 102

def merge!(hash)
  current = to_hash
  DeepMerge.deep_merge!(hash.dup, current)
  marshal_load(__convert(current).marshal_dump)
  self
end

#prepend_source!(source) ⇒ Object



23
24
25
26
27
28
# File 'lib/config/options.rb', line 23

def prepend_source!(source)
  source = (Sources::YAMLSource.new(source)) if source.is_a?(String)

  @config_sources ||= []
  @config_sources.unshift(source)
end

#reload!Object Also known as: load!

look through all our sources and rebuild the configuration



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/config/options.rb', line 48

def reload!
  conf = {}
  @config_sources.each do |source|
    source_conf = source.load

    if conf.empty?
      conf = source_conf
    else
      # see Options Details in lib/rails_config/vendor/deep_merge.rb
      DeepMerge.deep_merge!(source_conf,
                            conf,
                            preserve_unmergeables: false,
                            knockout_prefix: Config.knockout_prefix)
    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

  self
end

#reload_env!Object Also known as: load_env!



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/config/options.rb', line 30

def reload_env!
  return self if ENV.nil? || ENV.empty?
  conf = Hash.new
  ENV.each do |key, value|
    next unless key.to_s.index(Config.const_name) == 0
    hash = value
    key.to_s.split('.').reverse.each do |element|
      hash = {element => hash}
    end
    DeepMerge.deep_merge!(hash, conf, :preserve_unmergeables => false)
  end

  merge!(conf[Config.const_name] || {})
end

#reload_from_files(*files) ⇒ Object



74
75
76
77
# File 'lib/config/options.rb', line 74

def reload_from_files(*files)
  Config.load_and_set_settings(files)
  reload!
end

#to_hashObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/config/options.rb', line 79

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



97
98
99
100
# File 'lib/config/options.rb', line 97

def to_json(*args)
  require "json" unless defined?(JSON)
  to_hash.to_json(*args)
end