uber_config 0.0.6 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -12
- data/lib/uber_config/uber_hash.rb +12 -0
- data/lib/uber_config/version.rb +1 -1
- data/lib/uber_config.rb +69 -39
- data/test/config.json +3 -0
- data/test/config.yml +4 -0
- data/test/config2.json +3 -0
- data/test/test_uber_config.rb +17 -0
- metadata +6 -2
data/README.md
CHANGED
@@ -1,28 +1,27 @@
|
|
1
1
|
UberConfig is a config loader that will load config files from various locations.
|
2
2
|
|
3
|
+
# New in 1.0
|
4
|
+
|
5
|
+
- Will look for both yml AND json files
|
6
|
+
- Added :ext option to choose config extension
|
7
|
+
- Indifferent access so you can use either symbols or strings for keys
|
8
|
+
|
9
|
+
# Usage
|
10
|
+
|
3
11
|
Just run:
|
4
12
|
|
5
13
|
config = UberConfig.load
|
6
14
|
|
7
|
-
|
15
|
+
It will look `config.yml` or `config.json` by default. You can change what it will look for and
|
16
|
+
where with the different load() options.
|
8
17
|
|
9
18
|
Here is the order of where it looks:
|
10
19
|
|
11
20
|
- $abt_config global variable (for https://github.com/iron-io/abt)
|
12
21
|
- directory of file that calls UberConfig.load
|
13
22
|
- working directory
|
14
|
-
- TODO: look at ~/configs directory
|
23
|
+
- TODO: look at ~/configs/#{working_directory_name} directory
|
15
24
|
- ~/Dropbox/configs/#{working_directory_name}
|
16
25
|
|
17
26
|
:dir can be used to change working_directory_name.
|
18
27
|
|
19
|
-
## Helper methods
|
20
|
-
|
21
|
-
### Symbolize Keys
|
22
|
-
|
23
|
-
When it loads up config files, the hash keys will be strings by default because they are
|
24
|
-
strings in the files. You can convert them to keys by running `UberConfig.symbolize_keys!`, for example:
|
25
|
-
|
26
|
-
config = UberConfig.load
|
27
|
-
config = UberConfig.symbolize_keys!(config)
|
28
|
-
|
data/lib/uber_config/version.rb
CHANGED
data/lib/uber_config.rb
CHANGED
@@ -3,9 +3,13 @@ require "uber_config/version"
|
|
3
3
|
module UberConfig
|
4
4
|
|
5
5
|
# Load a config file from various system locations
|
6
|
+
# Options:
|
7
|
+
# :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'
|
8
|
+
# :dir => directory to look for in the default locations
|
9
|
+
# :ext => extension, either yml or json. Default will check both.
|
6
10
|
def self.load(options={})
|
7
11
|
|
8
|
-
# First check for abt config
|
12
|
+
# First check for abt config: https://github.com/iron-io/abt
|
9
13
|
if defined? $abt_config
|
10
14
|
puts "$abt_config found."
|
11
15
|
@config = $abt_config
|
@@ -19,13 +23,20 @@ module UberConfig
|
|
19
23
|
dir = [dir]
|
20
24
|
end
|
21
25
|
end
|
22
|
-
file = "config.yml"
|
23
|
-
if options[:file]
|
24
|
-
file = options[:file]
|
25
|
-
end
|
26
26
|
|
27
|
-
|
28
|
-
|
27
|
+
file = options[:file] || "config"
|
28
|
+
ext = options[:ext]
|
29
|
+
filenames = []
|
30
|
+
if file.include?(".") # then has extension
|
31
|
+
filenames << file
|
32
|
+
else
|
33
|
+
if ext
|
34
|
+
filenames << "#{file}.#{ext}"
|
35
|
+
else
|
36
|
+
filenames << "#{file}.yml"
|
37
|
+
filenames << "#{file}.json"
|
38
|
+
end
|
39
|
+
end
|
29
40
|
|
30
41
|
#p Kernel.caller
|
31
42
|
caller_file = caller[0][0...(caller[0].index(":in"))]
|
@@ -44,33 +55,36 @@ module UberConfig
|
|
44
55
|
end
|
45
56
|
|
46
57
|
# Now check near caller file
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
58
|
+
filenames.each do |file|
|
59
|
+
dir_and_file = dir.nil? ? [] : dir.dup
|
60
|
+
dir_and_file << file
|
61
|
+
#p dir_and_file
|
62
|
+
location = File.join(dir_and_file)
|
63
|
+
#p location
|
64
|
+
cf = File.expand_path(location, caller_dir)
|
65
|
+
@config = load_from(cf)
|
66
|
+
return @config if @config
|
67
|
+
|
68
|
+
# and working directory
|
69
|
+
cf = File.expand_path(location)
|
70
|
+
@config = load_from(cf)
|
71
|
+
return @config if @config
|
72
|
+
|
73
|
+
end
|
62
74
|
|
63
75
|
# Now check in Dropbox
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
76
|
+
filenames.each do |file|
|
77
|
+
dropbox_folders = ["~", "Dropbox", "configs"]
|
78
|
+
if dir
|
79
|
+
dropbox_folders = dropbox_folders + dir
|
80
|
+
else
|
81
|
+
dropbox_folders = dropbox_folders << auto_dir_name
|
82
|
+
end
|
83
|
+
dropbox_folders << file
|
84
|
+
cf = File.expand_path(File.join(dropbox_folders))
|
85
|
+
@config = load_from(cf)
|
86
|
+
return @config if @config
|
69
87
|
end
|
70
|
-
dropbox_folders << file
|
71
|
-
cf = File.expand_path(File.join(dropbox_folders))
|
72
|
-
@config = load_from(cf)
|
73
|
-
return @config if @config
|
74
88
|
|
75
89
|
# couldn't find it
|
76
90
|
raise "UberConfig could not find config file!"
|
@@ -82,17 +96,31 @@ module UberConfig
|
|
82
96
|
if File.exist?(cf)
|
83
97
|
config = YAML::load_file(cf)
|
84
98
|
# the following makes it indifferent access, but doesn't seem to for inner hashes
|
85
|
-
|
86
|
-
# case k
|
87
|
-
# when String then sym = k.to_sym; h[sym] if h.key?(sym)
|
88
|
-
# when Symbol then str = k.to_s; h[str] if h.key?(str)
|
89
|
-
# end
|
90
|
-
#end
|
99
|
+
set_default_proc(config)
|
91
100
|
return config
|
92
101
|
end
|
93
102
|
nil
|
94
103
|
end
|
95
104
|
|
105
|
+
def self.set_default_proc(hash)
|
106
|
+
puts 'setting default proc'
|
107
|
+
p hash
|
108
|
+
hash.default_proc = proc do |h, k|
|
109
|
+
case k
|
110
|
+
when String
|
111
|
+
sym = k.to_sym; h[sym] if h.key?(sym)
|
112
|
+
when Symbol
|
113
|
+
str = k.to_s; h[str] if h.key?(str)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
hash.each_pair do |k, v|
|
117
|
+
if v.is_a?(Hash)
|
118
|
+
set_default_proc(v)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
96
124
|
# Destructively convert all keys to symbols, as long as they respond to to_sym.
|
97
125
|
# inspired by activesupport
|
98
126
|
def self.symbolize_keys!(hash)
|
@@ -124,8 +152,10 @@ module UberConfig
|
|
124
152
|
def self.make_indifferent(hash)
|
125
153
|
hash.default_proc = proc do |h, k|
|
126
154
|
case k
|
127
|
-
when String then
|
128
|
-
|
155
|
+
when String then
|
156
|
+
sym = k.to_sym; h[sym] if h.key?(sym)
|
157
|
+
when Symbol then
|
158
|
+
str = k.to_s; h[str] if h.key?(str)
|
129
159
|
end
|
130
160
|
end
|
131
161
|
end
|
data/test/config.json
ADDED
data/test/config.yml
ADDED
data/test/config2.json
ADDED
data/test/test_uber_config.rb
CHANGED
@@ -7,6 +7,23 @@ class TestUberConfig < Test::Unit::TestCase
|
|
7
7
|
def test_loading
|
8
8
|
@config = UberConfig.load
|
9
9
|
p @config
|
10
|
+
|
11
|
+
assert_equal "value", @config['ymlx']
|
12
|
+
assert_equal "value", @config[:ymlx]
|
13
|
+
p @config[:outer]
|
14
|
+
assert_equal "inside", @config[:outer][:inner]
|
15
|
+
|
16
|
+
@config = UberConfig.load(:ext=>'json')
|
17
|
+
p @config
|
18
|
+
assert_equal "value", @config['jsonx']
|
19
|
+
assert_equal "value", @config[:jsonx]
|
20
|
+
|
21
|
+
@config = UberConfig.load(:file=>"config2")
|
22
|
+
p @config
|
23
|
+
assert_equal "value2", @config['jsonx']
|
24
|
+
assert_equal "value2", @config[:jsonx]
|
25
|
+
|
26
|
+
|
10
27
|
end
|
11
28
|
|
12
29
|
def test_symbolize
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uber_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-09-25 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Loads configs from common locations.
|
15
15
|
email:
|
@@ -23,7 +23,11 @@ files:
|
|
23
23
|
- README.md
|
24
24
|
- Rakefile
|
25
25
|
- lib/uber_config.rb
|
26
|
+
- lib/uber_config/uber_hash.rb
|
26
27
|
- lib/uber_config/version.rb
|
28
|
+
- test/config.json
|
29
|
+
- test/config.yml
|
30
|
+
- test/config2.json
|
27
31
|
- test/test_uber_config.rb
|
28
32
|
- uber_config.gemspec
|
29
33
|
homepage: ''
|