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.



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



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



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

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



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



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

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



40
41
42
43
# File 'lib/rails_config/options.rb', line 40

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

#to_hashObject



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

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