app_konfig 0.1.0 → 1.0.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.
- checksums.yaml +4 -4
- data/CHANGELOG.MD +6 -0
- data/README.md +6 -0
- data/config/secrets.yml +9 -0
- data/lib/app_konfig/config.rb +8 -0
- data/lib/app_konfig/version.rb +1 -1
- data/spec/app_konfig_spec.rb +69 -21
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 508ea468ebcc191b9230fccb19e2f78efafbbd66
|
4
|
+
data.tar.gz: 435157082976316f89fe77524c018e995289bdba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8da018cdeba86d2686aa45094deeeb5c2308a4aa88f40a6bf35032d74ec87294fb88285653634d038d016799875e784a6223537677bef22ec84abe6c6e3db46d
|
7
|
+
data.tar.gz: 8f9901d4d54a94a53df8fd61392db82e53017972c5b4ed5718879d124d0c6cd878e22dad7b2a705b81f9ab018279401b3bca23fa0ccc454ead7dc1a220b35596
|
data/CHANGELOG.MD
CHANGED
data/README.md
CHANGED
data/config/secrets.yml
CHANGED
data/lib/app_konfig/config.rb
CHANGED
@@ -3,6 +3,8 @@ require 'yaml'
|
|
3
3
|
require 'erb'
|
4
4
|
|
5
5
|
module AppKonfig
|
6
|
+
class ConfigurationKeyNotFound < Exception ; end
|
7
|
+
|
6
8
|
class Config < Hash
|
7
9
|
DEFAULT_ENV = 'development'
|
8
10
|
CONFIG_PATH = {
|
@@ -15,6 +17,12 @@ module AppKonfig
|
|
15
17
|
deep_merge!(pub_config).deep_merge!(sec_config)
|
16
18
|
end
|
17
19
|
|
20
|
+
def get(args)
|
21
|
+
args.split('.').inject(self) { |hash, arg| hash.fetch(arg) }
|
22
|
+
rescue KeyError
|
23
|
+
raise AppKonfig::ConfigurationKeyNotFound.new("configuration key not found")
|
24
|
+
end
|
25
|
+
|
18
26
|
def method_missing(key)
|
19
27
|
key = key.to_s
|
20
28
|
return self[key] unless self[key].is_a?(Hash)
|
data/lib/app_konfig/version.rb
CHANGED
data/spec/app_konfig_spec.rb
CHANGED
@@ -2,35 +2,83 @@ require 'spec_helper'
|
|
2
2
|
require 'app_konfig/config'
|
3
3
|
|
4
4
|
RSpec.describe AppKonfig::Config do
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
context '#get' do
|
6
|
+
it 'loads config based on RAILS_ENV' do
|
7
|
+
allow(ENV).to receive(:fetch).and_return('test')
|
8
|
+
expect(subject.get('hostname')).to eq('test.example.com')
|
9
|
+
end
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
it 'uses development config by default' do
|
12
|
+
expect(described_class::DEFAULT_ENV).to eq('development')
|
13
|
+
expect(subject.get('hostname')).to eq('example.com')
|
14
|
+
end
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
16
|
+
it 'handles nested keys' do
|
17
|
+
expect(subject.get('proxy.port')).to eq(3000)
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
it 'merges config from secrets file' do
|
21
|
+
allow(ENV).to receive(:fetch).and_return('production')
|
22
|
+
expect(subject.get('hostname')).to eq('production.example.com')
|
23
|
+
expect(subject.get('array_key')).to eq(['first_new', 'second_new', 'third_new'])
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'allows to use a configuration names the same as methods from the Hash class' do
|
27
|
+
allow(ENV).to receive(:fetch).and_return('production')
|
28
|
+
expect(subject.get('keys')).to eq(123456789)
|
29
|
+
expect(subject.get('key')).to eq("key")
|
30
|
+
expect(subject.get('other.values')).to eq(["first_old", "second_old", "third_old"])
|
31
|
+
expect(subject.get('values')).to eq("James Bond")
|
32
|
+
expect(subject.get('value')).to eq("")
|
33
|
+
end
|
22
34
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
35
|
+
it 'raises exception when a configuration key was not found' do
|
36
|
+
allow(ENV).to receive(:fetch).and_return('production')
|
37
|
+
expect { subject.get('test.test3.test2.test1') }.to raise_error(AppKonfig::ConfigurationKeyNotFound)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'allows to embed ruby inside a config file' do
|
41
|
+
allow(ENV).to receive(:[]).with('SECRET_KEY').and_return('value_from_env')
|
42
|
+
expect(subject.get('secret_key')).to eq('value_from_env')
|
43
|
+
end
|
27
44
|
end
|
28
45
|
|
29
|
-
|
30
|
-
|
31
|
-
|
46
|
+
context 'a chain method invoking' do
|
47
|
+
it 'loads config based on RAILS_ENV' do
|
48
|
+
allow(ENV).to receive(:fetch).and_return('test')
|
49
|
+
expect(subject.hostname).to eq('test.example.com')
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'uses development config by default' do
|
53
|
+
expect(described_class::DEFAULT_ENV).to eq('development')
|
54
|
+
expect(subject.hostname).to eq('example.com')
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'handles nested keys' do
|
58
|
+
expect(subject.proxy.port).to eq(3000)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'merges config from secrets file' do
|
62
|
+
allow(ENV).to receive(:fetch).and_return('production')
|
63
|
+
expect(subject.hostname).to eq('production.example.com')
|
64
|
+
expect(subject.array_key).to eq(['first_new', 'second_new', 'third_new'])
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'allows to use a configuration names the same as methods from the Hash class' do
|
68
|
+
allow(ENV).to receive(:fetch).and_return('production')
|
69
|
+
expect(subject.keys).to eq ["hostname", "key", "keys", "array_key", "other", "values", "value"]
|
70
|
+
expect(subject.other.values).to eq([["first_old", "second_old", "third_old"]])
|
71
|
+
expect(subject.values).to eq ["production.example.com", "key", 123456789, ["first_new", "second_new", "third_new"], {"values"=>["first_old", "second_old", "third_old"]}, "James Bond", ""]
|
72
|
+
expect(subject.value).to eq("")
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'allows to embed ruby inside a config file' do
|
76
|
+
allow(ENV).to receive(:[]).with('SECRET_KEY').and_return('value_from_env')
|
77
|
+
expect(subject.secret_key).to eq('value_from_env')
|
78
|
+
end
|
32
79
|
end
|
33
80
|
|
81
|
+
|
34
82
|
context 'when secrets file is not found' do
|
35
83
|
it 'fails silently' do
|
36
84
|
stub_const('AppKonfig::Config::CONFIG_PATH', {
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: app_konfig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bartosz Kopiński
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2016-06-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|