confg 1.0.3 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Rakefile +11 -0
- data/confg.gemspec +20 -16
- data/lib/confg.rb +35 -19
- data/lib/confg/configuration.rb +80 -102
- data/lib/confg/erb_context.rb +4 -4
- data/lib/confg/version.rb +5 -1
- data/test/confg_test.rb +68 -0
- data/test/example.yml +10 -0
- data/test/test_helper.rb +6 -0
- metadata +58 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 50b51215735b3ac4f8caedb04e36ae28176bb1bd6e7c14c2e3cdced3e7480a1e
|
4
|
+
data.tar.gz: 95b45d10ab8cb6751afd546d28f4fa76d48e56c4e2746489c5a91cb8dcfcba07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c94266338ff570a78f1db7e736be6264f89658d3c96711c78871a34a4a01746ad4b54eb117d909c07857a52549a3d8bd8f9898648611fb809560e656dfa360b8
|
7
|
+
data.tar.gz: a2c2042c39354842a10b72d5616dc725ba538b6f7e0ccd01ebd88f98fd84f74635949158aa4e30928c2d4738bc9b02b8faf0a6976563231fce48ce409296ca9b
|
data/Rakefile
CHANGED
data/confg.gemspec
CHANGED
@@ -1,21 +1,25 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
5
|
+
require "confg/version"
|
5
6
|
|
6
|
-
Gem::Specification.new do |
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "confg"
|
9
|
+
spec.version = Confg::VERSION
|
10
|
+
spec.authors = ["Mike Nelson"]
|
11
|
+
spec.email = ["mike@mnelson.io"]
|
12
|
+
spec.description = "Config the pipes"
|
13
|
+
spec.summary = "Sets shared variables for applications"
|
14
|
+
spec.homepage = ""
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
19
20
|
|
20
|
-
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
22
|
+
spec.add_development_dependency "byebug"
|
23
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
21
25
|
end
|
data/lib/confg.rb
CHANGED
@@ -1,13 +1,23 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "confg/version"
|
4
|
+
require "confg/configuration"
|
5
|
+
require "confg/erb_context"
|
4
6
|
|
5
7
|
module Confg
|
6
8
|
|
7
9
|
class << self
|
8
10
|
|
9
11
|
def root
|
10
|
-
@root
|
12
|
+
return @root if defined?(@root)
|
13
|
+
|
14
|
+
@root = calc_root_path
|
15
|
+
end
|
16
|
+
|
17
|
+
def env
|
18
|
+
return @env if defined?(@env)
|
19
|
+
|
20
|
+
@env = calc_env_string
|
11
21
|
end
|
12
22
|
|
13
23
|
def erb_function(function_name, &block)
|
@@ -17,29 +27,21 @@ module Confg
|
|
17
27
|
self
|
18
28
|
end
|
19
29
|
|
20
|
-
def configure
|
21
|
-
@configuration ||= ::Confg::Configuration.new(
|
30
|
+
def configure
|
31
|
+
@configuration ||= ::Confg::Configuration.new(env: env, root: root)
|
22
32
|
yield @configuration if block_given?
|
23
33
|
@configuration
|
24
34
|
end
|
25
|
-
|
35
|
+
alias config configure
|
26
36
|
|
27
37
|
def method_missing(method_name, *args, &block)
|
28
38
|
config.send(method_name, *args, &block)
|
29
39
|
end
|
30
40
|
|
31
|
-
def respond_to_missing?(*
|
41
|
+
def respond_to_missing?(*_args)
|
32
42
|
true
|
33
43
|
end
|
34
44
|
|
35
|
-
def get(path)
|
36
|
-
thing = self
|
37
|
-
path.split('.').each do |piece|
|
38
|
-
thing = thing.try(piece)
|
39
|
-
end
|
40
|
-
thing
|
41
|
-
end
|
42
|
-
|
43
45
|
protected
|
44
46
|
|
45
47
|
def calc_root_string
|
@@ -47,13 +49,27 @@ module Confg
|
|
47
49
|
return RAILS_ROOT if defined?(RAILS_ROOT)
|
48
50
|
return RACK_ROOT if defined?(RACK_ROOT)
|
49
51
|
|
50
|
-
ENV[
|
52
|
+
ENV["RAILS_ROOT"] || ENV["RACK_ROOT"] || Dir.pwd
|
53
|
+
end
|
54
|
+
|
55
|
+
def calc_root_path
|
56
|
+
::Pathname.new(calc_root_string).expand_path
|
57
|
+
end
|
58
|
+
|
59
|
+
def calc_env_string
|
60
|
+
return ::Rails.env.to_s if defined?(::Rails)
|
61
|
+
return RAILS_ENV if defined?(RAILS_ENV)
|
62
|
+
return RACK_ENV if defined?(RACK_ENV)
|
63
|
+
|
64
|
+
ENV["RAILS_ENV"] || ENV["RACK_ENV"] || nil
|
51
65
|
end
|
52
66
|
|
53
67
|
def reset!
|
54
|
-
remove_instance_variable("@configuration")
|
55
|
-
remove_instance_variable("@
|
68
|
+
remove_instance_variable("@configuration") if defined?(@configuration)
|
69
|
+
remove_instance_variable("@env") if defined?(@env)
|
70
|
+
remove_instance_variable("@root") if defined?(@root)
|
56
71
|
end
|
72
|
+
|
57
73
|
end
|
58
74
|
|
59
75
|
end
|
data/lib/confg/configuration.rb
CHANGED
@@ -1,47 +1,62 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "yaml"
|
2
4
|
|
3
5
|
module Confg
|
4
|
-
class Configuration
|
6
|
+
class Configuration < ::SimpleDelegator
|
7
|
+
|
8
|
+
attr_reader :confg_env, :confg_root
|
9
|
+
|
10
|
+
def initialize(env: Confg.env, root: Confg.root)
|
11
|
+
@confg_env = env.to_s
|
12
|
+
@confg_root = Pathname.new(root)
|
5
13
|
|
6
|
-
|
14
|
+
super({})
|
15
|
+
end
|
7
16
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
17
|
+
def tmp(key, value)
|
18
|
+
initial = get(key)
|
19
|
+
set(key, value)
|
20
|
+
yield
|
21
|
+
ensure
|
22
|
+
set(key, initial)
|
12
23
|
end
|
13
24
|
|
14
|
-
def merge(
|
15
|
-
|
16
|
-
|
25
|
+
def merge(other)
|
26
|
+
other.each_pair do |k, v|
|
27
|
+
set(k, v)
|
17
28
|
end
|
18
29
|
end
|
19
|
-
|
30
|
+
alias merge! merge
|
20
31
|
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
-
out[k] = v.to_hash if v.is_a?(self.class)
|
32
|
+
def to_h
|
33
|
+
__getobj__.transform_values do |v|
|
34
|
+
v.is_a?(self.class) ? v.to_h : v
|
25
35
|
end
|
26
|
-
out
|
27
36
|
end
|
28
|
-
alias_method :to_h, :to_hash
|
29
37
|
|
30
|
-
def
|
31
|
-
|
38
|
+
def get(key)
|
39
|
+
__getobj__[key.to_s]
|
32
40
|
end
|
41
|
+
alias [] get
|
33
42
|
|
34
|
-
def
|
35
|
-
|
43
|
+
def get!(key)
|
44
|
+
__getobj__.fetch(key.to_s)
|
36
45
|
end
|
37
46
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
47
|
+
def set(key, value = nil)
|
48
|
+
__getobj__[key.to_s] = case value
|
49
|
+
when ::Hash
|
50
|
+
set_block(key) do |child|
|
51
|
+
value.each_pair do |k, v|
|
52
|
+
child.set(k, v)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
else
|
56
|
+
value
|
57
|
+
end
|
44
58
|
end
|
59
|
+
alias []= set
|
45
60
|
|
46
61
|
def load_key(key)
|
47
62
|
# loads yaml file with given key
|
@@ -49,121 +64,84 @@ module Confg
|
|
49
64
|
end
|
50
65
|
|
51
66
|
def load_yaml(path, key: nil, ignore_env: false)
|
52
|
-
|
53
|
-
raw_content = File.open(path, 'r'){|io| io.read } rescue nil
|
67
|
+
found_path = find_config_yaml(path)
|
54
68
|
|
55
|
-
|
69
|
+
raise ArgumentError, "#{path} could not be found" if found_path.nil?
|
56
70
|
|
57
71
|
ctxt = ::Confg::ErbContext.new
|
58
|
-
|
72
|
+
raw_content = ::File.read(found_path)
|
73
|
+
erb_content = ctxt.evaluate(raw_content)
|
74
|
+
yaml_content = ::YAML.send :load, erb_content
|
59
75
|
|
60
76
|
unless ignore_env
|
61
|
-
|
62
|
-
content = content[Rails.env] if env && content.is_a?(::Hash) && content.has_key?(Rails.env)
|
77
|
+
yaml_content = yaml_content[confg_env] if confg_env && yaml_content.is_a?(::Hash) && yaml_content.key?(confg_env)
|
63
78
|
end
|
64
79
|
|
65
80
|
if key
|
66
|
-
|
81
|
+
set(key, yaml_content)
|
67
82
|
else
|
68
|
-
if
|
69
|
-
raise "A key must be provided to load the file at: #{
|
83
|
+
if yaml_content.is_a?(Array)
|
84
|
+
raise "A key must be provided to load the file at: #{found_path}"
|
70
85
|
else
|
71
|
-
|
72
|
-
|
86
|
+
yaml_content.each do |k, v|
|
87
|
+
set(k, v)
|
73
88
|
end
|
74
89
|
end
|
75
90
|
end
|
76
91
|
end
|
77
|
-
|
92
|
+
alias load_yml load_yaml
|
78
93
|
|
79
94
|
def method_missing(method_name, *args, &block)
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
if block_given?
|
84
|
-
self.set_block($1, &block)
|
85
|
-
elsif @attributes.respond_to?($1)
|
86
|
-
@attributes.send($1, *args)
|
87
|
-
else
|
88
|
-
self.get($1)
|
89
|
-
end
|
90
|
-
else
|
95
|
+
key = method_name.to_s
|
96
|
+
|
97
|
+
if __getobj__.respond_to?(key)
|
91
98
|
super
|
99
|
+
elsif key.end_with?("=") && !args.empty?
|
100
|
+
set(key[0...-1], args[0])
|
101
|
+
elsif block_given?
|
102
|
+
set_block(key, &block)
|
103
|
+
else
|
104
|
+
get!(key)
|
92
105
|
end
|
93
106
|
end
|
94
107
|
|
95
|
-
def
|
108
|
+
def respond_to_missing?(*_args)
|
96
109
|
true
|
97
110
|
end
|
98
111
|
|
99
112
|
protected
|
100
113
|
|
101
|
-
def
|
102
|
-
|
103
|
-
|
104
|
-
set_block key do |inner|
|
105
|
-
value.each do |k,v|
|
106
|
-
inner.set(k, v)
|
107
|
-
end
|
108
|
-
end
|
109
|
-
else
|
110
|
-
@attributes[key.to_s] = value
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def get(key)
|
115
|
-
if @attributes.has_key?(key.to_s)
|
116
|
-
@attributes[key.to_s]
|
117
|
-
else
|
118
|
-
get_missing_key(key.to_s)
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
def get_missing_key(key)
|
123
|
-
if @raise_error_on_miss
|
124
|
-
raise "Missing key: #{key} in #{@attributes.inspect}"
|
125
|
-
else
|
126
|
-
nil
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def set_block(key, &block)
|
131
|
-
inner = @attributes[key.to_s] || child_new
|
132
|
-
block.call(inner)
|
114
|
+
def set_block(key)
|
115
|
+
inner = get(key) || spawn_child
|
116
|
+
yield(inner)
|
133
117
|
set(key, inner)
|
134
118
|
end
|
135
119
|
|
136
120
|
def find_config_yaml(path)
|
137
121
|
path = path.to_s
|
138
122
|
# give it back if it starts with a slash
|
139
|
-
|
123
|
+
if path.start_with?("/")
|
124
|
+
return nil unless ::File.file?(path)
|
125
|
+
|
126
|
+
return path
|
127
|
+
end
|
140
128
|
|
141
129
|
to_try = []
|
142
|
-
unless path
|
143
|
-
to_try <<
|
130
|
+
unless path.end_with?(".yml")
|
131
|
+
to_try << confg_root.join("config/#{path}.yml")
|
144
132
|
end
|
145
|
-
to_try <<
|
146
|
-
to_try <<
|
133
|
+
to_try << confg_root.join("config/#{path}")
|
134
|
+
to_try << confg_root.join(path)
|
147
135
|
|
148
136
|
to_try.each do |file|
|
149
|
-
return file if File.file?(file)
|
137
|
+
return file.to_s if File.file?(file)
|
150
138
|
end
|
151
139
|
|
152
|
-
|
153
|
-
end
|
154
|
-
|
155
|
-
def child_class
|
156
|
-
# same as ours
|
157
|
-
self.class
|
158
|
-
end
|
159
|
-
|
160
|
-
def child_raise_error_on_miss
|
161
|
-
# same as ours
|
162
|
-
@raise_error_on_miss
|
140
|
+
nil
|
163
141
|
end
|
164
142
|
|
165
|
-
def
|
166
|
-
|
143
|
+
def spawn_child
|
144
|
+
self.class.new(env: confg_env, root: confg_root)
|
167
145
|
end
|
168
146
|
|
169
147
|
end
|
data/lib/confg/erb_context.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "erb"
|
3
4
|
|
4
5
|
module Confg
|
5
6
|
class ErbContext
|
6
7
|
|
7
8
|
def evaluate(raw_content)
|
8
|
-
|
9
|
-
YAML.load(raw_content)
|
9
|
+
::ERB.new(raw_content).result(binding)
|
10
10
|
end
|
11
11
|
|
12
12
|
end
|
data/lib/confg/version.rb
CHANGED
data/test/confg_test.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
class ConfgTest < Minitest::Test
|
6
|
+
|
7
|
+
def config
|
8
|
+
@config ||= ::Confg::Configuration.new(root: __dir__, env: "test")
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_simple_keys_can_be_assigned
|
12
|
+
config.foo = "bar"
|
13
|
+
assert_equal "bar", config.foo
|
14
|
+
assert_equal({ "foo" => "bar" }, config.to_h)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_a_child_block_can_be_opened
|
18
|
+
config.foo do |child|
|
19
|
+
child.bar = "baz"
|
20
|
+
end
|
21
|
+
|
22
|
+
assert_equal "baz", config.foo.bar
|
23
|
+
assert_equal({ "foo" => { "bar" => "baz" } }, config.to_h)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_a_child_block_can_be_merged_by_assigning_a_hash
|
27
|
+
config.foo = { "bar" => "baz" }
|
28
|
+
assert_equal "baz", config.foo.bar
|
29
|
+
assert_equal({ "foo" => { "bar" => "baz" } }, config.to_h)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_an_error_is_raised_for_a_missing_key
|
33
|
+
assert_raises KeyError do
|
34
|
+
config.foo
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_hash_methods_are_accessible
|
39
|
+
config.foo = "foo_value"
|
40
|
+
config.bar = "bar_value"
|
41
|
+
|
42
|
+
assert_equal(%w[foo bar], config.keys)
|
43
|
+
assert_equal(%w[foo_value bar_value], config.values)
|
44
|
+
|
45
|
+
ikeys = []
|
46
|
+
config.each_key { |k| ikeys << k }
|
47
|
+
assert_equal(%w[foo bar], ikeys)
|
48
|
+
|
49
|
+
ivalues = []
|
50
|
+
config.each_value { |v| ivalues << v }
|
51
|
+
assert_equal(%w[foo_value bar_value], ivalues)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_a_yml_file_can_be_loaded_by_env
|
55
|
+
config.load_yaml("example.yml")
|
56
|
+
assert_equal({ "foo" => "foo", "env_setting" => "setting_test" }, config.to_h)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_a_yml_file_can_be_loaded_by_raw
|
60
|
+
config.load_yaml("example.yml", ignore_env: true)
|
61
|
+
assert_equal({
|
62
|
+
"shared" => { "foo" => "foo" },
|
63
|
+
"production" => { "env_setting" => "setting_prod", "foo" => "foo" },
|
64
|
+
"test" => { "env_setting" => "setting_test", "foo" => "foo" },
|
65
|
+
}, config.to_h)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/test/example.yml
ADDED
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,29 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: confg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0.rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Nelson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - ">="
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
type: :
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
38
|
- - ">="
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
27
69
|
description: Config the pipes
|
28
70
|
email:
|
29
71
|
- mike@mnelson.io
|
@@ -40,6 +82,9 @@ files:
|
|
40
82
|
- lib/confg/configuration.rb
|
41
83
|
- lib/confg/erb_context.rb
|
42
84
|
- lib/confg/version.rb
|
85
|
+
- test/confg_test.rb
|
86
|
+
- test/example.yml
|
87
|
+
- test/test_helper.rb
|
43
88
|
homepage: ''
|
44
89
|
licenses: []
|
45
90
|
metadata: {}
|
@@ -54,13 +99,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
99
|
version: '0'
|
55
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
101
|
requirements:
|
57
|
-
- - "
|
102
|
+
- - ">"
|
58
103
|
- !ruby/object:Gem::Version
|
59
|
-
version:
|
104
|
+
version: 1.3.1
|
60
105
|
requirements: []
|
61
|
-
|
62
|
-
rubygems_version: 2.6.8
|
106
|
+
rubygems_version: 3.0.3
|
63
107
|
signing_key:
|
64
108
|
specification_version: 4
|
65
109
|
summary: Sets shared variables for applications
|
66
|
-
test_files:
|
110
|
+
test_files:
|
111
|
+
- test/confg_test.rb
|
112
|
+
- test/example.yml
|
113
|
+
- test/test_helper.rb
|