confg 2.0.0 → 2.1.0.rc1
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.
- checksums.yaml +4 -4
- data/lib/confg.rb +11 -6
- data/lib/confg/configuration.rb +32 -27
- data/lib/confg/version.rb +1 -1
- data/test/confg_test.rb +13 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc9f4292ee8c5e8816455c7a49dd1bdd70d7fec552df2da425d98c03867fecab
|
4
|
+
data.tar.gz: eddb359546e89a16143da84ecf344d83162b460c62615e4ce3ad1995f0d3b74d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17aac0fec22fbedeb2340a7d90b84d9e3eae13952aed5104785426b9e60229976ca2e84b3f024bad1cc37e0c531c4b8f8b77a334fa8765f63596bcafaee8cae4
|
7
|
+
data.tar.gz: 61ab852e489d70b2d16e67ff7bd11393828d42bb9667785eea99bc4c4844f8c06225c2a7bc052d01fef989f237d06233707edc6dc34c3cf560502d36c929ca83
|
data/lib/confg.rb
CHANGED
@@ -8,6 +8,10 @@ module Confg
|
|
8
8
|
|
9
9
|
class << self
|
10
10
|
|
11
|
+
def cache
|
12
|
+
@cache ||= {}
|
13
|
+
end
|
14
|
+
|
11
15
|
def root
|
12
16
|
return @root if defined?(@root)
|
13
17
|
|
@@ -27,12 +31,13 @@ module Confg
|
|
27
31
|
self
|
28
32
|
end
|
29
33
|
|
30
|
-
def
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
+
def config(env: self.env, root: self.root)
|
35
|
+
config_key = "#{env}--#{root}"
|
36
|
+
out = (cache[config_key] ||= ::Confg::Configuration.new(env: env, root: root))
|
37
|
+
yield out if block_given?
|
38
|
+
out
|
34
39
|
end
|
35
|
-
alias config
|
40
|
+
alias configure config
|
36
41
|
|
37
42
|
def method_missing(method_name, *args, &block)
|
38
43
|
config.send(method_name, *args, &block)
|
@@ -65,7 +70,7 @@ module Confg
|
|
65
70
|
end
|
66
71
|
|
67
72
|
def reset!
|
68
|
-
remove_instance_variable("@
|
73
|
+
remove_instance_variable("@cache") if defined?(@cache)
|
69
74
|
remove_instance_variable("@env") if defined?(@env)
|
70
75
|
remove_instance_variable("@root") if defined?(@root)
|
71
76
|
end
|
data/lib/confg/configuration.rb
CHANGED
@@ -3,19 +3,18 @@
|
|
3
3
|
require "yaml"
|
4
4
|
|
5
5
|
module Confg
|
6
|
-
class Configuration
|
6
|
+
class Configuration
|
7
7
|
|
8
|
-
attr_reader :confg_env, :confg_root
|
8
|
+
attr_reader :confg_env, :confg_root, :confg_data
|
9
9
|
|
10
10
|
def initialize(env: Confg.env, root: Confg.root)
|
11
11
|
@confg_env = env.to_s
|
12
12
|
@confg_root = Pathname.new(root)
|
13
|
-
|
14
|
-
super({})
|
13
|
+
@confg_data = {}
|
15
14
|
end
|
16
15
|
|
17
16
|
def inspect
|
18
|
-
"#<#{self.class.name} #{
|
17
|
+
"#<#{self.class.name} #{confg_data.inspect}>"
|
19
18
|
end
|
20
19
|
|
21
20
|
def tmp(key, value)
|
@@ -34,24 +33,28 @@ module Confg
|
|
34
33
|
alias merge! merge
|
35
34
|
|
36
35
|
def to_h
|
37
|
-
|
36
|
+
confg_data.transform_values do |v|
|
38
37
|
v.is_a?(self.class) ? v.to_h : v
|
39
38
|
end
|
40
39
|
end
|
41
40
|
|
41
|
+
def key?(key)
|
42
|
+
confg_data.key?(key.to_s)
|
43
|
+
end
|
44
|
+
|
42
45
|
def get(key)
|
43
46
|
fetch(key) { nil }
|
44
47
|
end
|
45
48
|
alias [] get
|
46
49
|
|
47
50
|
def fetch(key, &block)
|
48
|
-
|
51
|
+
confg_data.fetch(key.to_s, &block)
|
49
52
|
end
|
50
53
|
|
51
54
|
def set(key, value = nil)
|
52
|
-
|
55
|
+
confg_data[key.to_s] = case value
|
53
56
|
when ::Hash
|
54
|
-
|
57
|
+
open(key) do |child|
|
55
58
|
value.each_pair do |k, v|
|
56
59
|
child.set(k, v)
|
57
60
|
end
|
@@ -62,6 +65,12 @@ module Confg
|
|
62
65
|
end
|
63
66
|
alias []= set
|
64
67
|
|
68
|
+
def open(key)
|
69
|
+
inner = get(key) || spawn_child
|
70
|
+
yield(inner)
|
71
|
+
set(key, inner)
|
72
|
+
end
|
73
|
+
|
65
74
|
def load_key(key)
|
66
75
|
# loads yaml file with given key
|
67
76
|
load_yaml(key, key: key)
|
@@ -95,32 +104,28 @@ module Confg
|
|
95
104
|
end
|
96
105
|
alias load_yml load_yaml
|
97
106
|
|
98
|
-
def method_missing(
|
99
|
-
key =
|
107
|
+
def method_missing(key, *args, &block)
|
108
|
+
key = key.to_s
|
109
|
+
return set(key[0...-1], args[0]) if key.end_with?("=")
|
110
|
+
return fetch(key) if key?(key)
|
100
111
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
elsif block_given?
|
106
|
-
open_block(key, &block)
|
107
|
-
else
|
108
|
-
fetch(key)
|
112
|
+
begin
|
113
|
+
confg_data.send(key, *args, &block)
|
114
|
+
rescue NoMethodError => e
|
115
|
+
raise KeyError, "Unrecognized key `#{key}`", e.backtrace
|
109
116
|
end
|
110
117
|
end
|
111
118
|
|
112
|
-
def respond_to_missing?(
|
113
|
-
|
119
|
+
def respond_to_missing?(key, include_private = false)
|
120
|
+
key = key.to_s
|
121
|
+
return true if key.end_with?("=")
|
122
|
+
return true if key?(key)
|
123
|
+
|
124
|
+
confg_data.respond_to?(key, include_private)
|
114
125
|
end
|
115
126
|
|
116
127
|
protected
|
117
128
|
|
118
|
-
def open_block(key)
|
119
|
-
inner = get(key) || spawn_child
|
120
|
-
yield(inner)
|
121
|
-
set(key, inner)
|
122
|
-
end
|
123
|
-
|
124
129
|
def find_config_yaml(path)
|
125
130
|
path = path.to_s
|
126
131
|
# give it back if it starts with a slash
|
data/lib/confg/version.rb
CHANGED
data/test/confg_test.rb
CHANGED
@@ -15,7 +15,7 @@ class ConfgTest < Minitest::Test
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_a_child_block_can_be_opened
|
18
|
-
config.foo do |child|
|
18
|
+
config.open :foo do |child|
|
19
19
|
child.bar = "baz"
|
20
20
|
end
|
21
21
|
|
@@ -65,4 +65,16 @@ class ConfgTest < Minitest::Test
|
|
65
65
|
}, config.to_h)
|
66
66
|
end
|
67
67
|
|
68
|
+
def test_top_level_configs_are_cached_in_root_namespace
|
69
|
+
::Confg.send :reset!
|
70
|
+
assert_equal({}, ::Confg.cache)
|
71
|
+
|
72
|
+
default_config = ::Confg.config(env: "test", root: "/")
|
73
|
+
custom_config = ::Confg.config(env: "foobar", root: "/Users/x/")
|
74
|
+
|
75
|
+
refute_equal default_config.object_id, custom_config.object_id
|
76
|
+
assert_equal 2, ::Confg.cache.size
|
77
|
+
assert_equal %w[test--/ foobar--/Users/x/], ::Confg.cache.keys
|
78
|
+
end
|
79
|
+
|
68
80
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: confg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.1.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: 2020-04-
|
11
|
+
date: 2020-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -99,9 +99,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
99
|
version: '0'
|
100
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
101
|
requirements:
|
102
|
-
- - "
|
102
|
+
- - ">"
|
103
103
|
- !ruby/object:Gem::Version
|
104
|
-
version:
|
104
|
+
version: 1.3.1
|
105
105
|
requirements: []
|
106
106
|
rubygems_version: 3.0.3
|
107
107
|
signing_key:
|