configerator 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/configerator/configerator.rb +28 -14
- data/lib/generators/configerator/config_generator.rb +11 -0
- metadata +4 -6
- data/lib/generators/config_generator.rb +0 -9
- data/test/configerator_test.rb +0 -172
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a77e8c3b7be476c6b9a4607d75c9e97e95343a98
|
4
|
+
data.tar.gz: d840fd2c528b01c7a786287b94ae95360b923c28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6708a7555553df1b71255d6bc9b76559d53532e28da786ae7e1646f33416b552c304f2bb537fe3abf1844ed90f32b64843749219099e3f34804e0730a923957d
|
7
|
+
data.tar.gz: ef78a142714098468d93acab0e177f581d2ea202ca02f05f2f5c38e11599f85b4b211c43c7703cf421b270842fdb6bde6946af1bed74ecfc7621c67b0ceb4de9
|
@@ -1,36 +1,36 @@
|
|
1
1
|
require 'uri'
|
2
2
|
|
3
3
|
module Configerator
|
4
|
-
|
5
|
-
|
4
|
+
# Initializers (DSL)
|
6
5
|
def required(name, method=nil, error_on_load: true)
|
7
|
-
|
8
|
-
value = error_on_load ? ENV.fetch(name.to_s.upcase) : ENV[name.to_s.upcase]
|
9
|
-
value = cast(value, method)
|
6
|
+
value = cast(fetch_env(name, error_on_load: error_on_load), method)
|
10
7
|
|
11
8
|
create(name, value, error_on_load)
|
12
9
|
end
|
13
10
|
|
14
11
|
def optional(name, method=nil)
|
15
|
-
value = cast(
|
12
|
+
value = cast(fetch_env(name), method)
|
13
|
+
create(name, value)
|
14
|
+
end
|
15
|
+
|
16
|
+
def override(name, default, method=nil)
|
17
|
+
value = cast(fetch_env(name) || default, method)
|
16
18
|
create(name, value)
|
17
19
|
end
|
18
20
|
|
19
21
|
def namespace namespace, prefix: true, &block
|
20
22
|
@processed = []
|
21
23
|
@prefix = "#{namespace}_" if prefix
|
24
|
+
|
22
25
|
yield
|
23
|
-
|
26
|
+
|
27
|
+
instance_eval "def #{namespace}?; !!(#{@processed.join(' && ')}) end", __FILE__, __LINE__
|
24
28
|
ensure
|
25
29
|
@prefix = nil
|
26
30
|
@processed = []
|
27
31
|
end
|
28
32
|
|
29
|
-
|
30
|
-
value = cast(ENV.fetch(name.to_s.upcase, default), method)
|
31
|
-
create(name, value)
|
32
|
-
end
|
33
|
-
|
33
|
+
# Scope methods
|
34
34
|
def int
|
35
35
|
->(v) { v.to_i }
|
36
36
|
end
|
@@ -69,6 +69,7 @@ module Configerator
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
+
# Helpers
|
72
73
|
private
|
73
74
|
|
74
75
|
def cast(value, method)
|
@@ -76,13 +77,26 @@ module Configerator
|
|
76
77
|
end
|
77
78
|
|
78
79
|
def create(name, value, error_on_load=true)
|
79
|
-
name =
|
80
|
+
name = build_key(name).downcase
|
80
81
|
|
81
82
|
instance_variable_set(:"@#{name}", value)
|
82
|
-
|
83
|
+
|
84
|
+
instance_eval "def #{name}; @#{name} || (raise 'key not set: \"#{name.upcase}\"' unless #{error_on_load}) end", __FILE__, __LINE__
|
83
85
|
instance_eval "def #{name}?; !!#{name} end", __FILE__, __LINE__
|
84
86
|
|
85
87
|
@processed ||= []
|
86
88
|
@processed << name
|
87
89
|
end
|
90
|
+
|
91
|
+
def build_key(key)
|
92
|
+
key = "#{@prefix}#{key}" if @prefix
|
93
|
+
|
94
|
+
key.to_s.upcase
|
95
|
+
end
|
96
|
+
|
97
|
+
def fetch_env(key, error_on_load: false)
|
98
|
+
key = build_key(key)
|
99
|
+
|
100
|
+
error_on_load ? ENV.fetch(key) : ENV[key]
|
101
|
+
end
|
88
102
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Configerator
|
2
|
+
class ConfigGenerator < Rails::Generators::Base
|
3
|
+
desc "Creates a Configerator-based file at config/config.rb"
|
4
|
+
|
5
|
+
source_root File.expand_path("../../templates", __FILE__)
|
6
|
+
|
7
|
+
def create_config_file
|
8
|
+
copy_file "config.rb", "config/config.rb"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configerator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Mervine
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2017-02-18 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Simple module for implementing environment based configuration adapted
|
15
15
|
from Pliny and following the 12factor pattern.
|
@@ -22,9 +22,8 @@ extra_rdoc_files: []
|
|
22
22
|
files:
|
23
23
|
- lib/configerator.rb
|
24
24
|
- lib/configerator/configerator.rb
|
25
|
-
- lib/generators/config_generator.rb
|
25
|
+
- lib/generators/configerator/config_generator.rb
|
26
26
|
- lib/generators/templates/config.rb
|
27
|
-
- test/configerator_test.rb
|
28
27
|
homepage: https://github.com/heroku/configerator
|
29
28
|
licenses:
|
30
29
|
- MIT
|
@@ -49,5 +48,4 @@ rubygems_version: 2.5.1
|
|
49
48
|
signing_key:
|
50
49
|
specification_version: 4
|
51
50
|
summary: 'Configerator: A Config Helper'
|
52
|
-
test_files:
|
53
|
-
- test/configerator_test.rb
|
51
|
+
test_files: []
|
data/test/configerator_test.rb
DELETED
@@ -1,172 +0,0 @@
|
|
1
|
-
require 'uri'
|
2
|
-
require 'minitest/autorun'
|
3
|
-
require './lib/configerator'
|
4
|
-
|
5
|
-
module Config
|
6
|
-
extend Configerator
|
7
|
-
end
|
8
|
-
|
9
|
-
class TestConfigerator < Minitest::Test
|
10
|
-
FIXTURES = {
|
11
|
-
test_required: 'required',
|
12
|
-
test_optional: 'optional',
|
13
|
-
test_override: 'override',
|
14
|
-
|
15
|
-
test_int: 99,
|
16
|
-
test_float: 99.9,
|
17
|
-
test_string: 'ninty nine',
|
18
|
-
test_symbol: :ninty_nine,
|
19
|
-
test_url: 'https://99.com',
|
20
|
-
test_array_int: [ 9, 9 ],
|
21
|
-
test_array: [ 'nine', 'nine' ],
|
22
|
-
|
23
|
-
test_ns1: 'ns1',
|
24
|
-
test_ns2: 'ns2',
|
25
|
-
|
26
|
-
ns1: 'ns1',
|
27
|
-
ns2: 'ns2'
|
28
|
-
}.freeze
|
29
|
-
|
30
|
-
def setup
|
31
|
-
FIXTURES.each { |k, v|
|
32
|
-
ENV[k.to_s.upcase] = (v.is_a?(Array) ? v.join(',') : v.to_s)
|
33
|
-
}
|
34
|
-
end
|
35
|
-
|
36
|
-
def with_method
|
37
|
-
-> (v) { "method:#{v}" }
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_required
|
41
|
-
Config.required :test_required
|
42
|
-
|
43
|
-
assert_equal Config.test_required, 'required'
|
44
|
-
assert Config.test_required?
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_required_on_load_false
|
48
|
-
Config.required :test_required2, error_on_load: false
|
49
|
-
|
50
|
-
assert_raises RuntimeError do
|
51
|
-
Config.test_required2?
|
52
|
-
end
|
53
|
-
|
54
|
-
assert_raises RuntimeError do
|
55
|
-
Config.test_required2
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_required_with_method
|
60
|
-
Config.required :test_required, with_method
|
61
|
-
|
62
|
-
assert_equal Config.test_required, 'method:required'
|
63
|
-
assert Config.test_required?
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_required_missing
|
67
|
-
assert_raises KeyError do
|
68
|
-
Config.required :test_missing
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_optional
|
73
|
-
Config.optional :test_optional
|
74
|
-
|
75
|
-
assert_equal Config.test_optional, 'optional'
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_optional_with_method
|
79
|
-
Config.optional :test_optional, with_method
|
80
|
-
|
81
|
-
assert_equal Config.test_optional, 'method:optional'
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_optional_missing
|
85
|
-
Config.optional :test_missing_optional
|
86
|
-
|
87
|
-
assert_nil Config.test_missing_optional
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_namespace
|
91
|
-
Config.namespace :test do
|
92
|
-
Config.required :ns1
|
93
|
-
Config.optional :ns2
|
94
|
-
Config.override :ns3, "three"
|
95
|
-
end
|
96
|
-
|
97
|
-
assert Config.test_ns1
|
98
|
-
assert Config.test_ns2
|
99
|
-
assert Config.test_ns3
|
100
|
-
assert Config.test?
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_namepsace_missing
|
104
|
-
Config.namespace :test do
|
105
|
-
Config.required :ns1
|
106
|
-
Config.optional :ns2
|
107
|
-
Config.override :ns3, "three"
|
108
|
-
Config.optional :ns4
|
109
|
-
end
|
110
|
-
|
111
|
-
assert Config.test_ns1
|
112
|
-
assert Config.test_ns2
|
113
|
-
assert Config.test_ns3
|
114
|
-
refute Config.test_ns4
|
115
|
-
refute Config.test?
|
116
|
-
end
|
117
|
-
|
118
|
-
def test_namespace_without_prefix
|
119
|
-
Config.namespace :test, prefix: false do
|
120
|
-
Config.optional :ns1
|
121
|
-
Config.optional :ns2
|
122
|
-
end
|
123
|
-
|
124
|
-
assert Config.ns1
|
125
|
-
assert Config.ns2
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_override
|
129
|
-
Config.override :test_override, 'override_default'
|
130
|
-
|
131
|
-
assert_equal Config.test_override, 'override'
|
132
|
-
end
|
133
|
-
|
134
|
-
def test_override_with_method
|
135
|
-
Config.override :test_override, 'override_default', with_method
|
136
|
-
|
137
|
-
assert_equal Config.test_override, 'method:override'
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_override_missing
|
141
|
-
Config.override :test_override_missing, 'override_default'
|
142
|
-
|
143
|
-
assert_equal Config.test_override_missing, 'override_default'
|
144
|
-
end
|
145
|
-
|
146
|
-
# build basic casting tests
|
147
|
-
FIXTURES.each do |meth, val|
|
148
|
-
caster = meth.to_s.gsub(/^test_/, '')
|
149
|
-
|
150
|
-
unless %w[ required optional override url ns1 ns2 ].include? caster
|
151
|
-
method = \
|
152
|
-
if caster =~ /_/
|
153
|
-
parts = caster.split('_')
|
154
|
-
Config.send(parts.first.to_sym, Config.send(parts.last.to_sym))
|
155
|
-
else
|
156
|
-
Config.send(caster.to_sym)
|
157
|
-
end
|
158
|
-
|
159
|
-
define_method(meth) do
|
160
|
-
Config.required meth, method
|
161
|
-
|
162
|
-
assert_equal Config.send(meth), val
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
def test_url
|
168
|
-
Config.required :test_url, Config.url
|
169
|
-
|
170
|
-
assert_equal Config.test_url, URI.parse('https://99.com')
|
171
|
-
end
|
172
|
-
end
|