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
-
.merge_assets(list) ⇒ Object
Cycles through the array of single element hashes and deep merges any duplicates it finds.
- .root_path ⇒ Object
Class Method Details
.convert(h) ⇒ Object
Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/application_config/config_builder.rb', line 68 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
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/application_config/config_builder.rb', line 85 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
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/application_config/config_builder.rb', line 122 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
112 113 114 115 116 117 118 119 |
# File 'lib/application_config/config_builder.rb', line 112 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
59 60 61 |
# File 'lib/application_config/config_builder.rb', line 59 def self. @@expand_keys end |
.expand_string(config, base_path) ⇒ Object
expand a string and returns a list
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/application_config/config_builder.rb', line 98 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 47 48 49 50 51 52 53 |
# 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) next unless file_conf if conf.size > 0 DeepMerge.deep_merge!(file_conf, conf, :preserve_unmergeables => false) else conf = file_conf end 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
55 56 57 |
# File 'lib/application_config/config_builder.rb', line 55 def self.load_paths @@load_paths end |
.merge_assets(list) ⇒ Object
Cycles through the array of single element hashes and deep merges any duplicates it finds
This is needed so you can define stylesheet keys in multiple config files
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/application_config/config_builder.rb', line 143 def self.merge_assets(list) assets = Array(list).map do |i| if i.is_a?(OpenStruct) i.marshal_dump else i end end # filter out the duplicate single hash keys hash_keys = assets.select{|i| i.is_a?(Hash) and i.keys.size == 1}.group_by{|i| i.keys[0]} hash_keys.each do |key, value| if Array(value).size > 1 merged = value.inject({}){|merged, v| DeepMerge.deep_merge!(v,merged)} value[0].replace(merged) value[1..-1].each do |v| v.clear end end end assets.select{|i| !i.blank? } end |
.root_path ⇒ Object
63 64 65 |
# File 'lib/application_config/config_builder.rb', line 63 def self.root_path @@root_path end |