static_config 0.0.1 → 0.0.2
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.
@@ -16,13 +16,37 @@ module StaticConfig
|
|
16
16
|
def update(data, env_name, env_value)
|
17
17
|
if env_name =~ /^#{@env_prefix}_(.*)/
|
18
18
|
parts = $1.downcase.split(/_/)
|
19
|
-
|
20
|
-
|
19
|
+
value = convert(env_value)
|
20
|
+
build(data, parts, value)
|
21
21
|
end
|
22
22
|
data
|
23
23
|
rescue => e
|
24
24
|
raise "Unable to fit #{env_name} into #{data.inspect}"
|
25
25
|
end
|
26
|
+
|
27
|
+
def build(data, keys, value)
|
28
|
+
first, *rest = keys
|
29
|
+
while !rest.empty?
|
30
|
+
build(data[first] ||= {}, rest, value)
|
31
|
+
first += '_' + rest.shift
|
32
|
+
end
|
33
|
+
data[first] = value
|
34
|
+
end
|
35
|
+
|
36
|
+
def convert env_value
|
37
|
+
case env_value
|
38
|
+
when /^\d+$/
|
39
|
+
env_value.to_i
|
40
|
+
when /^(\d*\.\d+)|(\d+\.\d*)$/
|
41
|
+
env_value.to_f
|
42
|
+
when 'true'
|
43
|
+
true
|
44
|
+
when 'false'
|
45
|
+
false
|
46
|
+
else
|
47
|
+
env_value
|
48
|
+
end
|
49
|
+
end
|
26
50
|
end
|
27
51
|
end
|
28
52
|
end
|
@@ -2,15 +2,49 @@ require 'spec_helper'
|
|
2
2
|
require 'static_config/reader/environment'
|
3
3
|
|
4
4
|
describe StaticConfig::Reader::Environment do
|
5
|
-
|
6
|
-
|
5
|
+
def self.has_env(e)
|
6
|
+
before do
|
7
|
+
@original_env ||= {}
|
8
|
+
e.each do |key, value|
|
9
|
+
@original_env[key], ENV[key] = ENV[key], value
|
10
|
+
end
|
11
|
+
end
|
12
|
+
after do
|
13
|
+
e.each do |key, _|
|
14
|
+
ENV[key] = @original_env[key]
|
15
|
+
end
|
16
|
+
end
|
7
17
|
end
|
8
|
-
after do
|
9
|
-
ENV['MY_CONFIG_DEV_KEY'] = nil
|
10
|
-
end
|
11
|
-
let(:subject) { described_class.read(opts) }
|
12
18
|
|
19
|
+
let(:subject) { described_class.read(opts) }
|
13
20
|
def opts ; { :env_prefix => 'MY_CONFIG' } ; end
|
14
|
-
|
21
|
+
|
22
|
+
context do
|
23
|
+
has_env 'MY_CONFIG_TEST' => 'value'
|
24
|
+
it('reads environment') { should == { 'test' => 'value' } }
|
25
|
+
end
|
26
|
+
|
27
|
+
context do
|
28
|
+
has_env 'MY_CONFIG_A_B_C' => 'D'
|
29
|
+
it { subject['a_b_c'].should == 'D' }
|
30
|
+
it { subject['a']['b_c'].should == 'D' }
|
31
|
+
it { subject['a_b']['c'].should == 'D' }
|
32
|
+
it { subject['a']['b']['c'].should == 'D' }
|
33
|
+
end
|
34
|
+
|
35
|
+
context do
|
36
|
+
has_env({
|
37
|
+
'MY_CONFIG_INT' => '123',
|
38
|
+
'MY_CONFIG_TRUE' => 'true',
|
39
|
+
'MY_CONFIG_FALSE' => 'false',
|
40
|
+
'MY_CONFIG_FLOAT' => '123.45',
|
41
|
+
'MY_CONFIG_STRING' => 'string',
|
42
|
+
})
|
43
|
+
it { subject['int'].should == 123 }
|
44
|
+
it { subject['true'].should == true }
|
45
|
+
it { subject['false'].should == false }
|
46
|
+
it { subject['float'].should == 123.45 }
|
47
|
+
it { subject['string'].should == 'string' }
|
48
|
+
end
|
15
49
|
end
|
16
50
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: static_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matt Burke
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-09-12 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: Load configuration from yaml and/or environment variables
|