app_conf 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,7 +35,7 @@ Or individually:
35
35
  AppConf.load(filename1)
36
36
  AppConf.load(filename2)
37
37
 
38
- Use either method calls or hash:
38
+ Use either method calls or hash syntax:
39
39
  AppConf.fullname
40
40
  AppConf[:fullname]
41
41
  AppConf['fullname']
@@ -48,7 +48,7 @@ Override existing values:
48
48
  AppConf['loaded']['from']['yaml'] = 'can override'
49
49
 
50
50
  Set new values:
51
- AppConf.non.existing.value = 'can set'
51
+ AppConf.non_existing_value = 'can set'
52
52
 
53
53
  Clear entire tree:
54
54
  AppConf.clear
@@ -57,6 +57,9 @@ Returns nil for non-existent keys:
57
57
  AppConf.non_existing -> nil
58
58
  AppConf.non_existing.name -> NoMethodError: undefined method 'name' for nil:NilClass
59
59
 
60
+ Use `from_hash` to create non-existent nodes:
61
+ AppConf.from_hash({...})
62
+
60
63
  Not dependent on Rails but easy to use with it. For example:
61
64
  AppConf.load('config.yml', "#{Rails.env}.yml")
62
65
 
@@ -73,3 +76,8 @@ Why
73
76
  * Others are either too simple or incomplete, lack documentation or aren't Gem installable
74
77
  * Because I can :-)
75
78
 
79
+ Known Issues
80
+ ----------------------------------
81
+ Cannot assign values to unknown nested keys because they return nil (create the tree first):
82
+ AppConf.from_hash({:non_existing => {:name => 'bla'}})
83
+
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.2.0'
5
+ s.version = '0.2.1'
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
@@ -1,30 +1,30 @@
1
1
  require 'yaml'
2
2
 
3
3
  class AppConf
4
- private_class_method :new
4
+ @@root = new
5
+
5
6
  def initialize
6
- @config = {}
7
+ @hash = {}
7
8
  end
8
- @@config = new
9
9
 
10
10
  def self.load(*filenames)
11
11
  filenames.each do |filename|
12
- recurse(YAML.load(File.open(filename)), @@config)
12
+ from_hash(YAML.load(File.open(filename)))
13
13
  end
14
14
  end
15
15
 
16
16
  def self.clear
17
- @@config = new
17
+ @@root = new
18
18
  nil
19
19
  end
20
20
 
21
21
  def [](key)
22
- @config[key.to_s]
22
+ value = @hash[key.to_s]
23
23
  end
24
24
 
25
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
26
+ raise "Not allowed to overwrite nested entities" if @hash[key.to_s].is_a?(AppConf)
27
+ @hash[key.to_s] = value
28
28
  end
29
29
 
30
30
  def method_missing(method, *args, &block)
@@ -36,15 +36,14 @@ class AppConf
36
36
  end
37
37
 
38
38
  def self.method_missing(method, *args, &block)
39
- @@config.send(method, *args)
39
+ @@root.send(method, *args)
40
40
  end
41
41
 
42
- private
43
- def self.recurse(hash, app_config)
42
+ def self.from_hash(hash, app_config = @@root)
44
43
  hash.each do |key, value|
45
44
  if value.is_a?(Hash)
46
45
  new_app_config = new
47
- value = recurse(value, app_config[key] || new_app_config)
46
+ value = from_hash(value, app_config[key] || new_app_config)
48
47
  end
49
48
  app_config[key] = value if new_app_config.nil? || value === new_app_config
50
49
  end
@@ -9,6 +9,12 @@ describe AppConf do
9
9
  AppConf.load("#{@dir}/config.yml")
10
10
  end
11
11
 
12
+ it 'creates from a hash' do
13
+ AppConf.clear
14
+ AppConf.from_hash({:user => {:name => {:first => 'Joe'}}})
15
+ AppConf.user.name.first.must_equal 'Joe'
16
+ end
17
+
12
18
  it 'works with dot notation' do
13
19
  AppConf.fullname.must_equal 'Joe Bloggs'
14
20
  end
@@ -73,10 +79,6 @@ describe AppConf do
73
79
  end
74
80
 
75
81
  describe 'limitations' do
76
- it 'cannot be instantiated directly' do
77
- AppConf.new.must_be_nil
78
- end
79
-
80
82
  it 'returns nil when unknown key specified' do
81
83
  AppConf.unknown.must_be_nil
82
84
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Phil Thompson
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-27 00:00:00 +00:00
17
+ date: 2011-03-05 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -29,7 +29,7 @@ extra_rdoc_files: []
29
29
  files:
30
30
  - .gitignore
31
31
  - LICENSE
32
- - README.md
32
+ - README.markdown
33
33
  - Rakefile
34
34
  - app_conf.gemspec
35
35
  - lib/app_conf.rb