Class: RailsConfig::Options

Inherits:
OpenStruct
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rails_config/options.rb

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.



90
91
92
# File 'lib/rails_config/options.rb', line 90

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

#[]=(param, value) ⇒ Object



94
95
96
# File 'lib/rails_config/options.rb', line 94

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

#add_source!(source) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/rails_config/options.rb', line 11

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



72
73
74
# File 'lib/rails_config/options.rb', line 72

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  marshal_dump.empty?
end

#merge!(hash) ⇒ Object



81
82
83
84
85
86
# File 'lib/rails_config/options.rb', line 81

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

#reload!Object Also known as: load!

look through all our sources and rebuild the configuration



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rails_config/options.rb', line 37

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)
    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 RailsConfig.use_env

  return self
end

#reload_env!Object Also known as: load_env!



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rails_config/options.rb', line 19

def reload_env!
  return self if ENV.nil? || ENV.empty?
  conf = Hash.new
  ENV.each do |key, value|
    next unless key.to_s.index(RailsConfig.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[RailsConfig.const_name] || {})
end

#reload_from_files(*files) ⇒ Object



59
60
61
62
# File 'lib/rails_config/options.rb', line 59

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

#to_hashObject



64
65
66
67
68
69
70
# File 'lib/rails_config/options.rb', line 64

def to_hash
  result = {}
  marshal_dump.each do |k, v|
    result[k] = v.instance_of?(RailsConfig::Options) ? v.to_hash : v
  end
  result
end

#to_json(*args) ⇒ Object



76
77
78
79
# File 'lib/rails_config/options.rb', line 76

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