basic_config 0.0.2 → 0.1.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 +16 -5
- data/basic_config.gemspec +1 -1
- data/lib/basic_config.rb +20 -5
- data/spec/basic_config_spec.rb +90 -14
- metadata +2 -2
data/README.md
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
# BasicConfig
|
2
|
-
|
3
|
-
[](http://travis-ci.org/stephan778/basic_config)
|
1
|
+
# BasicConfig [](http://travis-ci.org/stephan778/basic_config)
|
4
2
|
|
5
3
|
Friendly configuration wrapper. If you find yourself using things like:
|
6
4
|
|
@@ -75,10 +73,23 @@ or corrupting some data until you finally notice and track it down back to this
|
|
75
73
|
|
76
74
|
If you are using a `BasicConfig`:
|
77
75
|
```ruby
|
76
|
+
AppConfig = BasicConfig.load_env('config/application.yml', 'development')
|
78
77
|
secret_token = AppConfig.something
|
79
78
|
```
|
80
|
-
you'll get `
|
81
|
-
|
79
|
+
you'll get `BasicConfig::NotFound` exception with a message similar to this:
|
80
|
+
|
81
|
+
Configuration key 'development.something' is missing in config/application.yml
|
82
|
+
|
83
|
+
if you're constructing BasicConfig with `new`, then it'll show source code
|
84
|
+
location where BasicConfig is initialized.
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
AppConfig = BasicConfig.new({ secret_token: 'something' })
|
88
|
+
secret_token = AppConfig.secret_toklen # Note a typo here
|
89
|
+
|
90
|
+
# Will result in exception:
|
91
|
+
# BasicConfig::NotFound: Configuration key 'secret_toklen' is missing in BasicConfig constructed at your_file.rb:5 in `new'
|
92
|
+
```
|
82
93
|
|
83
94
|
*Note:* There is also an `include?` method which you can use to check if
|
84
95
|
particular key exist in your config - `AppConfig.include?(:something)`.
|
data/basic_config.gemspec
CHANGED
data/lib/basic_config.rb
CHANGED
@@ -1,9 +1,24 @@
|
|
1
1
|
require 'yaml'
|
2
2
|
|
3
3
|
class BasicConfig
|
4
|
-
|
4
|
+
class NotFound < RuntimeError
|
5
|
+
attr_reader :name, :scope, :key
|
6
|
+
|
7
|
+
def initialize(name, scope, key)
|
8
|
+
@name = name
|
9
|
+
@scope = scope
|
10
|
+
@key = key
|
11
|
+
|
12
|
+
super("Configuration key '#{scope}#{key}' is missing in #{name}")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(hash, configuration_name = nil, configuration_scope = '')
|
5
17
|
raise ArgumentError, 'Hash can not be nil' if hash.nil?
|
6
18
|
|
19
|
+
@name = configuration_name || "BasicConfig constructed at #{caller[0]}"
|
20
|
+
@scope = configuration_scope
|
21
|
+
|
7
22
|
# Symbolize keys: don't want to add ActiveSupport dependency just for this.
|
8
23
|
@hash = hash.inject({}) do |h, (key, value)|
|
9
24
|
h[key.to_sym] = value
|
@@ -11,7 +26,7 @@ class BasicConfig
|
|
11
26
|
end
|
12
27
|
|
13
28
|
@hash.each do |key, value|
|
14
|
-
@hash[key] = BasicConfig.new(value) if value.is_a?(Hash)
|
29
|
+
@hash[key] = BasicConfig.new(value, @name, [@scope, key, '.'].join) if value.is_a?(Hash)
|
15
30
|
end
|
16
31
|
end
|
17
32
|
|
@@ -28,7 +43,7 @@ class BasicConfig
|
|
28
43
|
raise ArgumentError, 'Getter can not receive any arguments' if !args.empty? || block_given?
|
29
44
|
@hash[meth]
|
30
45
|
else
|
31
|
-
|
46
|
+
raise NotFound.new(@name, @scope, meth)
|
32
47
|
end
|
33
48
|
end
|
34
49
|
|
@@ -45,10 +60,10 @@ class BasicConfig
|
|
45
60
|
end
|
46
61
|
|
47
62
|
def self.load_file(name)
|
48
|
-
BasicConfig.new(YAML.load_file(name))
|
63
|
+
BasicConfig.new(YAML.load_file(name), name)
|
49
64
|
end
|
50
65
|
|
51
66
|
def self.load_env(name, env)
|
52
|
-
BasicConfig.new(YAML.load_file(name)[env])
|
67
|
+
BasicConfig.new(YAML.load_file(name)[env], name, env + '.')
|
53
68
|
end
|
54
69
|
end
|
data/spec/basic_config_spec.rb
CHANGED
@@ -41,28 +41,28 @@ describe BasicConfig do
|
|
41
41
|
end
|
42
42
|
|
43
43
|
it 'correctly handles respond_to? queries' do
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
should respond_to :one
|
45
|
+
should respond_to :three
|
46
|
+
should_not respond_to :unknown
|
47
47
|
end
|
48
|
-
|
49
|
-
it 'raises
|
50
|
-
expect { subject.four }.to raise_error(
|
48
|
+
|
49
|
+
it 'raises BasicConfig::KeyNotFound for unknown keys' do
|
50
|
+
expect { subject.four }.to raise_error(BasicConfig::NotFound)
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'can be converted back to hash' do
|
54
54
|
subject.to_hash.should == symbolized_hash
|
55
55
|
end
|
56
56
|
|
57
|
-
describe '
|
57
|
+
describe '.load_file' do
|
58
58
|
it 'uses YAML to load files' do
|
59
|
-
|
60
|
-
|
61
|
-
BasicConfig.load_file('file')
|
59
|
+
content = { key: 'value' }
|
60
|
+
YAML.stub(:load_file).with('file').and_return(content)
|
61
|
+
BasicConfig.load_file('file').to_hash.should == content
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
-
describe '
|
65
|
+
describe '.load_env' do
|
66
66
|
let(:content) do
|
67
67
|
{
|
68
68
|
'development' => {
|
@@ -73,10 +73,86 @@ describe BasicConfig do
|
|
73
73
|
}
|
74
74
|
}
|
75
75
|
end
|
76
|
+
let(:environment) { 'development' }
|
77
|
+
let(:expected_result) { { value: 'x' } }
|
78
|
+
|
76
79
|
it 'selects env section from YAML loaded file' do
|
77
|
-
YAML.
|
78
|
-
BasicConfig.
|
79
|
-
|
80
|
+
YAML.stub(:load_file).with('file').and_return(content)
|
81
|
+
BasicConfig.load_env('file', environment).to_hash.should == expected_result
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe BasicConfig::NotFound do
|
86
|
+
let(:exception) do
|
87
|
+
begin
|
88
|
+
config.missing_key
|
89
|
+
rescue BasicConfig::NotFound => expected_failure
|
90
|
+
expected_failure
|
91
|
+
end
|
92
|
+
end
|
93
|
+
subject { exception.message }
|
94
|
+
let(:original_config) { BasicConfig.new(hash) }
|
95
|
+
let(:original_scoping) { '' }
|
96
|
+
|
97
|
+
shared_examples_for 'specific failure' do
|
98
|
+
it 'contains the right location' do
|
99
|
+
should include location
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'contains the right key' do
|
103
|
+
should include "'#{missing_key_name}'"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
shared_examples_for 'construction contexts' do
|
108
|
+
context 'when constructed manually' do
|
109
|
+
let(:original_config) { BasicConfig.new(hash) }
|
110
|
+
let(:location) { 'spec/basic_config_spec.rb:109' }
|
111
|
+
|
112
|
+
it_behaves_like 'specific failure'
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'when loaded from YAML' do
|
116
|
+
let(:content) { hash }
|
117
|
+
let(:filename) { 'example.yml' }
|
118
|
+
let(:location) { filename }
|
119
|
+
|
120
|
+
before do
|
121
|
+
YAML.stub(:load_file).with(filename).and_return(content)
|
122
|
+
end
|
123
|
+
|
124
|
+
let(:original_config) { BasicConfig.load_file(filename) }
|
125
|
+
|
126
|
+
it_behaves_like 'specific failure'
|
127
|
+
|
128
|
+
context 'with env' do
|
129
|
+
let(:content) do
|
130
|
+
{
|
131
|
+
'development' => hash,
|
132
|
+
'test' => hash
|
133
|
+
}
|
134
|
+
end
|
135
|
+
let(:environment) { 'development' }
|
136
|
+
let(:original_config) { BasicConfig.load_env(filename, environment) }
|
137
|
+
let(:original_scoping) { 'development.' }
|
138
|
+
|
139
|
+
it_behaves_like 'specific failure'
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'top-level' do
|
145
|
+
let(:config) { original_config }
|
146
|
+
let(:missing_key_name) { "#{original_scoping}missing_key" }
|
147
|
+
|
148
|
+
it_behaves_like 'construction contexts'
|
149
|
+
end
|
150
|
+
|
151
|
+
context 'one-level in' do
|
152
|
+
let(:config) { original_config.three }
|
153
|
+
let(:missing_key_name) { "#{original_scoping}three.missing_key" }
|
154
|
+
|
155
|
+
it_behaves_like 'construction contexts'
|
80
156
|
end
|
81
157
|
end
|
82
158
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: basic_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|