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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config_hash.rb +38 -11
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eb58d0a645f55cb6a71ffff9085992006556e8d2
4
- data.tar.gz: 0e5d6a482720853a4b81f00a1b074422f0c1c17b
3
+ metadata.gz: 292b79c9f455f289d56b1f2622342e8768ce2248
4
+ data.tar.gz: c9f444e4c1458d70b3fdfc10551f2a02a8d62e46
5
5
  SHA512:
6
- metadata.gz: ef38ff84436fb6ea9252a874f0a0cdb11d9666501c43cce4f99de40d88e5be6e9bbb24ed5e3b044c0ee472a27c27cb01e4375b108973fbb2c81fadef85b6ad4c
7
- data.tar.gz: 377c287467c1995f4a903f44519fbc1b8a10108a25eac433538ee2abc6942f3db7a1ff0dd5e0d9e77037d0fc30973a5d9762b91490f40b3a3cfc15f09873fa35
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
- def initialize(hash, default=nil, **options, &default_block)
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 ArgumentError.new("Missing Key #{key} in #{self.keys}!")
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 ArgumentError.new("Missing Key #{method}!") if @raise_on_missing
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.9
4
+ version: 1.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Lome