app_conf 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -13,19 +13,23 @@ Installation
13
13
 
14
14
  Example
15
15
  ----------------------------------
16
+
16
17
  config.yml
18
+
17
19
  ---
18
20
  fullname: Joe Bloggs
19
21
  user:
20
22
  name: Joe
21
23
 
22
24
  other.yml
25
+
23
26
  ---
24
27
  user:
25
28
  address:
26
29
  street: 1 Some Road
27
30
 
28
31
  Code:
32
+
29
33
  AppConf.load('config.yml', 'other.yml')
30
34
  AppConf.fullname -> 'Joe Blogs'
31
35
  AppConf.user.name -> 'Joe'
@@ -33,39 +37,50 @@ Code:
33
37
 
34
38
  Syntax
35
39
  ----------------------------------
40
+
36
41
  Load multiple files at once:
42
+
37
43
  AppConf.load(*filenames)
38
44
 
39
45
  Or individually:
46
+
40
47
  AppConf.load(filename1)
41
48
  AppConf.load(filename2)
42
49
 
43
50
  Use either method calls or hash syntax:
51
+
44
52
  AppConf.fullname
45
53
  AppConf[:fullname]
46
54
  AppConf['fullname']
47
55
 
48
56
  Infinitely nested keys:
57
+
49
58
  AppConf.multiple.nested.keys
50
59
 
51
60
  Override existing values:
61
+
52
62
  AppConf.loaded.from.yaml = 'can override'
53
63
  AppConf['loaded']['from']['yaml'] = 'can override'
54
64
 
55
65
  Set new values:
66
+
56
67
  AppConf.non_existing_value = 'can set'
57
68
 
58
69
  Clear entire tree:
70
+
59
71
  AppConf.clear
60
72
 
61
73
  Returns nil for non-existent keys:
74
+
62
75
  AppConf.non_existing -> nil
63
76
  AppConf.non_existing.name -> NoMethodError: undefined method 'name' for nil:NilClass
64
77
 
65
78
  Use `from_hash` to create non-existent nodes:
79
+
66
80
  AppConf.from_hash({...})
67
81
 
68
82
  Not dependent on Rails but easy to use with it. For example:
83
+
69
84
  AppConf.load('config.yml', "#{Rails.env}.yml")
70
85
 
71
86
  Other stuff
@@ -84,5 +99,6 @@ Why
84
99
  Known Issues
85
100
  ----------------------------------
86
101
  Cannot assign values to unknown nested keys because they return nil (create the tree first):
102
+
87
103
  AppConf.from_hash({:non_existing => {:name => 'bla'}})
88
104
 
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ task :install do
14
14
 
15
15
  result = `gem build #{gemspec_path} 2>&1`
16
16
  if result =~ /Successfully built/
17
- system "gem uninstall -x #{spec.name} 2>&1"
17
+ system "gem uninstall -I #{spec.name} 2>&1"
18
18
  system "gem install #{spec.file_name} --no-rdoc --no-ri 2>&1"
19
19
  else
20
20
  raise result
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.4'
5
+ s.version = '0.2.5'
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)'
@@ -13,5 +13,7 @@ Gem::Specification.new do |s|
13
13
  s.test_files = `git ls-files -- spec/*`.split("\n")
14
14
 
15
15
  s.require_path = 'lib'
16
+
17
+ s.add_development_dependency 'fakefs'
16
18
  end
17
19
 
data/lib/app_conf.rb CHANGED
@@ -12,6 +12,16 @@ class AppConf
12
12
  end
13
13
  end
14
14
 
15
+ def self.save(key, filename)
16
+ mode = File.exist?(filename) ? 'r+' : 'w+'
17
+ File.open(filename, mode) do |f|
18
+ while f.readline =~ /^#/; end unless f.eof?
19
+ hash = {key.to_s => @@root[key].to_hash}
20
+ YAML.dump(hash, f)
21
+ f.truncate(f.pos)
22
+ end
23
+ end
24
+
15
25
  def self.clear
16
26
  @@root = new
17
27
  nil
@@ -1,112 +1,143 @@
1
1
  $LOAD_PATH << 'lib'
2
+ require 'fakefs/safe'
2
3
  require 'minitest/autorun'
3
4
  require 'app_conf'
4
5
 
5
6
  describe AppConf do
7
+ before(:each) do
8
+ FakeFS.activate!
9
+
10
+ config =
11
+ "fullname: Joe Bloggs
12
+ user:
13
+ name:
14
+ first: Joe
15
+ "
16
+ other =
17
+ "user:
18
+ address:
19
+ street: 1 Some Road
20
+ "
21
+ File.open('config.yml', 'w') {|f| f.write(config) }
22
+ File.open('other.yml', 'w') {|f| f.write(other)}
23
+ AppConf.clear
24
+ AppConf.load("config.yml")
25
+ end
26
+
27
+ after(:each) do
28
+ FakeFS.deactivate!
29
+ end
30
+
6
31
  it 'returns nil on empty tree' do
32
+ AppConf.clear
7
33
  AppConf.user.must_be_nil
8
34
  end
9
35
 
10
- describe 'General' do
11
- before(:each) do
12
- @dir = File.dirname(__FILE__)
13
- AppConf.clear
14
- AppConf.load("#{@dir}/config.yml")
36
+ describe 'save' do
37
+ it 'saves a key and children to yml' do
38
+ AppConf.save :user, 'temp.yml'
39
+ YAML.load_file('temp.yml').must_equal({'user' => {'name' => {'first' => 'Joe'}}})
15
40
  end
16
41
 
17
- describe 'inspect' do
18
- it 'outputs a tree hash' do
19
- AppConf.load("#{@dir}/other.yml")
20
- AppConf.to_hash.must_equal({'fullname' => 'Joe Bloggs', 'user' => {'name' => {'first' => 'Joe'}, 'address' => {'street' => '1 Some Road'}}})
21
- end
42
+ it 'skips past comments' do
43
+ File.open('config.yml', 'w') {|f| f.write("# comment 1\n# comment 2\n\nRest of the config")}
44
+ AppConf.save :user, 'config.yml'
45
+ File.read('config.yml').must_equal "# comment 1\n# comment 2\n\n--- \nuser: \n name: \n first: Joe\n"
22
46
  end
47
+ end
23
48
 
24
- describe 'keys' do
25
- it 'returns them' do
26
- AppConf.from_hash(:users => {:joe => nil, :mark => nil})
27
-
28
- AppConf.users.keys.must_equal %w(joe mark)
29
- end
49
+ describe 'to_hash' do
50
+ it 'outputs a hash map' do
51
+ AppConf.load("other.yml")
52
+ AppConf.to_hash.must_equal({'fullname' => 'Joe Bloggs', 'user' => {'name' => {'first' => 'Joe'}, 'address' => {'street' => '1 Some Road'}}})
30
53
  end
54
+ end
31
55
 
32
- it 'creates from a hash' do
33
- AppConf.clear
34
- AppConf.from_hash(:user => {:name => {:first => 'Joe'}})
35
- AppConf.user.name.first.must_equal 'Joe'
36
- end
56
+ describe 'keys' do
57
+ it 'returns them' do
58
+ AppConf.from_hash(:users => {:joe => nil, :mark => nil})
37
59
 
38
- it 'works with dot notation' do
39
- AppConf.fullname.must_equal 'Joe Bloggs'
60
+ AppConf.users.keys.must_equal %w(joe mark)
40
61
  end
62
+ end
41
63
 
42
- it 'works with hash notation' do
43
- AppConf[:fullname].must_equal 'Joe Bloggs'
44
- AppConf['fullname'].must_equal 'Joe Bloggs'
45
- end
64
+ it 'creates from a hash' do
65
+ AppConf.clear
66
+ AppConf.from_hash(:user => {:name => {:first => 'Joe'}})
67
+ AppConf.user.name.first.must_equal 'Joe'
68
+ end
46
69
 
47
- describe 'clear' do
48
- it 'clears all keys' do
49
- AppConf.fullname.wont_be_nil
50
- AppConf.clear
51
- AppConf.fullname.must_be_nil
52
- end
70
+ it 'works with dot notation' do
71
+ AppConf.fullname.must_equal 'Joe Bloggs'
72
+ end
53
73
 
54
- it 'does not return AppConf instance' do
55
- AppConf.clear.must_be_nil
56
- end
57
- end
74
+ it 'works with hash notation' do
75
+ AppConf[:fullname].must_equal 'Joe Bloggs'
76
+ AppConf['fullname'].must_equal 'Joe Bloggs'
77
+ end
58
78
 
59
- it 'works with nested dot notation' do
60
- AppConf.user.name.first.must_equal 'Joe'
79
+ describe 'clear' do
80
+ it 'clears all keys' do
81
+ AppConf.fullname.wont_be_nil
82
+ AppConf.clear
83
+ AppConf.fullname.must_be_nil
61
84
  end
62
85
 
63
- it 'works with nested hash notation' do
64
- AppConf[:user][:name][:first].must_equal 'Joe'
65
- AppConf['user']['name']['first'].must_equal 'Joe'
86
+ it 'does not return AppConf instance' do
87
+ AppConf.clear.must_be_nil
66
88
  end
89
+ end
67
90
 
68
- it 'works with mixed notation' do
69
- AppConf[:user][:name][:first].must_equal 'Joe'
70
- AppConf.user['name'].first.must_equal 'Joe'
71
- end
91
+ it 'works with nested dot notation' do
92
+ AppConf.user.name.first.must_equal 'Joe'
93
+ end
72
94
 
73
- it 'works with multiple files' do
74
- AppConf.clear
75
- AppConf.load("#{@dir}/config.yml", "#{@dir}/other.yml")
76
- AppConf.user.address.street.must_equal '1 Some Road'
77
- AppConf.user.name.first.must_equal 'Joe'
78
- end
95
+ it 'works with nested hash notation' do
96
+ AppConf[:user][:name][:first].must_equal 'Joe'
97
+ AppConf['user']['name']['first'].must_equal 'Joe'
98
+ end
79
99
 
80
- it 'loads additional files' do
81
- AppConf.load("#{@dir}/other.yml")
82
- AppConf.user.address.street.must_equal '1 Some Road'
83
- AppConf.user.name.first.must_equal 'Joe'
84
- end
100
+ it 'works with mixed notation' do
101
+ AppConf[:user][:name][:first].must_equal 'Joe'
102
+ AppConf.user['name'].first.must_equal 'Joe'
103
+ end
85
104
 
86
- it 'allows additional keys to be set' do
87
- AppConf.user.name.last = 'Bloggs'
88
- AppConf.user.name.last.must_equal 'Bloggs'
89
- end
105
+ it 'works with multiple files' do
106
+ AppConf.clear
107
+ AppConf.load("config.yml", "other.yml")
108
+ AppConf.user.address.street.must_equal '1 Some Road'
109
+ AppConf.user.name.first.must_equal 'Joe'
110
+ end
90
111
 
91
- it 'allows additional keys to be set with hash notation' do
92
- AppConf.user[:name][:last] = 'Bloggs'
93
- AppConf.user.name.last.must_equal 'Bloggs'
94
- end
112
+ it 'loads additional files' do
113
+ AppConf.load("other.yml")
114
+ AppConf.user.address.street.must_equal '1 Some Road'
115
+ AppConf.user.name.first.must_equal 'Joe'
116
+ end
95
117
 
96
- it 'allows existing keys to be overridden' do
97
- AppConf.user.name.first = 'Jody'
98
- AppConf.user.name.first.must_equal 'Jody'
99
- end
118
+ it 'allows additional keys to be set' do
119
+ AppConf.user.name.last = 'Bloggs'
120
+ AppConf.user.name.last.must_equal 'Bloggs'
121
+ end
100
122
 
101
- describe 'limitations' do
102
- it 'returns nil when unknown key specified' do
103
- AppConf.unknown.must_be_nil
104
- end
123
+ it 'allows additional keys to be set with hash notation' do
124
+ AppConf.user[:name][:last] = 'Bloggs'
125
+ AppConf.user.name.last.must_equal 'Bloggs'
126
+ end
127
+
128
+ it 'allows existing keys to be overridden' do
129
+ AppConf.user.name.first = 'Jody'
130
+ AppConf.user.name.first.must_equal 'Jody'
131
+ end
132
+
133
+ describe 'limitations' do
134
+ it 'returns nil when unknown key specified' do
135
+ AppConf.unknown.must_be_nil
136
+ end
105
137
 
106
- it 'does not allow nested items to be overwritten' do
107
- lambda { AppConf.user.name = 'something' }.must_raise RuntimeError
108
- lambda { AppConf[:user][:name] = 'something' }.must_raise RuntimeError
109
- end
138
+ it 'does not allow nested items to be overwritten' do
139
+ lambda { AppConf.user.name = 'something' }.must_raise RuntimeError
140
+ lambda { AppConf[:user][:name] = 'something' }.must_raise RuntimeError
110
141
  end
111
142
  end
112
143
  end
metadata CHANGED
@@ -1,28 +1,33 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: app_conf
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.5
4
5
  prerelease:
5
- version: 0.2.4
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Phil Thompson
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-05-07 00:00:00 +01:00
14
- default_executable:
15
- dependencies: []
16
-
12
+ date: 2011-12-14 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fakefs
16
+ requirement: &20417420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *20417420
17
25
  description:
18
26
  email: phil@electricvisions.com
19
27
  executables: []
20
-
21
28
  extensions: []
22
-
23
29
  extra_rdoc_files: []
24
-
25
- files:
30
+ files:
26
31
  - .gitignore
27
32
  - LICENSE
28
33
  - README.markdown
@@ -30,37 +35,29 @@ files:
30
35
  - app_conf.gemspec
31
36
  - lib/app_conf.rb
32
37
  - spec/app_conf_spec.rb
33
- - spec/config.yml
34
- - spec/other.yml
35
- has_rdoc: true
36
38
  homepage: https://github.com/PhilT/app_conf
37
39
  licenses: []
38
-
39
40
  post_install_message:
40
41
  rdoc_options: []
41
-
42
- require_paths:
42
+ require_paths:
43
43
  - lib
44
- required_ruby_version: !ruby/object:Gem::Requirement
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
45
  none: false
46
- requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- version: "0"
50
- required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  none: false
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
55
  version: 1.3.6
56
56
  requirements: []
57
-
58
57
  rubyforge_project:
59
- rubygems_version: 1.6.2
58
+ rubygems_version: 1.8.6
60
59
  signing_key:
61
60
  specification_version: 3
62
61
  summary: Simplest YAML Backed Application Wide Configuration (AppConfig)
63
- test_files:
62
+ test_files:
64
63
  - spec/app_conf_spec.rb
65
- - spec/config.yml
66
- - spec/other.yml
data/spec/config.yml DELETED
@@ -1,5 +0,0 @@
1
- fullname: Joe Bloggs
2
- user:
3
- name:
4
- first: Joe
5
-
data/spec/other.yml DELETED
@@ -1,4 +0,0 @@
1
- user:
2
- address:
3
- street: 1 Some Road
4
-