Class: ApplicationConfig::ConfigBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/application_config/config_builder.rb

Overview

Summary

This is API documentation, NOT documentation on how to use this plugin. For that, see the README.

Class Method Summary collapse

Class Method Details

.convert(h) ⇒ Object

Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/application_config/config_builder.rb', line 36

def self.convert(h) #:nodoc:
  s = OpenStruct.new
  h.each do |k, v|
    s.new_ostruct_member(k)
    if v.instance_of?(Hash)
      s.send( (k+'=').to_sym, convert(v))
    elsif v.instance_of?(Array)
      converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
      s.send("#{k}=".to_sym, converted_array)
    else
      s.send("#{k}=".to_sym, v)
    end
  end
  s
end

.load_files(*conf_load_paths) ⇒ Object

Create a config object (OpenStruct) from a yaml file. If a second yaml file is given, then the sections of that file will overwrite the sections if the first file if they exist in the first file.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/application_config/config_builder.rb', line 12

def self.load_files(*conf_load_paths)
  config = OpenStruct.new
  config.load_paths = []
  config.load_paths << conf_load_paths
  config.load_paths.flatten!.uniq!
  
  # add singleton method to our AppConfig that reloads its settings from the load_paths options
  def config.reload!
    conf = {:load_paths => self.load_paths}
    self.load_paths.to_a.each do |path|
      file_conf = YAML.load(ERB.new(IO.read(path)).result) if path and File.exists?(path)
      conf.merge!(file_conf) if file_conf
    end
    
    # load all the new values into the openstruct
    marshal_load(ApplicationConfig::ConfigBuilder.convert(conf).marshal_dump)
    return self
  end

  config.reload!
  return config
end