enver 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -1
- data/lib/enver/extension.rb +1 -1
- data/lib/enver/loader.rb +18 -4
- data/lib/enver/version.rb +1 -1
- data/spec/enver/loader_spec.rb +42 -20
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a3f8bffa9a3c689bfc89db10641b37addb7df20
|
4
|
+
data.tar.gz: a0517f566731d2fa42fa6a1e088d2928b9d14e5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba2784cd3efc0072717a1ddca43d8a00bc21c3668f3d6b1a3f87fe5207c1254dbb6ef84d39c4987934dc5d67aa23b965c9fc65b39c4bce76aae8387f0df7b35c
|
7
|
+
data.tar.gz: 8ee7c7dd6fde84d0e9b163c0d55d6e9e83418122a9646ccc0dd219e67d9dd4ef72b1512da3f92c79f0fa485c0a9f46519450e48cc92ac4bb022fe0e787b0579d
|
data/README.md
CHANGED
@@ -24,6 +24,8 @@ Or install it yourself as:
|
|
24
24
|
export CLIENT_KEY=xxx
|
25
25
|
export CLIENT_SECRET=yyy
|
26
26
|
export SERVERS=4
|
27
|
+
export NEW_RELIC_LICENSE_KEY=lisencekey
|
28
|
+
export NEW_RELIC_APP_NAME=appname
|
27
29
|
```
|
28
30
|
|
29
31
|
```ruby
|
@@ -32,17 +34,25 @@ env = Enver.load do
|
|
32
34
|
string :client_secret, 'CLIENT_SECRET'
|
33
35
|
integer :servers, 'SERVERS'
|
34
36
|
array :path, 'PATH', pattern: ':'
|
37
|
+
|
38
|
+
partial :new_relic, 'NEW_RELIC_' do
|
39
|
+
string :license_key, 'LICENSE_KEY'
|
40
|
+
string :app_name, 'APP_NAME'
|
41
|
+
end
|
35
42
|
end
|
36
43
|
|
37
44
|
env.client_key # => 'xxx'
|
38
45
|
env.client_secret # => 'yyy'
|
39
46
|
env.servers # => 4
|
40
47
|
env.path # => array of your paths
|
48
|
+
|
49
|
+
env.new_relic.license_key # => 'licensekey'
|
50
|
+
env.new_relic.app_name # => 'appname'
|
41
51
|
```
|
42
52
|
|
43
53
|
## Contributing
|
44
54
|
|
45
|
-
1. Fork it ( https://github.com/
|
55
|
+
1. Fork it ( https://github.com/mashiro/enver/fork )
|
46
56
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
57
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
58
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/enver/extension.rb
CHANGED
data/lib/enver/loader.rb
CHANGED
@@ -2,17 +2,18 @@ require 'ostruct'
|
|
2
2
|
|
3
3
|
module Enver
|
4
4
|
class Loader
|
5
|
-
attr_reader :
|
5
|
+
attr_reader :attributes
|
6
6
|
|
7
|
-
def initialize(env, &block)
|
7
|
+
def initialize(env, prefix = '', &block)
|
8
8
|
@env = env
|
9
|
-
@
|
9
|
+
@prefix = prefix
|
10
|
+
@attributes = OpenStruct.new
|
10
11
|
instance_eval(&block) if block
|
11
12
|
end
|
12
13
|
|
13
14
|
def value(name, env_name, options = {})
|
14
15
|
value = fetch env_name, options
|
15
|
-
|
16
|
+
store(name, value.is_a?(String) ? yield(value) : value)
|
16
17
|
end
|
17
18
|
|
18
19
|
def string(name, env_name, options = {})
|
@@ -48,9 +49,22 @@ module Enver
|
|
48
49
|
end
|
49
50
|
end
|
50
51
|
|
52
|
+
def partial(name, env_prefix, options = {}, &block)
|
53
|
+
store name, Loader.new(@env, with_prefix(env_prefix), &block).attributes
|
54
|
+
end
|
55
|
+
|
51
56
|
private
|
52
57
|
|
58
|
+
def with_prefix(env_name)
|
59
|
+
"#{@prefix}#{env_name}"
|
60
|
+
end
|
61
|
+
|
62
|
+
def store(name, value)
|
63
|
+
@attributes.send("#{name}=", value)
|
64
|
+
end
|
65
|
+
|
53
66
|
def fetch(env_name, options = {})
|
67
|
+
env_name = with_prefix(env_name)
|
54
68
|
if options.has_key? :default
|
55
69
|
@env.fetch(env_name, options[:default])
|
56
70
|
else
|
data/lib/enver/version.rb
CHANGED
data/spec/enver/loader_spec.rb
CHANGED
@@ -22,6 +22,10 @@ RSpec.describe Enver::Loader do
|
|
22
22
|
'BOOLEAN10' => 'はい',
|
23
23
|
'ARRAY1' => 'foo,bar,buzz',
|
24
24
|
'ARRAY2' => 'foo:bar:buzz',
|
25
|
+
'MY_VALUE1' => 'myvalue1',
|
26
|
+
'MY_VALUE2' => 'myvalue2',
|
27
|
+
'MY_SUPER_VALUE1' => 'mysupervalue1',
|
28
|
+
'MY_SUPER_VALUE2' => 'mysupervalue2',
|
25
29
|
}
|
26
30
|
end
|
27
31
|
let(:loader) { Enver::Loader.new(env) }
|
@@ -32,34 +36,52 @@ RSpec.describe Enver::Loader do
|
|
32
36
|
end
|
33
37
|
|
34
38
|
describe '#string' do
|
35
|
-
it { expect(loader.string(
|
36
|
-
it { expect(loader.string(
|
39
|
+
it { expect(loader.string(:v, 'STRING1')).to eq('') }
|
40
|
+
it { expect(loader.string(:v, 'STRING2')).to eq('test') }
|
37
41
|
end
|
38
42
|
|
39
43
|
describe '#integer' do
|
40
|
-
it { expect{loader.integer(
|
41
|
-
it { expect(loader.integer(
|
42
|
-
it { expect{loader.integer(
|
43
|
-
it { expect{loader.integer(
|
44
|
+
it { expect{loader.integer(:v, 'INTEGER1')}.to raise_error }
|
45
|
+
it { expect(loader.integer(:v, 'INTEGER2')).to eq(1) }
|
46
|
+
it { expect{loader.integer(:v, 'INTEGER3')}.to raise_error }
|
47
|
+
it { expect{loader.integer(:v, 'INTEGER4')}.to raise_error }
|
44
48
|
end
|
45
49
|
|
46
50
|
describe '#boolean' do
|
47
|
-
it { expect(loader.boolean(
|
48
|
-
it { expect(loader.boolean(
|
49
|
-
it { expect(loader.boolean(
|
50
|
-
it { expect(loader.boolean(
|
51
|
-
it { expect(loader.boolean(
|
52
|
-
it { expect(loader.boolean(
|
53
|
-
it { expect(loader.boolean(
|
54
|
-
it { expect(loader.boolean(
|
55
|
-
it { expect(loader.boolean(
|
56
|
-
it { expect(loader.boolean(
|
57
|
-
it { expect(loader.boolean(
|
51
|
+
it { expect(loader.boolean(:v, 'BOOLEAN1')).to eq(false) }
|
52
|
+
it { expect(loader.boolean(:v, 'BOOLEAN2')).to eq(true) }
|
53
|
+
it { expect(loader.boolean(:v, 'BOOLEAN3')).to eq(false) }
|
54
|
+
it { expect(loader.boolean(:v, 'BOOLEAN4')).to eq(true) }
|
55
|
+
it { expect(loader.boolean(:v, 'BOOLEAN5')).to eq(false) }
|
56
|
+
it { expect(loader.boolean(:v, 'BOOLEAN6')).to eq(true) }
|
57
|
+
it { expect(loader.boolean(:v, 'BOOLEAN7')).to eq(false) }
|
58
|
+
it { expect(loader.boolean(:v, 'BOOLEAN8')).to eq(true) }
|
59
|
+
it { expect(loader.boolean(:v, 'BOOLEAN9')).to eq(false) }
|
60
|
+
it { expect(loader.boolean(:v, 'BOOLEAN10')).to eq(false) }
|
61
|
+
it { expect(loader.boolean(:v, 'BOOLEAN10', true_values: %w(はい))).to eq(true) }
|
58
62
|
end
|
59
63
|
|
60
64
|
describe '#array' do
|
61
|
-
it { expect(loader.array(
|
62
|
-
it { expect(loader.array(
|
63
|
-
it { expect(loader.array(
|
65
|
+
it { expect(loader.array(:v, 'ARRAY1')).to eq(['foo', 'bar', 'buzz']) }
|
66
|
+
it { expect(loader.array(:v, 'ARRAY2', pattern: ':')).to eq(['foo', 'bar', 'buzz']) }
|
67
|
+
it { expect(loader.array(:v, 'ARRAY1', limit: 2)).to eq(['foo', 'bar,buzz']) }
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '#partial' do
|
71
|
+
it do
|
72
|
+
loader.partial :my, 'MY_' do
|
73
|
+
string :value1, 'VALUE1'
|
74
|
+
string :value2, 'VALUE2'
|
75
|
+
partial :super, 'SUPER_' do
|
76
|
+
string :value1, 'VALUE1'
|
77
|
+
string :value2, 'VALUE2'
|
78
|
+
end
|
79
|
+
end
|
80
|
+
env = loader.attributes
|
81
|
+
expect(env.my.value1).to eq('myvalue1')
|
82
|
+
expect(env.my.value2).to eq('myvalue2')
|
83
|
+
expect(env.my.super.value1).to eq('mysupervalue1')
|
84
|
+
expect(env.my.super.value2).to eq('mysupervalue2')
|
85
|
+
end
|
64
86
|
end
|
65
87
|
end
|