canfig 0.0.11 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/canfig/config.rb +22 -8
- data/lib/canfig/env_config.rb +14 -6
- data/lib/canfig/version.rb +1 -1
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e93bb14cbee7d6538f347e414b773babf038266f19b21e2f8a84ee09c18696de
|
4
|
+
data.tar.gz: 49562f881619ae061b28b42fbc8fbb03791147ba271bf9874a66f2605534b767
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e01a0997c25733e07ce8d6ad23e02776f45776e19fadb76dd092d8e0cfe9e4feb6105d71d5fdb443454cb848bf36b2a97574db3b35ce71988541b759505c7c7
|
7
|
+
data.tar.gz: a81749b2737652a52f9a93b7f9ad651db82e6e38f1f30e6890b4d20dfba92ed10a31ee2e1f0967f9ccf80c2aed05e983eb6ba6628f491e299b1a1ae7d7875276
|
data/lib/canfig/config.rb
CHANGED
@@ -28,24 +28,37 @@ module Canfig
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
+
def thread_configs
|
32
|
+
Thread.current[:_canfig_configs] ||= {}
|
33
|
+
Thread.current[:_canfig_configs]
|
34
|
+
end
|
35
|
+
|
36
|
+
def state
|
37
|
+
thread_configs[object_id] ||= {}
|
38
|
+
thread_configs[object_id]
|
39
|
+
end
|
40
|
+
|
31
41
|
def set(key, val)
|
32
42
|
raise NoMethodError, "undefined method `#{key.to_s}=' for #{self.to_s}" unless allowed?(key)
|
33
43
|
|
34
44
|
save_state! do
|
35
|
-
|
45
|
+
state[key] = val
|
36
46
|
end
|
37
47
|
end
|
38
48
|
|
39
49
|
def env(key, default=nil, &block)
|
40
|
-
|
50
|
+
state[key] ||= begin
|
41
51
|
val = ENV.fetch(key.to_s.underscore.upcase, default, &block)
|
42
|
-
val
|
52
|
+
val = default if val.blank?
|
53
|
+
val = env(val, default, &block) if val && ENV.key?(val.to_s)
|
54
|
+
val = eval(val) if val == 'true' || val == 'false'
|
55
|
+
val
|
43
56
|
end
|
44
57
|
end
|
45
58
|
|
46
59
|
def clear(*keys)
|
47
60
|
save_state! do
|
48
|
-
keys.each { |key|
|
61
|
+
keys.each { |key| state[key] = nil }
|
49
62
|
end
|
50
63
|
end
|
51
64
|
|
@@ -55,7 +68,9 @@ module Canfig
|
|
55
68
|
|
56
69
|
def get(key, default=nil, &block)
|
57
70
|
raise NoMethodError, "undefined method `#{key.to_s}' for #{self.to_s}" unless allowed?(key)
|
58
|
-
|
71
|
+
state_val = state[key]
|
72
|
+
return (block_given? ? yield : default) if state_val.nil?
|
73
|
+
state_val
|
59
74
|
end
|
60
75
|
|
61
76
|
def [](key)
|
@@ -63,11 +78,11 @@ module Canfig
|
|
63
78
|
end
|
64
79
|
|
65
80
|
def to_h
|
66
|
-
Hash[@allowed.map { |key| [key,
|
81
|
+
Hash[@allowed.map { |key| [key, state[key]] }]
|
67
82
|
end
|
68
83
|
|
69
84
|
def changed
|
70
|
-
Hash[@allowed.map { |key| [key, [@saved_state[key],
|
85
|
+
Hash[@allowed.map { |key| [key, [@saved_state[key], state[key]]] if @saved_state[key] != state[key] }.compact]
|
71
86
|
end
|
72
87
|
|
73
88
|
def changed?(key)
|
@@ -122,7 +137,6 @@ module Canfig
|
|
122
137
|
|
123
138
|
def initialize(*args, **options, &block)
|
124
139
|
@allowed = (args.map(&:to_sym) + options.symbolize_keys.keys).uniq
|
125
|
-
@state = {}
|
126
140
|
enable_state_saves!
|
127
141
|
configure options, &block
|
128
142
|
end
|
data/lib/canfig/env_config.rb
CHANGED
@@ -2,11 +2,9 @@ module Canfig
|
|
2
2
|
class EnvConfig < OpenConfig
|
3
3
|
attr_reader :namespace
|
4
4
|
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
attributes.each do |key,val|
|
9
|
-
env(key, val)
|
5
|
+
def configure_with_args(argh)
|
6
|
+
save_state! do
|
7
|
+
argh.symbolize_keys.each { |key,val| env(key, val) }
|
10
8
|
end
|
11
9
|
end
|
12
10
|
|
@@ -19,8 +17,18 @@ module Canfig
|
|
19
17
|
key = key.to_s.underscore.upcase
|
20
18
|
key = key.gsub(/^#{namespace}/, '') if namespace
|
21
19
|
val = ENV.fetch("#{namespace}#{key}", default, &block)
|
22
|
-
val
|
20
|
+
val = default if val.blank?
|
21
|
+
val = env(val, default, &block) if val && ENV.key?(val.to_s)
|
22
|
+
val = eval(val) if val == 'true' || val == 'false'
|
23
|
+
val
|
23
24
|
end
|
24
25
|
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def initialize(namespace=nil, **attributes, &block)
|
30
|
+
@namespace = namespace ? "#{namespace.to_s.underscore.upcase.gsub(/_$/, '')}_" : namespace
|
31
|
+
super(**attributes, &block)
|
32
|
+
end
|
25
33
|
end
|
26
34
|
end
|
data/lib/canfig/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: canfig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Rebec
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -77,7 +77,7 @@ files:
|
|
77
77
|
homepage: http://github.com/markrebec/canfig
|
78
78
|
licenses: []
|
79
79
|
metadata: {}
|
80
|
-
post_install_message:
|
80
|
+
post_install_message:
|
81
81
|
rdoc_options: []
|
82
82
|
require_paths:
|
83
83
|
- lib
|
@@ -93,13 +93,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubygems_version: 3.1.2
|
96
|
-
signing_key:
|
96
|
+
signing_key:
|
97
97
|
specification_version: 4
|
98
98
|
summary: Dead simple canned configuration for gems or whatever
|
99
99
|
test_files:
|
100
|
-
- spec/spec_helper.rb
|
101
|
-
- spec/fixtures/config.yml
|
102
|
-
- spec/canfig/config_spec.rb
|
103
100
|
- spec/canfig/open_config_spec.rb
|
104
101
|
- spec/canfig/yaml_spec.rb
|
102
|
+
- spec/canfig/config_spec.rb
|
103
|
+
- spec/fixtures/config.yml
|
104
|
+
- spec/spec_helper.rb
|
105
105
|
- spec/canfig_spec.rb
|