config_hash 1.1.7 → 1.1.8
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/config_hash.rb +20 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbae7323037e724fba6ee978ad49cd5475661809
|
4
|
+
data.tar.gz: 711be611c58d42964119aeaa90a99924c8aa3f60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b22170f3f6faa4492a9d53651556311bb1fd75576c4072cb53702cb4409a85801c6669781e033776bb3d39274556bfa9501cfea5b6c5cb889f2f6583abed1391
|
7
|
+
data.tar.gz: 455d63c4b1481c77b9d2bf511b8d9a9744f31d19e7e2c76cafdaf6cc19d671e992216394feef381cf5390d3997f2de99cc7283d1d8963d1ac0cd75afa1fc7491
|
data/lib/config_hash.rb
CHANGED
@@ -6,10 +6,11 @@ class ConfigHash < Hash
|
|
6
6
|
raise ArgumentError.new("first argument must be a hash!")
|
7
7
|
end
|
8
8
|
|
9
|
-
@freeze
|
10
|
-
@lazy_loading
|
11
|
-
@processors
|
12
|
-
@
|
9
|
+
@freeze = options.fetch(:freeze, true)
|
10
|
+
@lazy_loading = options.fetch(:lazy_loading, false)
|
11
|
+
@processors = options.fetch(:processors, [])
|
12
|
+
@raise_on_missing = options.fetch(:raise_on_missing, true)
|
13
|
+
@processed = {}
|
13
14
|
|
14
15
|
if !(@processors.is_a?(Array) && @processors.all? { |p| p.is_a?(Proc) || p.is_a?(Method) })
|
15
16
|
raise ArgumentError.new("processors must be a list of callables!")
|
@@ -30,6 +31,10 @@ class ConfigHash < Hash
|
|
30
31
|
|
31
32
|
def [](key)
|
32
33
|
key = key.to_sym if key.is_a? String
|
34
|
+
if @raise_on_missing && !self.include?(key)
|
35
|
+
raise ArgumentError.new("Missing Key #{key} in #{self.keys}!")
|
36
|
+
end
|
37
|
+
|
33
38
|
if @lazy_loading
|
34
39
|
@processed[key] ||= process(super(key))
|
35
40
|
else
|
@@ -38,20 +43,24 @@ class ConfigHash < Hash
|
|
38
43
|
end
|
39
44
|
|
40
45
|
def []=(key, value)
|
41
|
-
return super(key, value) if self.frozen?
|
46
|
+
return super(key, value) if self.frozen? # will raise an error.
|
42
47
|
|
43
48
|
key = key.to_sym if key.is_a? String
|
44
49
|
super(key, value).tap { __build_accessor(key) }
|
45
50
|
end
|
46
51
|
|
47
52
|
def method_missing(method, *args)
|
48
|
-
return super(method, *args) if @freeze
|
49
|
-
|
50
53
|
# if we're not freezing, we can allow assignment and expect nil results.
|
51
54
|
if method =~ /^(.*)=$/ && args.length == 1
|
55
|
+
return super(method, *args) if @freeze # will raise an error
|
52
56
|
key = method.to_s.tr('=', '').to_sym
|
53
57
|
self[key] = args[0]
|
54
58
|
__build_accessor(key)
|
59
|
+
|
60
|
+
self[key] # assignment should return the value
|
61
|
+
else
|
62
|
+
raise ArgumentError.new("Missing Key #{method}!") if @raise_on_missing
|
63
|
+
nil
|
55
64
|
end
|
56
65
|
end
|
57
66
|
|
@@ -86,7 +95,10 @@ class ConfigHash < Hash
|
|
86
95
|
when Hash then ConfigHash.new(
|
87
96
|
value,
|
88
97
|
value.default,
|
89
|
-
freeze:
|
98
|
+
freeze: @freeze,
|
99
|
+
processors: @processors,
|
100
|
+
lazy_loading: @lazy_loading,
|
101
|
+
raise_on_missing: @raise_on_missing,
|
90
102
|
&value.default_proc
|
91
103
|
)
|
92
104
|
when Array then value.map { |sv| construct(sv) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: config_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Lome
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|