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.0"
@@logger =
Logger.new(STDOUT)

Class Method Summary collapse

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
127
128
129
130
131
132
133
134
# File 'lib/uber_config.rb', line 25

def self.load(options={})

  # First check for abt config: https://github.com/iron-io/abt
  if defined? $abt_config
    logger.info "$abt_config found."
    @config = $abt_config
    return @config
  end

  if ENV['CONFIG_CACHE_KEY']
    logger.info "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.info  "Got config from cache."
    set_default_proc(config_from_cache)
    return config_from_cache
  end

  dir = nil
  if options[:dir]
    dir = options[:dir]
    if dir.is_a?(String)
      dir = [dir]
    end
  end

  file = options[:file] || "config"
  ext = options[: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

  #p Kernel.caller
  caller_file = caller[0][0...(caller[0].index(":in"))]
  caller_file = caller_file[0...(caller_file.rindex(":"))]
  #p caller_file
  caller_dir = File.dirname(caller_file)
  #puts "caller_dir: " + caller_dir
  caller_dir = File.expand_path(caller_dir)
  #puts "caller_dir: " + caller_dir
  caller_dir_split = caller_dir.split("/")
  #p 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
    p dir_and_file
    dir_and_file << file
    p dir_and_file
    location = File.join(dir_and_file)
    #p location
    cf = File.expand_path(location, caller_dir)
    @config = load_from(cf)
    return @config if @config

    # and working directory
    cf = File.expand_path(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.expand_path(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



136
137
138
139
140
141
142
143
144
145
# File 'lib/uber_config.rb', line 136

def self.load_from(cf)
  logger.info "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

.loggerObject



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



194
195
196
197
198
199
200
201
202
203
# File 'lib/uber_config.rb', line 194

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/uber_config.rb', line 147

def self.set_default_proc(hash)
  #puts 'setting default proc'
  #p hash
  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.



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/uber_config.rb', line 181

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



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/uber_config.rb', line 168

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