canfig 0.0.6 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/canfig.rb +2 -0
- data/lib/canfig/config.rb +17 -5
- data/lib/canfig/env_config.rb +26 -0
- data/lib/canfig/instance.rb +1 -1
- data/lib/canfig/open_config.rb +1 -1
- data/lib/canfig/version.rb +1 -1
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ba430bd04920933ac504636144cb797457e51446c1850c02ce8ba76a2fab1136
|
4
|
+
data.tar.gz: 9b38d00d36ab5928c606a5aa869cb26597276743d9433f558b079dd8487ce2ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: acf50db63b8d815cb835fc8ae8213e009040aecb68f17d1c09e760ef5fe10087af3eb5c9db91507385fc62617c1bc146b6bc4113addde3e803b5c5fd072c5414
|
7
|
+
data.tar.gz: d97d1a93681dfe5c13ef6ea893eb2817cf5a6484a18b77ef40b2ea2da99fd12b2f54823c441f78d2db848e017091f0600e42b61dd20fa80e701549032890edac
|
data/lib/canfig.rb
CHANGED
@@ -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'
|
data/lib/canfig/config.rb
CHANGED
@@ -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
|
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,26 @@
|
|
1
|
+
module Canfig
|
2
|
+
class EnvConfig < OpenConfig
|
3
|
+
attr_reader :namespace
|
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)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def get(key, default=nil, &block)
|
14
|
+
super || env(key, default, &block)
|
15
|
+
end
|
16
|
+
|
17
|
+
def env(key, default=nil, &block)
|
18
|
+
@state[key.to_sym] ||= begin
|
19
|
+
key = key.to_s.underscore.upcase
|
20
|
+
key = key.gsub(/^#{namespace}/, '') if namespace
|
21
|
+
val = ENV.fetch("#{namespace}#{key}", default, &block)
|
22
|
+
val && ENV.key?(val.to_s) ? env(val, default, &block) : val
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/canfig/instance.rb
CHANGED
data/lib/canfig/open_config.rb
CHANGED
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.0.
|
4
|
+
version: 0.0.11
|
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: 2020-12-30 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,15 +92,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
92
|
- !ruby/object:Gem::Version
|
92
93
|
version: '0'
|
93
94
|
requirements: []
|
94
|
-
|
95
|
-
|
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:
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
- spec/fixtures/config.yml
|
100
102
|
- spec/canfig/config_spec.rb
|
101
103
|
- spec/canfig/open_config_spec.rb
|
102
104
|
- spec/canfig/yaml_spec.rb
|
103
105
|
- spec/canfig_spec.rb
|
104
|
-
- spec/fixtures/config.yml
|
105
|
-
- spec/spec_helper.rb
|