app_conf 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -17
- data/app_conf.gemspec +1 -1
- data/lib/app_conf.rb +13 -7
- data/spec/app_conf_spec.rb +21 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -24,42 +24,40 @@ Code:
|
|
24
24
|
AppConf.load('config.yml', 'other.yml')
|
25
25
|
AppConf.fullname -> 'Joe Blogs'
|
26
26
|
AppConf.user.name -> 'Joe'
|
27
|
-
AppConf.user
|
27
|
+
AppConf.user[:address]['street'] -> '1 Some Road'
|
28
28
|
|
29
29
|
Syntax
|
30
30
|
----------------------------------
|
31
|
-
|
32
|
-
|
31
|
+
Load multiple files at once:
|
33
32
|
AppConf.load(*filenames)
|
34
33
|
|
35
|
-
|
36
|
-
|
34
|
+
Or individually:
|
37
35
|
AppConf.load(filename1)
|
38
36
|
AppConf.load(filename2)
|
39
37
|
|
40
|
-
|
38
|
+
Use either method calls or hash:
|
39
|
+
AppConf.fullname
|
40
|
+
AppConf[:fullname]
|
41
|
+
AppConf['fullname']
|
41
42
|
|
43
|
+
Infinitely nested keys:
|
42
44
|
AppConf.multiple.nested.keys
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
+
Override existing values:
|
46
47
|
AppConf.loaded.from.yaml = 'can override'
|
48
|
+
AppConf['loaded']['from']['yaml'] = 'can override'
|
47
49
|
|
48
|
-
|
49
|
-
|
50
|
+
Set new values:
|
50
51
|
AppConf.non.existing.value = 'can set'
|
51
52
|
|
52
|
-
|
53
|
-
|
53
|
+
Clear entire tree:
|
54
54
|
AppConf.clear
|
55
55
|
|
56
|
-
|
57
|
-
|
56
|
+
Returns nil for non-existent keys:
|
58
57
|
AppConf.non_existing -> nil
|
59
|
-
AppConf.non_existing.name -> NoMethodError: undefined method
|
60
|
-
|
61
|
-
* Not dependent on Rails but easy to use with it. For example:
|
58
|
+
AppConf.non_existing.name -> NoMethodError: undefined method 'name' for nil:NilClass
|
62
59
|
|
60
|
+
Not dependent on Rails but easy to use with it. For example:
|
63
61
|
AppConf.load('config.yml', "#{Rails.env}.yml")
|
64
62
|
|
65
63
|
Other stuff
|
data/app_conf.gemspec
CHANGED
@@ -2,7 +2,7 @@ require 'base64'
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'app_conf'
|
5
|
-
s.version = '0.
|
5
|
+
s.version = '0.2.0'
|
6
6
|
s.authors = 'Phil Thompson'
|
7
7
|
s.email = Base64.decode64("cGhpbEBlbGVjdHJpY3Zpc2lvbnMuY29t\n")
|
8
8
|
s.summary = 'Simplest YAML Backed Application Wide Configuration (AppConfig)'
|
data/lib/app_conf.rb
CHANGED
@@ -18,14 +18,20 @@ class AppConf
|
|
18
18
|
nil
|
19
19
|
end
|
20
20
|
|
21
|
+
def [](key)
|
22
|
+
@config[key.to_s]
|
23
|
+
end
|
24
|
+
|
25
|
+
def []=(key, value)
|
26
|
+
raise "Not allowed to overwrite nested entities" if @config[key.to_s].is_a?(AppConf)
|
27
|
+
@config[key.to_s] = value
|
28
|
+
end
|
29
|
+
|
21
30
|
def method_missing(method, *args, &block)
|
22
|
-
method = method.to_s
|
23
31
|
if method[-1] == '='
|
24
|
-
method
|
25
|
-
raise "Not allowed to overwrite nested entities" if @config[method].is_a?(AppConf)
|
26
|
-
@config[method] = args.first
|
32
|
+
self[method[0..-2]] = args.first
|
27
33
|
else
|
28
|
-
|
34
|
+
self[method]
|
29
35
|
end
|
30
36
|
end
|
31
37
|
|
@@ -38,9 +44,9 @@ private
|
|
38
44
|
hash.each do |key, value|
|
39
45
|
if value.is_a?(Hash)
|
40
46
|
new_app_config = new
|
41
|
-
value = recurse(value, app_config
|
47
|
+
value = recurse(value, app_config[key] || new_app_config)
|
42
48
|
end
|
43
|
-
app_config
|
49
|
+
app_config[key] = value if new_app_config.nil? || value === new_app_config
|
44
50
|
end
|
45
51
|
app_config
|
46
52
|
end
|
data/spec/app_conf_spec.rb
CHANGED
@@ -13,6 +13,11 @@ describe AppConf do
|
|
13
13
|
AppConf.fullname.must_equal 'Joe Bloggs'
|
14
14
|
end
|
15
15
|
|
16
|
+
it 'works with hash notation' do
|
17
|
+
AppConf[:fullname].must_equal 'Joe Bloggs'
|
18
|
+
AppConf['fullname'].must_equal 'Joe Bloggs'
|
19
|
+
end
|
20
|
+
|
16
21
|
describe 'clear' do
|
17
22
|
it 'clears all keys' do
|
18
23
|
AppConf.fullname.wont_be_nil
|
@@ -29,6 +34,16 @@ describe AppConf do
|
|
29
34
|
AppConf.user.name.first.must_equal 'Joe'
|
30
35
|
end
|
31
36
|
|
37
|
+
it 'works with nested hash notation' do
|
38
|
+
AppConf[:user][:name][:first].must_equal 'Joe'
|
39
|
+
AppConf['user']['name']['first'].must_equal 'Joe'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'works with mixed notation' do
|
43
|
+
AppConf[:user][:name][:first].must_equal 'Joe'
|
44
|
+
AppConf.user['name'].first.must_equal 'Joe'
|
45
|
+
end
|
46
|
+
|
32
47
|
it 'works with multiple files' do
|
33
48
|
AppConf.clear
|
34
49
|
AppConf.load("#{@dir}/config.yml", "#{@dir}/other.yml")
|
@@ -47,6 +62,11 @@ describe AppConf do
|
|
47
62
|
AppConf.user.name.last.must_equal 'Bloggs'
|
48
63
|
end
|
49
64
|
|
65
|
+
it 'allows additional keys to be set with hash notation' do
|
66
|
+
AppConf.user[:name][:last] = 'Bloggs'
|
67
|
+
AppConf.user.name.last.must_equal 'Bloggs'
|
68
|
+
end
|
69
|
+
|
50
70
|
it 'allows existing keys to be overridden' do
|
51
71
|
AppConf.user.name.first = 'Jody'
|
52
72
|
AppConf.user.name.first.must_equal 'Jody'
|
@@ -63,6 +83,7 @@ describe AppConf do
|
|
63
83
|
|
64
84
|
it 'does not allow nested items to be overwritten' do
|
65
85
|
lambda { AppConf.user.name = 'something' }.must_raise RuntimeError
|
86
|
+
lambda { AppConf[:user][:name] = 'something' }.must_raise RuntimeError
|
66
87
|
end
|
67
88
|
end
|
68
89
|
end
|