app_conf 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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.address.street -> '1 Some Road'
27
+ AppConf.user[:address]['street'] -> '1 Some Road'
28
28
 
29
29
  Syntax
30
30
  ----------------------------------
31
- * Load multiple files at once:
32
-
31
+ Load multiple files at once:
33
32
  AppConf.load(*filenames)
34
33
 
35
- * Or individually:
36
-
34
+ Or individually:
37
35
  AppConf.load(filename1)
38
36
  AppConf.load(filename2)
39
37
 
40
- * Infinitely nested keys:
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
- * Override existing values:
45
-
46
+ Override existing values:
46
47
  AppConf.loaded.from.yaml = 'can override'
48
+ AppConf['loaded']['from']['yaml'] = 'can override'
47
49
 
48
- * Set new values:
49
-
50
+ Set new values:
50
51
  AppConf.non.existing.value = 'can set'
51
52
 
52
- * Clear entire tree:
53
-
53
+ Clear entire tree:
54
54
  AppConf.clear
55
55
 
56
- * Returns nil for non-existent keys:
57
-
56
+ Returns nil for non-existent keys:
58
57
  AppConf.non_existing -> nil
59
- AppConf.non_existing.name -> NoMethodError: undefined method `name' for nil:NilClass
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.1.2'
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 = method[0..-2]
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
- @config[method]
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.send(key) || new_app_config)
47
+ value = recurse(value, app_config[key] || new_app_config)
42
48
  end
43
- app_config.send("#{key}=", value) if new_app_config.nil? || value === new_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
@@ -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
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
7
  - 2
9
- version: 0.1.2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Phil Thompson