app_konfig 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: acc70f7bfa0cce596647ce885058b92dd7f213b9
4
- data.tar.gz: c9dd25965b13dc90aacc2e9f1af645b6a8373e40
3
+ metadata.gz: 508ea468ebcc191b9230fccb19e2f78efafbbd66
4
+ data.tar.gz: 435157082976316f89fe77524c018e995289bdba
5
5
  SHA512:
6
- metadata.gz: e095dd35cde544a0a8dbb1546b7ff02ed548940519c9c30e2ab466af512b2a4deccde90f84234e0293646e64a62295c174f18148ef074e5e787c2e6f6d9d0757
7
- data.tar.gz: c8cce7af00dd510bcd93d33d042e862b5ab62c82032966ecf18c0f2964074f2ac2098fcc330e69cedf78f15e2f40fff58453b8b826036fcb6200025bb79b1cc2
6
+ metadata.gz: 8da018cdeba86d2686aa45094deeeb5c2308a4aa88f40a6bf35032d74ec87294fb88285653634d038d016799875e784a6223537677bef22ec84abe6c6e3db46d
7
+ data.tar.gz: 8f9901d4d54a94a53df8fd61392db82e53017972c5b4ed5718879d124d0c6cd878e22dad7b2a705b81f9ab018279401b3bca23fa0ccc454ead7dc1a220b35596
data/CHANGELOG.MD CHANGED
@@ -1,3 +1,9 @@
1
+ 1.0.0
2
+
3
+ - Add a getter method to solve some conflicts with configuration keys (@mtczerwinski)
4
+ - Add test (@mtczerwinski)
5
+ - Add information into readme (@mtczerwinski)
6
+
1
7
  0.1.0
2
8
 
3
9
  - Lessen rails dependency - minimal version is now 3.0 (@nekath)
data/README.md CHANGED
@@ -52,6 +52,12 @@ AppConfig.value
52
52
  AppConfig.proxy.ip
53
53
  AppConfig.secret_key
54
54
  ```
55
+ or
56
+ ```ruby
57
+ AppConfig.get('value')
58
+ AppConfig.get('proxy.ip')
59
+ AppConfig.get('secret_key')
60
+ ```
55
61
 
56
62
  ## Contributing
57
63
 
data/config/secrets.yml CHANGED
@@ -1,6 +1,15 @@
1
1
  production:
2
+ key: "key"
3
+ keys: 123456789
2
4
  hostname: 'production.example.com'
3
5
  array_key:
4
6
  - first_new
5
7
  - second_new
6
8
  - third_new
9
+ other:
10
+ values:
11
+ - first_old
12
+ - second_old
13
+ - third_old
14
+ values: 'James Bond'
15
+ value: ''
@@ -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)
@@ -1,3 +1,3 @@
1
1
  module AppKonfig
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  end
@@ -2,35 +2,83 @@ require 'spec_helper'
2
2
  require 'app_konfig/config'
3
3
 
4
4
  RSpec.describe AppKonfig::Config do
5
- it 'is a Hash' do
6
- expect(subject).to be_a(Hash)
7
- end
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
- it 'loads config based on RAILS_ENV' do
10
- allow(ENV).to receive(:fetch).and_return('test')
11
- expect(subject.hostname).to eq('test.example.com')
12
- end
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
- it 'uses development config by default' do
15
- expect(described_class::DEFAULT_ENV).to eq('development')
16
- expect(subject.hostname).to eq('example.com')
17
- end
16
+ it 'handles nested keys' do
17
+ expect(subject.get('proxy.port')).to eq(3000)
18
+ end
18
19
 
19
- it 'handles nested keys' do
20
- expect(subject.proxy.port).to eq(3000)
21
- end
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
- it 'merges config from secrets file' do
24
- allow(ENV).to receive(:fetch).and_return('production')
25
- expect(subject.hostname).to eq('production.example.com')
26
- expect(subject.array_key).to eq(['first_new', 'second_new', 'third_new'])
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
- it 'allows to embed ruby inside a config file' do
30
- allow(ENV).to receive(:[]).with('SECRET_KEY').and_return('value_from_env')
31
- expect(subject.secret_key).to eq('value_from_env')
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: 0.1.0
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: 2015-09-05 00:00:00.000000000 Z
12
+ date: 2016-06-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport