omniconf 0.0.1.pre → 0.0.1

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.
@@ -1,8 +1,7 @@
1
- development:
2
- api:
3
- username: user
4
- password: pass
5
1
  test:
6
- api:
7
- username: test_user
8
- password: test_password
2
+ yaml_key: yaml_value
3
+ nested:
4
+ yaml_key: nested_yaml_value
5
+ overridden: by_yaml
6
+ development:
7
+ yaml_key: yaml_value_dev
@@ -2,36 +2,72 @@ require 'spec_helper'
2
2
  require 'omniconf/adapters/active_record'
3
3
 
4
4
  describe Omniconf::Adapter::ActiveRecord do
5
- before do
6
- @database = Omniconf::Adapter::ActiveRecord.new({
5
+ def load_configuration
6
+ @adapter = Omniconf::Adapter::ActiveRecord.new(:active_record, {
7
7
  :model_name => :ConfigValue,
8
8
  :environment => 'test',
9
- :config_file => File.join(File.dirname(__FILE__),
10
- '../../fixtures/omniconf/adapters/active_record/config/database.yml'
9
+ :config_file => File.expand_path(
10
+ '../../../fixtures/omniconf/adapters/active_record/config/database.yml',
11
+ __FILE__
11
12
  )
12
13
  })
13
- @database.setup
14
+ @adapter.setup
14
15
 
15
- require File.join(File.dirname(__FILE__),
16
- '../../fixtures/omniconf/adapters/active_record/db/schema.rb'
16
+ load File.expand_path(
17
+ '../../../fixtures/omniconf/adapters/active_record/db/schema.rb', __FILE__
17
18
  )
18
19
 
19
- @database.load_configuration!
20
+ @adapter.load_configuration!
20
21
  end
21
22
 
22
- describe "#[]" do
23
- it "returns configuration values from database" do
24
- @database.configuration.foo.should == 'bar'
25
- @database.configuration.foo2.should == 'bar2'
23
+ describe "#configuration" do
24
+ before do
25
+ Omniconf.stub(:merge_configuration!)
26
+ load_configuration
26
27
  end
27
28
 
28
- pending "it doesn't cast values to String" # TODO serialize them and keep type
29
- end
29
+ let(:configuration) { @adapter.configuration }
30
+
31
+ describe "getting values" do
32
+ it "returns a value" do
33
+ configuration.ar_key.should == 'ar_value'
34
+ end
35
+
36
+ it "returns a nested value" do
37
+ configuration.nested.ar_key.should == 'nested_ar_value'
38
+ end
39
+ end
40
+
41
+ describe "setting values" do
42
+ it "updates configuration values into database" do
43
+ configuration.ar_key.should == 'ar_value'
44
+ configuration.ar_key = 'new_ar_value'
45
+ configuration.ar_key.should == 'new_ar_value'
46
+ end
47
+
48
+ it "creates configuration values into database" do
49
+ configuration.new_ar_key.should be_nil
50
+ configuration.new_ar_key = 'new_ar_value'
51
+ configuration.new_ar_key.should == 'new_ar_value'
52
+ end
53
+
54
+ it "it doesn't cast values to String" # TODO serialize them and keep type
55
+ end
56
+
57
+ describe "#get_or_default" do
58
+ it "creates a new default value" do
59
+ configuration.newValue.should be_nil
60
+ @adapter.should_receive(:set_value).with(['newValue'], 'a')
61
+ configuration.get_or_default('newValue', 'a').should == 'a'
62
+ configuration.get_or_default('newValue', 'b').should == 'a'
63
+ end
30
64
 
31
- describe "#get_or_default" do
32
- it "sets default value if new" do
33
- @database.model.get_or_default('newValue', 'a').should == 'a'
34
- @database.model.get_or_default('newValue', 'b').should == 'a'
65
+ it "creates a new default nested value" do
66
+ configuration.nested.newValue.should be_nil
67
+ @adapter.should_receive(:set_value).with(['nested', 'newValue'], 'a')
68
+ configuration.nested.get_or_default('newValue', 'a').should == 'a'
69
+ configuration.nested.get_or_default('newValue', 'b').should == 'a'
70
+ end
35
71
  end
36
72
  end
37
73
  end
@@ -2,26 +2,86 @@ require 'spec_helper'
2
2
  require 'omniconf/adapters/yaml'
3
3
 
4
4
  describe Omniconf::Adapter::Yaml do
5
- def load_environment env
6
- @yaml = Omniconf::Adapter::Yaml.new({
5
+ def load_configuration env
6
+ @adapter = Omniconf::Adapter::Yaml.new(:yaml_conf, {
7
7
  :environment => env,
8
8
  :file => File.join(File.dirname(__FILE__),
9
9
  '../../fixtures/omniconf/adapters/yaml/config/settings.yml'
10
10
  )
11
11
  })
12
- @yaml.load_configuration!
12
+ @adapter.load_configuration!
13
13
  end
14
14
 
15
- it "allows to get nested attributes" do
16
- load_environment 'test'
17
- @yaml.configuration.api.username.should == 'test_user'
18
- @yaml.configuration.api.password.should == 'test_password'
19
- end
15
+ describe "#configuration" do
16
+ before do
17
+ Omniconf.stub(:merge_configuration!)
18
+ load_configuration('test')
19
+ end
20
+
21
+ let(:configuration) { @adapter.configuration }
22
+
23
+ describe "getting values" do
24
+ it "returns a value" do
25
+ configuration.yaml_key.should == 'yaml_value'
26
+ end
27
+
28
+ it "returns a nested value" do
29
+ configuration.nested.yaml_key.should == 'nested_yaml_value'
30
+ end
31
+ end
32
+
33
+ describe "setting values" do
34
+ it "fails to update a value" do
35
+ configuration.yaml_key.should_not be_nil
36
+ expect {
37
+ configuration.yaml_key = 'whatever'
38
+ }.to raise_error Omniconf::ReadOnlyConfigurationValue
39
+ end
40
+
41
+ it "fails to update a nested value" do
42
+ configuration.nested.yaml_key.should_not be_nil
43
+ expect {
44
+ configuration.nested.yaml_key = 'whatever'
45
+ }.to raise_error Omniconf::ReadOnlyConfigurationValue
46
+ end
47
+
48
+ it "fails to create a value" do
49
+ configuration.new_yaml_key.should be_nil
50
+ expect {
51
+ configuration.new_yaml_key = 'whatever'
52
+ }.to raise_error Omniconf::ReadOnlyConfigurationValue
53
+ end
54
+
55
+ it "fails to create a nested value" do
56
+ configuration.nested.new_yaml_key.should be_nil
57
+ expect {
58
+ configuration.nested.new_yaml_key = 'whatever'
59
+ }.to raise_error Omniconf::ReadOnlyConfigurationValue
60
+ end
61
+ end
62
+
63
+ describe "#get_or_default" do
64
+ it "fails to create a new default value" do
65
+ configuration.newValue.should be_nil
66
+ expect {
67
+ configuration.get_or_default('newValue', 'a').should == 'a'
68
+ }.to raise_error Omniconf::ReadOnlyConfigurationValue
69
+ configuration.newValue.should be_nil
70
+ end
71
+
72
+ it "fails to create a new nested default value" do
73
+ configuration.nested.newValue.should be_nil
74
+ expect {
75
+ configuration.nested.get_or_default('newValue', 'a').should == 'a'
76
+ }.to raise_error Omniconf::ReadOnlyConfigurationValue
77
+ configuration.nested.newValue.should be_nil
78
+ end
79
+ end
20
80
 
21
- it "loads the right environment" do
22
- load_environment 'development'
23
- @yaml.configuration.api.username.should == 'user'
24
- @yaml.configuration.api.password.should == 'pass'
81
+ it "loads the right environment" do
82
+ load_configuration 'development'
83
+ configuration.yaml_key.should == 'yaml_value_dev'
84
+ end
25
85
  end
26
86
  end
27
87
 
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+ require 'omniconf/configuration'
3
+
4
+ describe Omniconf::Configuration do
5
+ let(:hash) do
6
+ {
7
+ 'key' => 'level0',
8
+ 'nested0' => {
9
+ 'key' => 'level1',
10
+ 'nested1' => {
11
+ 'key' => 'level2',
12
+ 'nested2' => {
13
+ 'answer' => 42
14
+ }
15
+ }
16
+ }
17
+ }
18
+ end
19
+
20
+ describe "#initialize" do
21
+ it "takes a hash as argument" do
22
+ expect { Omniconf::Configuration.new nil, hash }.to_not raise_error
23
+ end
24
+ end
25
+
26
+ context "with a configuration" do
27
+ before do
28
+ @config = Omniconf::Configuration.new nil, hash
29
+ end
30
+
31
+ describe "getting values" do
32
+ it "returns a value" do
33
+ @config.key.should == 'level0'
34
+ end
35
+
36
+ it "returns a nested value" do
37
+ @config.nested0.key.should == 'level1'
38
+ @config.nested0.nested1.nested2.answer.should == 42
39
+ end
40
+
41
+ it "#nil? returns true for non-existant values" do
42
+ @config.nope_404.should be_nil
43
+ end
44
+
45
+ it "is false when a non-existant value is coerced to a boolean" do
46
+ (!!(@config.nope_404)).should == false
47
+ (@config.nope_404 ? true : false).should == false
48
+ end
49
+ end
50
+
51
+ describe "setting values" do
52
+ it "updates a value" do
53
+ @config.key.should_not == 'new_level0'
54
+ @config.key = 'new_level0'
55
+ @config.key.should == 'new_level0'
56
+ end
57
+
58
+ it "creates a new value" do
59
+ @config.key2.should be_nil
60
+ @config.key2 = 'new_value'
61
+ @config.key2.should == 'new_value'
62
+ end
63
+
64
+ it "updates a nested value" do
65
+ @config.to_hash.should == hash
66
+ @config.nested0.nested1.key.should_not == :new
67
+ @config.nested0.nested1.key = :new
68
+ @config.nested0.nested1.key.should == :new
69
+ expected = hash
70
+ expected['nested0']['nested1']['key'] = :new
71
+ @config.to_hash.should == expected
72
+ end
73
+
74
+ it "creates a new nested value" do
75
+ @config.to_hash.should == hash
76
+ @config.nested0.new.should be_nil
77
+ @config.nested0.new = 'new_value'
78
+ @config.nested0.new.should == 'new_value'
79
+ expected = hash
80
+ expected['nested0']['new'] = 'new_value'
81
+ @config.to_hash.should == expected
82
+ end
83
+ end
84
+ end
85
+ end
86
+
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+ require 'omniconf/helpers'
3
+
4
+ describe Hash do
5
+ describe "#recursive_stringify_keys!" do
6
+ it "stringify hash's keys recursively" do
7
+ hash = {:a => 1, :b => {:aa => {:aaa => 'AAA', 'bbb' => nil}}}
8
+ hash.recursive_stringify_keys!
9
+ hash.should == {'a' => 1, 'b' => {'aa' => {'aaa' => 'AAA', 'bbb' => nil}}}
10
+ end
11
+ end
12
+ end
13
+
@@ -16,47 +16,93 @@ describe Omniconf do
16
16
  end
17
17
  end
18
18
 
19
- describe "#load_configuration!" do
20
- def setup
19
+ context "with some fixtures" do
20
+ before do
21
21
  Omniconf.setup do |config|
22
- config.load_configuration = false
22
+ config.load_configuration = true
23
23
  config.sources = {
24
24
  :yaml => {
25
25
  :type => :yaml,
26
26
  :environment => 'test',
27
- :file => File.join(File.dirname(__FILE__),
28
- 'fixtures/omniconf/adapters/yaml/config/settings.yml'
27
+ :file => File.expand_path(
28
+ '../fixtures/omniconf/adapters/yaml/config/settings.yml', __FILE__
29
29
  )
30
30
  },
31
31
  :database => {
32
32
  :type => :active_record,
33
33
  :model_name => :ConfigValue,
34
34
  :environment => 'test',
35
- :config_file => File.join(File.dirname(__FILE__),
36
- 'fixtures/omniconf/adapters/active_record/config/database.yml'
35
+ :config_file => File.expand_path(
36
+ '../fixtures/omniconf/adapters/active_record/config/database.yml',
37
+ __FILE__
37
38
  )
38
39
  }
39
40
  }
40
41
  end
41
- end
42
42
 
43
- def insert_db_fixtures
44
- @database = Omniconf.settings.sources[:database][:adapter]
45
- @database.setup
46
-
47
- require File.join(File.dirname(__FILE__),
48
- 'fixtures/omniconf/adapters/active_record/db/schema.rb'
43
+ # insert some fixtures into DB
44
+ load File.expand_path(
45
+ '../fixtures/omniconf/adapters/active_record/db/schema.rb', __FILE__
49
46
  )
47
+
48
+ Omniconf.reload_configuration!
50
49
  end
51
50
 
52
- it "loads and merges all configurations" do
53
- setup
54
- insert_db_fixtures
51
+ describe "#configuration" do
52
+ let(:config) { Omniconf.configuration }
53
+
54
+ it "returns an instance of Omniconf::Configuration" do
55
+ config.should be_an_instance_of Omniconf::Configuration
56
+ end
57
+
58
+ describe "getting values" do
59
+ it "loads and merges all configurations" do
60
+ config.yaml_key.should == 'yaml_value'
61
+ config.nested.yaml_key.should == 'nested_yaml_value'
62
+ config.ar_key.should == 'ar_value'
63
+ config.nested.ar_key.should == 'nested_ar_value'
64
+ end
65
+ end
66
+
67
+ describe "setting values" do
68
+ it "updates a value on its original source" do
69
+ db_adapter = Omniconf.settings.sources[:database][:adapter]
70
+ db_adapter.should_receive(:set_value).with(['ar_key'], 'new_ar_value')
71
+
72
+ config.ar_key = 'new_ar_value'
73
+
74
+ config.ar_key.should == 'new_ar_value'
75
+ Omniconf.sources[:database].ar_key.should == 'new_ar_value'
76
+ end
77
+
78
+ it "creates a new value on the given source" do
79
+ config.new_value.should be_nil
80
+ db_adapter = Omniconf.settings.sources[:database][:adapter]
81
+ db_adapter.should_receive(:set_value).with(['new_value'], 'new_ar_value')
55
82
 
56
- Omniconf.load_configuration!
83
+ Omniconf.sources[:database].new_value = 'new_ar_value'
57
84
 
58
- Omniconf.configuration.api.password.should == 'test_password'
59
- Omniconf.configuration.foo.should == 'bar'
85
+ Omniconf.sources[:database].new_value.should == 'new_ar_value'
86
+ config.new_value.should == 'new_ar_value'
87
+ end
88
+
89
+ it "fails to create a new value with no sources" do
90
+ # How the hell would I know where to store it?!
91
+ config.no.should be_nil
92
+ expect {
93
+ config.no = 'whatever'
94
+ }.to raise_error Omniconf::UnknownConfigurationValue
95
+ end
96
+
97
+ it "fails to create a new value with no parents" do
98
+ db_adapter = Omniconf.settings.sources[:database][:adapter]
99
+ Omniconf.sources[:database].no.should be_nil
100
+
101
+ expect {
102
+ Omniconf.sources[:database].no.no = 'new_ar_value'
103
+ }.to raise_error Omniconf::UnknownConfigurationValue
104
+ end
105
+ end
60
106
  end
61
107
  end
62
108
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,7 @@
1
- require 'lib/omniconf/adapters/base'
2
- require 'spec/support/mock'
1
+ require 'omniconf'
2
+
3
+ Omniconf.setup do |config|
4
+ config.load_configuration = false
5
+ config.logger_level = Logger::ERROR # don't pollute specs output
6
+ end
3
7
 
metadata CHANGED
@@ -1,167 +1,128 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: omniconf
3
- version: !ruby/object:Gem::Version
4
- hash: 961915968
5
- prerelease: 6
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- - pre
11
- version: 0.0.1.pre
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
12
6
  platform: ruby
13
- authors:
14
- - "C\xC3\xA9dric Felizard"
7
+ authors:
8
+ - Cedric Felizard
15
9
  autorequire:
16
10
  bindir: bin
17
11
  cert_chain: []
18
-
19
- date: 2012-02-01 00:00:00 Z
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: recursive-open-struct
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
33
- type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
12
+ date: 2012-02-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
36
15
  name: rake
37
- prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
16
+ requirement: &2156203800 !ruby/object:Gem::Requirement
39
17
  none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.9'
47
22
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: rspec
51
23
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
24
+ version_requirements: *2156203800
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &2156201760 !ruby/object:Gem::Requirement
53
28
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
- version: "0"
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.8'
61
33
  type: :development
62
- version_requirements: *id003
63
- - !ruby/object:Gem::Dependency
64
- name: sqlite3
65
34
  prerelease: false
66
- requirement: &id004 !ruby/object:Gem::Requirement
35
+ version_requirements: *2156201760
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &2156200400 !ruby/object:Gem::Requirement
67
39
  none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '1.3'
75
44
  type: :development
76
- version_requirements: *id004
77
- - !ruby/object:Gem::Dependency
78
- name: activerecord
79
45
  prerelease: false
80
- requirement: &id005 !ruby/object:Gem::Requirement
46
+ version_requirements: *2156200400
47
+ - !ruby/object:Gem::Dependency
48
+ name: activerecord
49
+ requirement: &2156199340 !ruby/object:Gem::Requirement
81
50
  none: false
82
- requirements:
83
- - - ">="
84
- - !ruby/object:Gem::Version
85
- hash: 3
86
- segments:
87
- - 0
88
- version: "0"
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
89
55
  type: :development
90
- version_requirements: *id005
91
- description: Merge configurations from multiple backends for easy use in a Rails application.
92
- email:
56
+ prerelease: false
57
+ version_requirements: *2156199340
58
+ description: Merge configurations from multiple back-ends for easy use in a complex
59
+ application.
60
+ email:
93
61
  - cedric@picklive.com
94
62
  executables: []
95
-
96
63
  extensions: []
97
-
98
64
  extra_rdoc_files: []
99
-
100
- files:
65
+ files:
101
66
  - .gitignore
102
67
  - .rspec
103
68
  - .rvmrc
69
+ - .travis.yml
104
70
  - Gemfile
71
+ - LICENSE
105
72
  - README.md
106
73
  - Rakefile
107
74
  - lib/omniconf.rb
108
75
  - lib/omniconf/adapters/active_record.rb
109
76
  - lib/omniconf/adapters/base.rb
77
+ - lib/omniconf/adapters/read_only.rb
110
78
  - lib/omniconf/adapters/yaml.rb
79
+ - lib/omniconf/blank_state.rb
80
+ - lib/omniconf/configuration.rb
81
+ - lib/omniconf/errors.rb
82
+ - lib/omniconf/helpers.rb
111
83
  - lib/omniconf/settings.rb
112
84
  - lib/omniconf/version.rb
113
85
  - omniconf.gemspec
114
- - spec/data.sqlite3
115
86
  - spec/fixtures/omniconf/adapters/active_record/config/database.yml
116
87
  - spec/fixtures/omniconf/adapters/active_record/db/schema.rb
117
88
  - spec/fixtures/omniconf/adapters/yaml/config/settings.yml
118
89
  - spec/omniconf/adapters/active_record_spec.rb
119
90
  - spec/omniconf/adapters/yaml_spec.rb
91
+ - spec/omniconf/configuration_spec.rb
92
+ - spec/omniconf/helpers_spec.rb
120
93
  - spec/omniconf_spec.rb
121
94
  - spec/spec_helper.rb
122
- - spec/support/mock.rb
123
95
  homepage: https://github.com/Picklive/omniconf
124
96
  licenses: []
125
-
126
97
  post_install_message:
127
98
  rdoc_options: []
128
-
129
- require_paths:
99
+ require_paths:
130
100
  - lib
131
- required_ruby_version: !ruby/object:Gem::Requirement
101
+ required_ruby_version: !ruby/object:Gem::Requirement
132
102
  none: false
133
- requirements:
134
- - - ">="
135
- - !ruby/object:Gem::Version
136
- hash: 3
137
- segments:
138
- - 0
139
- version: "0"
140
- required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
108
  none: false
142
- requirements:
143
- - - ">"
144
- - !ruby/object:Gem::Version
145
- hash: 25
146
- segments:
147
- - 1
148
- - 3
149
- - 1
150
- version: 1.3.1
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
151
113
  requirements: []
152
-
153
114
  rubyforge_project: omniconf
154
- rubygems_version: 1.8.10
115
+ rubygems_version: 1.8.11
155
116
  signing_key:
156
117
  specification_version: 3
157
118
  summary: Merge multiple configuration sources into one.
158
- test_files:
159
- - spec/data.sqlite3
119
+ test_files:
160
120
  - spec/fixtures/omniconf/adapters/active_record/config/database.yml
161
121
  - spec/fixtures/omniconf/adapters/active_record/db/schema.rb
162
122
  - spec/fixtures/omniconf/adapters/yaml/config/settings.yml
163
123
  - spec/omniconf/adapters/active_record_spec.rb
164
124
  - spec/omniconf/adapters/yaml_spec.rb
125
+ - spec/omniconf/configuration_spec.rb
126
+ - spec/omniconf/helpers_spec.rb
165
127
  - spec/omniconf_spec.rb
166
128
  - spec/spec_helper.rb
167
- - spec/support/mock.rb
data/spec/data.sqlite3 DELETED
Binary file