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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config_hash.rb +20 -8
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4402ad71ee24799c7a20a27289000faad82afe5f
4
- data.tar.gz: 5da3733c25449940c827652a7113832b30f090d0
3
+ metadata.gz: dbae7323037e724fba6ee978ad49cd5475661809
4
+ data.tar.gz: 711be611c58d42964119aeaa90a99924c8aa3f60
5
5
  SHA512:
6
- metadata.gz: 2dba44f34cc99e567b931d5666c07525f7dd65c4d3dc43eadb22e0742942cd11d90d7ec80caff1065e9699a072063eaaef6969c2934128b18b3b62ad33ad54f0
7
- data.tar.gz: 42ba9db1f57d08e82f2d43b8c53837edad8b088fa3858e3c46eec656ee7b901ae6d1554da4e2f5e83861404f4fbfed873fac9e24b137d1b79d00b5c616ef784b
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 = options.fetch(:freeze, true)
10
- @lazy_loading = options.fetch(:lazy_loading, false)
11
- @processors = options.fetch(:processors, [])
12
- @processed = {}
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: @freeze, processors: @processors, lazy_loading: @lazy_loading,
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.7
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-04 00:00:00.000000000 Z
11
+ date: 2017-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler