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.

Constant Summary collapse

@@load_paths =
[]
@@expand_keys =
[]
@@root_path =
""

Class Method Summary collapse

Class Method Details

.convert(h) ⇒ Object

Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/application_config/config_builder.rb', line 61

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

.expand(config, base_path) ⇒ Object

expand a config val



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/application_config/config_builder.rb', line 78

def self.expand(config, base_path)
  case config.class.to_s
  when "Hash"
    return expand_hash(config, base_path)
  when "Array"
    return expand_array(config, base_path)
  when "String"
    return expand_string(config, base_path)
  end      
  return config
end

.expand_array(config, base_path) ⇒ Object

expand an array by cycling through all the values



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/application_config/config_builder.rb', line 115

def self.expand_array(config, base_path)
  # puts "Expanding Array: #{config.inspect}"
  new_config = []
  config.each do |val|
    new_val = expand(val, base_path)
    if new_val.is_a?(Array)
      new_val.each do |inner|
        new_config << inner
      end
    else
      new_config << new_val
    end
  end
  return new_config.uniq
end

.expand_hash(config, base_path) ⇒ Object

expand a hash by cycling throw all the hash values



105
106
107
108
109
110
111
112
# File 'lib/application_config/config_builder.rb', line 105

def self.expand_hash(config, base_path)
  # puts "Expanding Hash: #{config.inspect}"
  new_config = {}
  config.each do |key, val|
    new_config[key] = expand(val, base_path)
  end
  return new_config
end

.expand_keysObject



52
53
54
# File 'lib/application_config/config_builder.rb', line 52

def self.expand_keys
  @@expand_keys
end

.expand_string(config, base_path) ⇒ Object

expand a string and returns a list



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/application_config/config_builder.rb', line 91

def self.expand_string(config, base_path)
  # puts "Expanding String: #{config.inspect}"
  if config.include?("*")
    results = Dir["#{base_path}/#{config}"].map{|i| i.to_s.gsub("#{base_path}/", "") }

    # puts "EXPANDED PATH: #{base_path}/#{config}"
    # puts results.inspect        
    return results
  else
    return config
  end
end

.load_files(options = {}) ⇒ 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.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/application_config/config_builder.rb', line 15

def self.load_files(options = {})
  config = OpenStruct.new
  
  @@load_paths = [options[:paths]].flatten.compact.uniq
  @@expand_keys = [options[:expand_keys]].flatten.compact.uniq
  @@root_path = options[:root_path]
  
  # add singleton method to our AppConfig that reloads its settings from the load_paths options
  def config.reload!
    conf = {}
    ConfigBuilder.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
    
    # expand the javascripts config to handle *.* paths
    ConfigBuilder.expand_keys.to_a.each do |expand_path|
      expand_path = expand_path.to_s
      if conf[expand_path]
        conf[expand_path] = ApplicationConfig::ConfigBuilder.expand(conf[expand_path], "#{ConfigBuilder.root_path}/public/#{expand_path}")
      end
    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

.load_pathsObject



48
49
50
# File 'lib/application_config/config_builder.rb', line 48

def self.load_paths
  @@load_paths
end

.root_pathObject



56
57
58
# File 'lib/application_config/config_builder.rb', line 56

def self.root_path
  @@root_path
end