Class: RailsConfig::Options

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

Instance Method Summary collapse

Instance Method Details

#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

#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
# 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

  # 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



38
39
40
41
# File 'lib/rails_config/options.rb', line 38

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

#to_hashObject



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

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



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

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