settings-env-loader 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/settings-env-loader.rb +57 -61
- data/settings-env-loader.gemspec +1 -1
- data/spec/lib/settings-env-loader_spec.rb +2 -2
- metadata +3 -3
data/lib/settings-env-loader.rb
CHANGED
@@ -1,77 +1,73 @@
|
|
1
1
|
require "enumerator"
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
module Loader
|
6
|
-
VERSION = "0.0.1"
|
3
|
+
module SettingsEnvLoader
|
4
|
+
VERSION = "0.1.0"
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
# Merges ENV and modify the hash directly
|
7
|
+
def merge_env(prefix = nil, separator = '_')
|
8
|
+
Merger.new(prefix, separator, self).merge
|
9
|
+
self
|
10
|
+
end
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
# Iterates the Hash as ENV key value pairs
|
13
|
+
def each_env(prefix = nil, separator = '_')
|
14
|
+
Enumerator.new do |y|
|
15
|
+
Exporter.new(prefix, separator, self).each(y)
|
16
|
+
end
|
17
|
+
end
|
20
18
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
19
|
+
class Merger
|
20
|
+
def initialize(prefix, separator, hash)
|
21
|
+
@prefix = prefix
|
22
|
+
@separator = separator
|
23
|
+
@hash = hash
|
24
|
+
end
|
27
25
|
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
def merge
|
27
|
+
@hash.each do |key, value|
|
28
|
+
env_name = [@prefix, key].compact.join(@separator).upcase
|
31
29
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|
30
|
+
if value.is_a?(Hash)
|
31
|
+
Merger.new(env_name, @separator, value).merge
|
32
|
+
else
|
33
|
+
env_value = ENV[env_name]
|
34
|
+
if env_value
|
35
|
+
@hash[key] = convert(env_value, value)
|
40
36
|
end
|
41
37
|
end
|
38
|
+
end
|
39
|
+
end
|
42
40
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
56
|
-
end
|
41
|
+
def convert(value, origin_value)
|
42
|
+
case origin_value
|
43
|
+
when Symbol
|
44
|
+
value.to_sym
|
45
|
+
when Fixnum
|
46
|
+
value.to_i
|
47
|
+
when Float
|
48
|
+
value.to_f
|
49
|
+
when Array
|
50
|
+
Array(YAML.load(value))
|
51
|
+
else
|
52
|
+
value
|
57
53
|
end
|
54
|
+
end
|
55
|
+
end
|
58
56
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
57
|
+
class Exporter
|
58
|
+
def initialize(prefix, separator, hash)
|
59
|
+
@prefix = prefix
|
60
|
+
@separator = separator
|
61
|
+
@hash = hash
|
62
|
+
end
|
65
63
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
end
|
74
|
-
end
|
64
|
+
def each(y)
|
65
|
+
@hash.each do |key, value|
|
66
|
+
env_name = [@prefix, key].compact.join(@separator).upcase
|
67
|
+
if value.is_a?(Hash)
|
68
|
+
Exporter.new(env_name, @separator, value).each(y)
|
69
|
+
else
|
70
|
+
y << [env_name, value.to_s]
|
75
71
|
end
|
76
72
|
end
|
77
73
|
end
|
data/settings-env-loader.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'settings-env-loader'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "settings-env-loader"
|
8
|
-
gem.version =
|
8
|
+
gem.version = SettingsEnvLoader::VERSION
|
9
9
|
gem.authors = ["Ian Yang"]
|
10
10
|
gem.email = ["me@iany.me"]
|
11
11
|
gem.description = %q{Scan ENV and override correspondong settings in a nested Hash}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'settings-env-loader'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe SettingsEnvLoader do
|
4
4
|
let(:hash) do
|
5
5
|
{
|
6
6
|
:float => 1.2,
|
@@ -16,7 +16,7 @@ describe Settings::Env::Loader do
|
|
16
16
|
subject { hash }
|
17
17
|
|
18
18
|
before do
|
19
|
-
hash.extend
|
19
|
+
hash.extend SettingsEnvLoader
|
20
20
|
end
|
21
21
|
|
22
22
|
before do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: settings-env-loader
|
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:
|
@@ -75,7 +75,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
75
|
version: '0'
|
76
76
|
segments:
|
77
77
|
- 0
|
78
|
-
hash: -
|
78
|
+
hash: -4257699166367919171
|
79
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
80
|
none: false
|
81
81
|
requirements:
|
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
84
|
version: '0'
|
85
85
|
segments:
|
86
86
|
- 0
|
87
|
-
hash: -
|
87
|
+
hash: -4257699166367919171
|
88
88
|
requirements: []
|
89
89
|
rubyforge_project:
|
90
90
|
rubygems_version: 1.8.23
|