Module: RailsConfig

Defined in:
lib/rails_config.rb,
lib/rails_config/railtie.rb

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

@@_ran_once =

ensures the setup only gets run once

false
@@const_name =
"Settings"
@@load_paths =
[]

Class Method Summary collapse

Class Method Details

.convert(h) ⇒ Object

Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rails_config.rb', line 58

def self.convert(h) #:nodoc:
  s = OpenStruct.new
  h.each do |k, v|
    s.new_ostruct_member(k)
    if v.is_a?(Hash)
      s.send( (k+'=').to_sym, convert(v))
    elsif v.is_a?(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(*files) ⇒ 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.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rails_config.rb', line 28

def self.load_files(*files)
  config = OpenStruct.new

  @@load_paths = [files].flatten.compact.uniq
  # add singleton method to our Settings that reloads its settings from the load_paths
  def config.reload!

    conf = {}
    RailsConfig.load_paths.to_a.each do |path|
      file_conf = YAML.load(ERB.new(IO.read(path.to_s)).result) if path and File.exists?(path.to_s)
      next unless file_conf

      if conf.size > 0
        DeepMerge.deep_merge!(file_conf, conf, :preserve_unmergeables => false)
      else
        conf = file_conf
      end
    end

    # load all the new values into the openstruct
    marshal_load(RailsConfig.convert(conf).marshal_dump)

    return self
  end

  config.reload!
  return config
end

.load_pathsObject



22
23
24
# File 'lib/rails_config.rb', line 22

def self.load_paths
  @@load_paths
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (RailsConfig)

    the object that the method was called on



16
17
18
19
# File 'lib/rails_config.rb', line 16

def self.setup
  yield self if @@_ran_once == false
  @@_ran_once = true
end