Module: UberConfig
- Defined in:
- lib/uber_config/pusher.rb,
lib/uber_config.rb,
lib/uber_config/version.rb
Overview
To use, add the following to your Rakefile desc ‘Pushes config to IronCache and sets Heroku config variable.’ namespace :config do
task :push do
UberConfig.push_heroku
end
end
Defined Under Namespace
Modules: Pusher
Constant Summary collapse
- VERSION =
"1.1.3"
- @@logger =
Logger.new(STDOUT)
Class Method Summary collapse
-
.load(options = {}) ⇒ Object
Load a config file from various system locations Options: :file => filename, default is ‘config’ which means it will look for both config.yml and config.json.
- .load_from(cf) ⇒ Object
- .logger ⇒ Object
- .logger=(l) ⇒ Object
- .make_indifferent(hash) ⇒ Object
- .set_default_proc(hash) ⇒ Object
-
.symbolize_keys(hash) ⇒ Object
Non destructive, returns new hash.
-
.symbolize_keys!(hash) ⇒ Object
Destructively convert all keys to symbols, as long as they respond to to_sym.
Class Method Details
.load(options = {}) ⇒ Object
Load a config file from various system locations Options:
:file => filename, default is 'config' which means it will look for both config.yml and config.json. You can use the full filename like 'config.json'
:dir => directory to look for in the default locations
:ext => extension, either yml or json. Default will check both.
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/uber_config.rb', line 25 def self.load(={}) # First check for abt config: https://github.com/iron-io/abt if defined? $abt_config logger.debug "$abt_config found." @config = $abt_config return @config end if ENV['CONFIG_CACHE_KEY'] logger.debug "ENV[CONFIG_CACHE_KEY] found." logger.debug "Getting config from #{ENV['CONFIG_CACHE_KEY']}" config_from_cache = open(ENV['CONFIG_CACHE_KEY']).read config_from_cache = JSON.parse(config_from_cache) config_from_cache = YAML.load(config_from_cache['value']) logger.debug "Got config from cache." set_default_proc(config_from_cache) return config_from_cache end dir = nil if [:dir] dir = [:dir] if dir.is_a?(String) dir = [dir] end end file = [:file] || "config" ext = [:ext] # let env variables override. Useful for things like minitest which don't allow you to pass in cli args to the tests. if ENV['CONFIGDIR'] dir = ENV['CONFIGDIR'] end if ENV['CONFIGFILE'] file = ENV['CONFIGFILE'] end # let cli args override if ARGV.include?('--configdir') dir = ARGV[ARGV.index('--configdir') + 1] end if ARGV.include?('--configfile') file = ARGV[ARGV.index('--configfile') + 1] end filenames = [] if file.include?(".") # then has extension filenames << file else if ext filenames << "#{file}.#{ext}" else filenames << "#{file}.yml" filenames << "#{file}.json" end end caller_file = caller[0][0...(caller[0].index(":in"))] caller_file = caller_file[0...(caller_file.rindex(":"))] caller_dir = File.dirname(caller_file) caller_dir = File.(caller_dir) caller_dir_split = caller_dir.split("/") auto_dir_name = caller_dir_split.last if auto_dir_name == "test" caller_dir_split.pop auto_dir_name = caller_dir_split.last end # Now check near caller file filenames.each do |file| dir_and_file = dir.nil? ? [] : dir.dup dir_and_file << file location = File.join(dir_and_file) cf = File.(location, caller_dir) @config = load_from(cf) return @config if @config # and working directory cf = File.(location) @config = load_from(cf) return @config if @config end # Now check in Dropbox filenames.each do |file| dropbox_folders = ["~", "Dropbox", "configs"] if dir dropbox_folders = dropbox_folders << dir else dropbox_folders = dropbox_folders << auto_dir_name end dropbox_folders << file cf = File.(File.join(dropbox_folders)) @config = load_from(cf) return @config if @config end # couldn't find it raise "UberConfig could not find config file!" end |
.load_from(cf) ⇒ Object
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/uber_config.rb', line 128 def self.load_from(cf) logger.debug "Checking for config file at #{cf}..." if File.exist?(cf) config = YAML::load_file(cf) # the following makes it indifferent access, but doesn't seem to for inner hashes set_default_proc(config) return config end nil end |
.logger ⇒ Object
11 12 13 |
# File 'lib/uber_config.rb', line 11 def self.logger @@logger end |
.logger=(l) ⇒ Object
15 16 17 |
# File 'lib/uber_config.rb', line 15 def self.logger=(l) @@logger = l end |
.make_indifferent(hash) ⇒ Object
185 186 187 188 189 190 191 192 193 194 |
# File 'lib/uber_config.rb', line 185 def self.make_indifferent(hash) hash.default_proc = proc do |h, k| case k when String then sym = k.to_sym; h[sym] if h.key?(sym) when Symbol then str = k.to_s; h[str] if h.key?(str) end end end |
.set_default_proc(hash) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/uber_config.rb', line 139 def self.set_default_proc(hash) #puts 'setting default proc' hash.default_proc = proc do |h, k| case k when String sym = k.to_sym; h[sym] if h.key?(sym) when Symbol str = k.to_s; h[str] if h.key?(str) end end hash.each_pair do |k, v| if v.is_a?(Hash) set_default_proc(v) end end end |
.symbolize_keys(hash) ⇒ Object
Non destructive, returns new hash.
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/uber_config.rb', line 172 def self.symbolize_keys(hash) keys = hash.keys ret = {} keys.each do |key| v = hash[key] if v.is_a?(Hash) v = symbolize_keys(v) end ret[(key.to_sym rescue key) || key] = v end ret end |
.symbolize_keys!(hash) ⇒ Object
Destructively convert all keys to symbols, as long as they respond to to_sym. inspired by activesupport
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/uber_config.rb', line 159 def self.symbolize_keys!(hash) keys = hash.keys keys.each do |key| v = hash.delete(key) if v.is_a?(Hash) v = symbolize_keys!(v) end hash[(key.to_sym rescue key) || key] = v end hash end |