Class: RailsConfig::Options

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



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

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

#[]=(param, value) ⇒ Object



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

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

#add_source!(source) ⇒ Object



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

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

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

#empty?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/rails_config/options.rb', line 5

def empty?
  marshal_dump.empty?
end

#merge!(hash) ⇒ Object



62
63
64
65
66
67
# File 'lib/rails_config/options.rb', line 62

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rails_config/options.rb', line 18

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

  conf.each_pair { |locale, values|
    unless locale == I18n.default_locale.to_s
      conf[locale.to_s].merge!(conf[I18n.default_locale.to_s].merge(conf[locale.to_s])) if conf[I18n.default_locale.to_s].is_a?(Hash) && conf[locale.to_s].is_a?(Hash)
    end
  }

  # swap out the contents of the OStruct with a hash (need to recursively convert)
  marshal_load(__convert(conf).marshal_dump)

  return self
end

#reload_from_files(*files) ⇒ Object



44
45
46
47
# File 'lib/rails_config/options.rb', line 44

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

#to_hashObject



49
50
51
52
53
54
55
# File 'lib/rails_config/options.rb', line 49

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



57
58
59
60
# File 'lib/rails_config/options.rb', line 57

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