Module: UberConfig
- Defined in:
- lib/uber_config.rb,
lib/uber_config/version.rb
Constant Summary collapse
- VERSION =
"0.0.6"
Class Method Summary collapse
-
.load(options = {}) ⇒ Object
Load a config file from various system locations.
- .load_from(cf) ⇒ Object
- .make_indifferent(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
6 7 8 9 10 11 12 13 14 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 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 |
# File 'lib/uber_config.rb', line 6 def self.load(={}) # First check for abt config if defined? $abt_config puts "$abt_config found." @config = $abt_config return @config end dir = nil if [:dir] dir = [:dir] if dir.is_a?(String) dir = [dir] end end file = "config.yml" if [:file] file = [:file] end working_directory = Dir.pwd #puts "working_dir: " + working_directory #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.(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 dir_and_file = dir.nil? ? [] : dir.dup dir_and_file << file p dir_and_file location = File.join(dir_and_file) p location 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 puts "auto_dir_name: #{auto_dir_name}" # Now check in Dropbox 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 # couldn't find it raise "UberConfig could not find config file!" end |
.load_from(cf) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/uber_config.rb', line 80 def self.load_from(cf) puts "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 #config.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 return config end nil end |
.make_indifferent(hash) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/uber_config.rb', line 124 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 |
.symbolize_keys(hash) ⇒ Object
Non destructive, returns new hash.
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/uber_config.rb', line 111 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
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/uber_config.rb', line 98 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 |