app_conf 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  AppConf
2
2
  =======
3
- Simplest YAML Backed Application Configuration
3
+ Simplest YAML Backed Application Wide Configuration (AppConfig)
4
4
 
5
5
  Installation
6
6
  ----------------------------------
7
7
  gem install app_conf
8
8
 
9
- Usage
9
+ Example
10
10
  ----------------------------------
11
11
  config.yml
12
12
  ---
@@ -28,21 +28,50 @@ Code:
28
28
 
29
29
  Syntax
30
30
  ----------------------------------
31
+ * Load multiple files at once:
32
+
31
33
  AppConf.load(*filenames)
34
+
35
+ * Or individually:
36
+
37
+ AppConf.load(filename1)
38
+ AppConf.load(filename2)
39
+
40
+ * Infinitely nested keys:
41
+
32
42
  AppConf.multiple.nested.keys
43
+
44
+ * Override existing values:
45
+
33
46
  AppConf.loaded.from.yaml = 'can override'
47
+
48
+ * Set new values:
49
+
34
50
  AppConf.non.existing.value = 'can set'
35
51
 
52
+ * Clear entire tree:
53
+
54
+ AppConf.clear
55
+
56
+ * Returns nil for non-existent keys:
57
+
58
+ 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:
62
+
63
+ AppConf.load('config.yml', "#{Rails.env}.yml")
64
+
36
65
  Other stuff
37
66
  ----------------------------------
38
67
  * Works with Ruby 1.9.2
39
68
  * No gem dependencies
40
- * Tested with MiniTest::Spec
41
- * Not dependent on Rails but easy to use with it. For example:
42
- `AppConf.load('config.yml', "#{Rails.env}.yml")`
69
+ * Fully tested with MiniTest::Spec
70
+ * Packaged as a Gem on RubyGems.org
43
71
 
44
72
  Why
45
73
  ----------------------------------
46
- * Because I wanted to write the simplest app config possible
47
- * Because I can
74
+ * Because I wanted to write the simplest useful app config possible
75
+ * Others are either too simple or incomplete, lack documentation or aren't Gem installable
76
+ * Because I can :-)
48
77
 
data/app_conf.gemspec CHANGED
@@ -2,10 +2,10 @@ require 'base64'
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'app_conf'
5
- s.version = '0.1.1'
5
+ s.version = '0.1.2'
6
6
  s.authors = 'Phil Thompson'
7
7
  s.email = Base64.decode64("cGhpbEBlbGVjdHJpY3Zpc2lvbnMuY29t\n")
8
- s.summary = 'Simplest YAML Backed Application Configuration'
8
+ s.summary = 'Simplest YAML Backed Application Wide Configuration (AppConfig)'
9
9
  s.homepage = 'https://github.com/PhilT/app_conf'
10
10
  s.required_rubygems_version = '>= 1.3.6'
11
11
 
data/lib/app_conf.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  require 'yaml'
2
2
 
3
3
  class AppConf
4
- attr_reader :config
5
-
6
4
  private_class_method :new
7
5
  def initialize
8
6
  @config = {}
@@ -15,6 +13,11 @@ class AppConf
15
13
  end
16
14
  end
17
15
 
16
+ def self.clear
17
+ @@config = new
18
+ nil
19
+ end
20
+
18
21
  def method_missing(method, *args, &block)
19
22
  method = method.to_s
20
23
  if method[-1] == '='
@@ -31,10 +34,13 @@ class AppConf
31
34
  end
32
35
 
33
36
  private
34
- def self.recurse(yaml, app_config)
35
- yaml.each do |k, v|
36
- v = recurse(v, app_config.send(k) || new) if v.is_a?(Hash)
37
- app_config.config[k] = v
37
+ def self.recurse(hash, app_config)
38
+ hash.each do |key, value|
39
+ if value.is_a?(Hash)
40
+ new_app_config = new
41
+ value = recurse(value, app_config.send(key) || new_app_config)
42
+ end
43
+ app_config.send("#{key}=", value) if new_app_config.nil? || value === new_app_config
38
44
  end
39
45
  app_config
40
46
  end
@@ -5,44 +5,65 @@ require 'app_conf'
5
5
  describe AppConf do
6
6
  before(:each) do
7
7
  @dir = File.dirname(__FILE__)
8
- end
9
-
10
- it 'cannot be instantiated directly' do
11
- AppConf.new.must_be_nil
8
+ AppConf.clear
9
+ AppConf.load("#{@dir}/config.yml")
12
10
  end
13
11
 
14
12
  it 'works with dot notation' do
15
- AppConf.load("#{@dir}/config.yml")
16
13
  AppConf.fullname.must_equal 'Joe Bloggs'
17
14
  end
18
15
 
16
+ describe 'clear' do
17
+ it 'clears all keys' do
18
+ AppConf.fullname.wont_be_nil
19
+ AppConf.clear
20
+ AppConf.fullname.must_be_nil
21
+ end
22
+
23
+ it 'does not return AppConf instance' do
24
+ AppConf.clear.must_be_nil
25
+ end
26
+ end
27
+
19
28
  it 'works with nested dot notation' do
20
- AppConf.load("#{@dir}/config.yml")
21
29
  AppConf.user.name.first.must_equal 'Joe'
22
30
  end
23
31
 
24
32
  it 'works with multiple files' do
33
+ AppConf.clear
25
34
  AppConf.load("#{@dir}/config.yml", "#{@dir}/other.yml")
26
35
  AppConf.user.address.street.must_equal '1 Some Road'
27
36
  AppConf.user.name.first.must_equal 'Joe'
28
37
  end
29
38
 
39
+ it 'loads additional files' do
40
+ AppConf.load("#{@dir}/other.yml")
41
+ AppConf.user.address.street.must_equal '1 Some Road'
42
+ AppConf.user.name.first.must_equal 'Joe'
43
+ end
44
+
30
45
  it 'allows additional keys to be set' do
31
- AppConf.load("#{@dir}/config.yml")
32
46
  AppConf.user.name.last = 'Bloggs'
33
47
  AppConf.user.name.last.must_equal 'Bloggs'
34
48
  end
35
49
 
36
50
  it 'allows existing keys to be overridden' do
37
- AppConf.load("#{@dir}/config.yml")
38
51
  AppConf.user.name.first = 'Jody'
39
52
  AppConf.user.name.first.must_equal 'Jody'
40
53
  end
41
54
 
42
- it 'does not allow nested items to be overwritten' do
43
- AppConf.load("#{@dir}/config.yml")
44
- lambda { AppConf.user.name = 'something' }.must_raise RuntimeError
45
- end
55
+ describe 'limitations' do
56
+ it 'cannot be instantiated directly' do
57
+ AppConf.new.must_be_nil
58
+ end
46
59
 
60
+ it 'returns nil when unknown key specified' do
61
+ AppConf.unknown.must_be_nil
62
+ end
63
+
64
+ it 'does not allow nested items to be overwritten' do
65
+ lambda { AppConf.user.name = 'something' }.must_raise RuntimeError
66
+ end
67
+ end
47
68
  end
48
69
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Phil Thompson
@@ -69,7 +69,7 @@ rubyforge_project:
69
69
  rubygems_version: 1.3.7
70
70
  signing_key:
71
71
  specification_version: 3
72
- summary: Simplest YAML Backed Application Configuration
72
+ summary: Simplest YAML Backed Application Wide Configuration (AppConfig)
73
73
  test_files:
74
74
  - spec/app_conf_spec.rb
75
75
  - spec/config.yml