canfig 0.0.7 → 0.0.12

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: 325e086a529a793fd0ea5c35847862fd6b636e73ac769a598a938f8ea2acbf41
4
- data.tar.gz: 980081b20061f2fa099901253e530f49d98e2afa4ee6cf98de85894c8cbe34ee
3
+ metadata.gz: 0d19a1e225983ae80da02fce77a7d35aa9e05c2ea3c4460b434693de8d6a6340
4
+ data.tar.gz: 85e143439451d3209846e2e4761205ecbaeba44cbf42dbc89263d5f4f53f7e3d
5
5
  SHA512:
6
- metadata.gz: fa2134a5165c1a1d9cba3644eb6302d917e899b0c9043954dc830d9f69ef2f1d039f8949db5f661aecba96b867951049054054d58fdc237b791fe268698a5b0d
7
- data.tar.gz: b3d9cb5f625734681ae24f48eafbe12460f57cd2b4977081899a37ef82cc2f45ce33059de2b8f8b80d1b840b292197c72448cec118df4dad1b10f133b980ac2d
6
+ metadata.gz: 8bf05ccd8d29215d4dd5f66b989b782d8a688fcab557567b169426582136c4a39818bd26b6f8253ceb08b93f28b670ec026458de0aaf36ed4c7209a086392c94
7
+ data.tar.gz: 8e79d8407c0903522b79e75925e3697475deb4ac28f6efec2163d1624cffa5d5bb3f2ff4201259aa0fdd45d9018c5c547be3385c5f1f317d575d1358f29b3d8a
@@ -1,9 +1,11 @@
1
1
  require 'active_support/core_ext/array/extract_options'
2
2
  require 'active_support/core_ext/hash/keys'
3
+ require 'active_support/inflector'
3
4
  require 'canfig/version'
4
5
  require 'canfig/yaml'
5
6
  require 'canfig/config'
6
7
  require 'canfig/open_config'
8
+ require 'canfig/env_config'
7
9
  require 'canfig/module'
8
10
  require 'canfig/class'
9
11
  require 'canfig/instance'
@@ -36,13 +36,26 @@ module Canfig
36
36
  end
37
37
  end
38
38
 
39
+ def env(key, default=nil, &block)
40
+ @state[key] ||= begin
41
+ val = ENV.fetch(key.to_s.underscore.upcase, default, &block)
42
+ val && ENV.key?(val.to_s) ? env(val, default, &block) : val
43
+ end
44
+ end
45
+
46
+ def clear(*keys)
47
+ save_state! do
48
+ keys.each { |key| @state[key] = nil }
49
+ end
50
+ end
51
+
39
52
  def []=(key, val)
40
53
  set key, val
41
54
  end
42
55
 
43
- def get(key)
56
+ def get(key, default=nil, &block)
44
57
  raise NoMethodError, "undefined method `#{key.to_s}' for #{self.to_s}" unless allowed?(key)
45
- @state[key]
58
+ @state[key] || (block_given? ? yield : default)
46
59
  end
47
60
 
48
61
  def [](key)
@@ -99,7 +112,7 @@ module Canfig
99
112
  opt = meth.to_s.gsub(/=/,'').to_sym
100
113
  return set(opt, args.first) if allowed?(opt)
101
114
  else
102
- return @state[meth] if allowed?(meth)
115
+ return get(meth, *args, &block) if allowed?(meth)
103
116
  end
104
117
 
105
118
  super
@@ -107,8 +120,7 @@ module Canfig
107
120
 
108
121
  protected
109
122
 
110
- def initialize(*args, &block)
111
- options = args.extract_options!
123
+ def initialize(*args, **options, &block)
112
124
  @allowed = (args.map(&:to_sym) + options.symbolize_keys.keys).uniq
113
125
  @state = {}
114
126
  enable_state_saves!
@@ -0,0 +1,31 @@
1
+ module Canfig
2
+ class EnvConfig < OpenConfig
3
+ attr_reader :namespace
4
+
5
+ def configure_with_args(argh)
6
+ save_state! do
7
+ argh.symbolize_keys.each { |key,val| env(key, val) }
8
+ end
9
+ end
10
+
11
+ def get(key, default=nil, &block)
12
+ super || env(key, default, &block)
13
+ end
14
+
15
+ def env(key, default=nil, &block)
16
+ @state[key.to_sym] ||= begin
17
+ key = key.to_s.underscore.upcase
18
+ key = key.gsub(/^#{namespace}/, '') if namespace
19
+ val = ENV.fetch("#{namespace}#{key}", default, &block)
20
+ val && ENV.key?(val.to_s) ? env(val, default, &block) : val
21
+ end
22
+ end
23
+
24
+ protected
25
+
26
+ def initialize(namespace=nil, **attributes, &block)
27
+ @namespace = namespace ? "#{namespace.to_s.underscore.upcase.gsub(/_$/, '')}_" : namespace
28
+ super(**attributes, &block)
29
+ end
30
+ end
31
+ end
@@ -1,8 +1,8 @@
1
1
  module Canfig
2
2
  class OpenConfig < Config
3
3
  def set(key, val)
4
- super(key, val)
5
4
  @allowed << key unless @allowed.include?(key)
5
+ super(key, val)
6
6
  end
7
7
 
8
8
  def allowed?(opt)
@@ -1,3 +1,3 @@
1
1
  module Canfig
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.12'
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.7
4
+ version: 0.0.12
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: 2019-03-02 00:00:00.000000000 Z
11
+ date: 2020-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -62,6 +62,7 @@ files:
62
62
  - lib/canfig.rb
63
63
  - lib/canfig/class.rb
64
64
  - lib/canfig/config.rb
65
+ - lib/canfig/env_config.rb
65
66
  - lib/canfig/instance.rb
66
67
  - lib/canfig/module.rb
67
68
  - lib/canfig/open_config.rb
@@ -76,7 +77,7 @@ files:
76
77
  homepage: http://github.com/markrebec/canfig
77
78
  licenses: []
78
79
  metadata: {}
79
- post_install_message:
80
+ post_install_message:
80
81
  rdoc_options: []
81
82
  require_paths:
82
83
  - lib
@@ -91,9 +92,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
92
  - !ruby/object:Gem::Version
92
93
  version: '0'
93
94
  requirements: []
94
- rubyforge_project:
95
- rubygems_version: 2.7.7
96
- signing_key:
95
+ rubygems_version: 3.1.2
96
+ signing_key:
97
97
  specification_version: 4
98
98
  summary: Dead simple canned configuration for gems or whatever
99
99
  test_files: