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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba430bd04920933ac504636144cb797457e51446c1850c02ce8ba76a2fab1136
4
- data.tar.gz: 9b38d00d36ab5928c606a5aa869cb26597276743d9433f558b079dd8487ce2ec
3
+ metadata.gz: e93bb14cbee7d6538f347e414b773babf038266f19b21e2f8a84ee09c18696de
4
+ data.tar.gz: 49562f881619ae061b28b42fbc8fbb03791147ba271bf9874a66f2605534b767
5
5
  SHA512:
6
- metadata.gz: acf50db63b8d815cb835fc8ae8213e009040aecb68f17d1c09e760ef5fe10087af3eb5c9db91507385fc62617c1bc146b6bc4113addde3e803b5c5fd072c5414
7
- data.tar.gz: d97d1a93681dfe5c13ef6ea893eb2817cf5a6484a18b77ef40b2ea2da99fd12b2f54823c441f78d2db848e017091f0600e42b61dd20fa80e701549032890edac
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
- @state[key] = val
45
+ state[key] = val
36
46
  end
37
47
  end
38
48
 
39
49
  def env(key, default=nil, &block)
40
- @state[key] ||= begin
50
+ state[key] ||= begin
41
51
  val = ENV.fetch(key.to_s.underscore.upcase, default, &block)
42
- val && ENV.key?(val.to_s) ? env(val, default, &block) : 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| @state[key] = nil }
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
- @state[key] || (block_given? ? yield : default)
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, @state[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], @state[key]]] if @saved_state[key] != @state[key] }.compact]
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
@@ -2,11 +2,9 @@ module Canfig
2
2
  class EnvConfig < OpenConfig
3
3
  attr_reader :namespace
4
4
 
5
- def initialize(namespace=nil, **attributes, &block)
6
- @namespace = namespace ? "#{namespace.to_s.underscore.upcase.gsub(/_$/, '')}_" : namespace
7
- super(*attributes.keys, &block)
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 && ENV.key?(val.to_s) ? env(val, default, &block) : 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
@@ -1,3 +1,3 @@
1
1
  module Canfig
2
- VERSION = '0.0.11'
2
+ VERSION = '0.1.1'
3
3
  end
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.0.11
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: 2020-12-30 00:00:00.000000000 Z
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