config_hash 1.1.9 → 1.1.10
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 +38 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 292b79c9f455f289d56b1f2622342e8768ce2248
|
4
|
+
data.tar.gz: c9f444e4c1458d70b3fdfc10551f2a02a8d62e46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e65f0156b16ae9d3b51a49e4ffa89614907aefc8d27b2592f375dba1a15a4074b18858df6945c66330f1b27a8d65c1bf8a573c23bf838e2237484758512d83c
|
7
|
+
data.tar.gz: d22bf341aa33d14aa05eb1253ddee70a3680a0c870e037a6fe416841b1046d5a011a83dcf1c791dcf4c66d350cdeac845ad4868b12f61c759884ae4dcf1ba75c
|
data/lib/config_hash.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'config_hash/processors'
|
2
2
|
|
3
3
|
class ConfigHash < Hash
|
4
|
-
|
4
|
+
include ConfigHash::Enumerable
|
5
|
+
|
6
|
+
def initialize(hash, **options)
|
5
7
|
unless hash.kind_of?(Hash)
|
6
8
|
raise ArgumentError.new("first argument must be a hash!")
|
7
9
|
end
|
@@ -20,6 +22,8 @@ class ConfigHash < Hash
|
|
20
22
|
@processors << ConfigHash::Processors.method(proc) if options[proc]
|
21
23
|
end
|
22
24
|
|
25
|
+
super(hash.default, &hash.default_proc)
|
26
|
+
|
23
27
|
# recursively reconstruct this hash from the passed in hash.
|
24
28
|
hash.each do |key, value|
|
25
29
|
key = key.to_sym if key.is_a?(String)
|
@@ -34,10 +38,10 @@ class ConfigHash < Hash
|
|
34
38
|
def [](key)
|
35
39
|
key = key.to_sym if key.is_a? String
|
36
40
|
if @raise_on_missing && !self.include?(key)
|
37
|
-
raise
|
41
|
+
raise KeyError.new("key not found: #{key}")
|
38
42
|
end
|
39
43
|
|
40
|
-
if @lazy_loading
|
44
|
+
if @lazy_loading && @processors.any?
|
41
45
|
@processed[key] ||= process(super(key))
|
42
46
|
else
|
43
47
|
super(key)
|
@@ -45,8 +49,6 @@ class ConfigHash < Hash
|
|
45
49
|
end
|
46
50
|
|
47
51
|
def []=(key, value)
|
48
|
-
return super(key, value) if self.frozen? # will raise an error.
|
49
|
-
|
50
52
|
key = key.to_sym if key.is_a? String
|
51
53
|
super(key, value).tap { __build_accessor(key) }
|
52
54
|
end
|
@@ -56,21 +58,43 @@ class ConfigHash < Hash
|
|
56
58
|
# if appropriate.
|
57
59
|
|
58
60
|
def values
|
59
|
-
super unless @lazy_loading && @processors.any?
|
61
|
+
return super unless @lazy_loading && @processors.any?
|
60
62
|
self.keys.map { |k| self[k] }
|
61
63
|
end
|
62
64
|
|
63
65
|
# use [] accessor to process values correctly for lazy loading
|
64
66
|
def each
|
65
|
-
super unless @lazy_loading && @processors.any?
|
67
|
+
return super unless @lazy_loading && @processors.any?
|
66
68
|
self.keys.each { |k| yield k, self[k] }
|
67
69
|
end
|
68
70
|
|
69
71
|
def map
|
70
|
-
super unless @lazy_loading && @processors.any?
|
72
|
+
return super unless @lazy_loading && @processors.any?
|
71
73
|
self.keys.map { |k| yield k, self[k] }
|
72
74
|
end
|
73
75
|
|
76
|
+
def select
|
77
|
+
return super unless @lazy_loading && @processors.any?
|
78
|
+
selected = Hash[
|
79
|
+
self.keys.select { |k| yield k, self[k] }.map { |k| [k, self[k]] }
|
80
|
+
]
|
81
|
+
self.class.new(selected,
|
82
|
+
freeze: @freeze,
|
83
|
+
processors: @processors,
|
84
|
+
lazy_loading: @lazy_loading,
|
85
|
+
raise_on_missing: @raise_on_missing,
|
86
|
+
)
|
87
|
+
end
|
88
|
+
|
89
|
+
[:all?, :any?, :none?, :one?].each do |method|
|
90
|
+
define_method(method) do
|
91
|
+
return super unless @lazy_loading && @processors.any?
|
92
|
+
self.keys.send(method) { |k| yield k, self[k] }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
## misc. overrides
|
97
|
+
|
74
98
|
def method_missing(method, *args)
|
75
99
|
# if we're not freezing, we can allow assignment and expect nil results.
|
76
100
|
if method =~ /^(.*)=$/ && args.length == 1
|
@@ -81,7 +105,7 @@ class ConfigHash < Hash
|
|
81
105
|
|
82
106
|
self[key] # assignment should return the value
|
83
107
|
else
|
84
|
-
raise
|
108
|
+
raise KeyError.new("key not found: #{method}!") if @raise_on_missing
|
85
109
|
nil
|
86
110
|
end
|
87
111
|
end
|
@@ -107,6 +131,11 @@ class ConfigHash < Hash
|
|
107
131
|
end
|
108
132
|
end
|
109
133
|
|
134
|
+
# returns the value as reduced by the processors.
|
135
|
+
# only applies to scalars that are not class, module, proc, or method.
|
136
|
+
#
|
137
|
+
# @param [Mixed] value The value to process
|
138
|
+
# @return [Mixed] the value, modified by calls to the processor.
|
110
139
|
def process(value)
|
111
140
|
case value
|
112
141
|
when ConfigHash then value # the sub-config-hash will process on its own
|
@@ -122,12 +151,10 @@ class ConfigHash < Hash
|
|
122
151
|
when ConfigHash then value
|
123
152
|
when Hash then ConfigHash.new(
|
124
153
|
value,
|
125
|
-
value.default,
|
126
154
|
freeze: @freeze,
|
127
155
|
processors: @processors,
|
128
156
|
lazy_loading: @lazy_loading,
|
129
157
|
raise_on_missing: @raise_on_missing,
|
130
|
-
&value.default_proc
|
131
158
|
)
|
132
159
|
when Array then value.map { |sv| construct(sv) }
|
133
160
|
when Class, Module, Proc, Method then value
|