Class: ApplicationConfig::ConfigBuilder
- Inherits:
-
Object
- Object
- ApplicationConfig::ConfigBuilder
- 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
-
.convert(h) ⇒ Object
Recursively converts Hashes to OpenStructs (including Hashes inside Arrays).
-
.expand(config, base_path) ⇒ Object
expand a config val.
-
.expand_array(config, base_path) ⇒ Object
expand an array by cycling through all the values.
-
.expand_hash(config, base_path) ⇒ Object
expand a hash by cycling throw all the hash values.
- .expand_keys ⇒ Object
-
.expand_string(config, base_path) ⇒ Object
expand a string and returns a list.
-
.load_files(options = {}) ⇒ Object
Create a config object (OpenStruct) from a yaml file.
- .load_paths ⇒ Object
- .root_path ⇒ Object
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.(config, base_path) case config.class.to_s when "Hash" return (config, base_path) when "Array" return (config, base_path) when "String" return (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.(config, base_path) # puts "Expanding Array: #{config.inspect}" new_config = [] config.each do |val| new_val = (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.(config, base_path) # puts "Expanding Hash: #{config.inspect}" new_config = {} config.each do |key, val| new_config[key] = (val, base_path) end return new_config end |
.expand_keys ⇒ Object
52 53 54 |
# File 'lib/application_config/config_builder.rb', line 52 def self. @@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.(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( = {}) config = OpenStruct.new @@load_paths = [[:paths]].flatten.compact.uniq @@expand_keys = [[:expand_keys]].flatten.compact.uniq @@root_path = [: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..to_a.each do || = .to_s if conf[] conf[] = ApplicationConfig::ConfigBuilder.(conf[], "#{ConfigBuilder.root_path}/public/#{}") 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_paths ⇒ Object
48 49 50 |
# File 'lib/application_config/config_builder.rb', line 48 def self.load_paths @@load_paths end |
.root_path ⇒ Object
56 57 58 |
# File 'lib/application_config/config_builder.rb', line 56 def self.root_path @@root_path end |