config_hash 1.0.0 → 1.1.0

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 +18 -28
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73f82976e9390c9a3415a6eb9184d8b198a4593f
4
- data.tar.gz: 3802b8e1088be490850e06034d5e8ffa7a8de439
3
+ metadata.gz: 11a34ee8349781b3893bc7ef8dfa913c59174d8b
4
+ data.tar.gz: fbdcc4607f4a4c39c142680c57cacce06eaf243c
5
5
  SHA512:
6
- metadata.gz: b3012332aeb6d10baf6d1f8183337dce40815a9428cf91b69c0eb25902a0cff794eaac1e9a2e11d01d60cb7bb66c16bca065f74db8b25b0c167e7ed42555a960
7
- data.tar.gz: 17c079178938569abf148d7292b6a91e1f2d85719e30b7f90a0d68cdcdb70abc28d09dc2b1bfe5b3a4e006d2937dfaf604be0fdff1b8ccda7399b2b2b536cbe3
6
+ metadata.gz: 8d2bdb3ecd78db04b9de48f9ead579dbaef0ae5f42e252acf2a7b31b8f829e062267640eb27444469525663e2317408ccdc3e43e78b8cc62bcff9d3d69871a2c
7
+ data.tar.gz: 34ebc465ae1cb5bd59fbeca0c71803883a45e4bf3fd4a23e556f409cc1979600b240d32283820f3f3c3842c259e811e3e05022911752abc1e17b15663e5899bc
data/lib/config_hash.rb CHANGED
@@ -16,28 +16,19 @@ class ConfigHash < Hash
16
16
  @processors << ConfigHash::Processors.method(proc) if options[proc]
17
17
  end
18
18
 
19
- super(default, &default_block)
20
-
21
- # recursively construct this hash from the passed in hash.
19
+ # recursively reconstruct this hash from the passed in hash.
22
20
  hash.each do |key, value|
23
- key = key.is_a?(String) ? key.to_sym : key # force strings to symbols
24
- self[construct(key)] = construct(value)
21
+ key = key.to_sym if key.is_a?(String)
22
+ self[key] = construct(value)
25
23
 
26
- if key.is_a? Symbol # allow '.' notation for symbol keys.
27
- self.instance_eval("def #{key}; process(self[:#{key}]); end")
24
+ class << self; self; end.class_eval do
25
+ define_method(key) { self[key] }
28
26
  end
29
27
  end
30
28
 
31
29
  self.freeze if @freeze
32
30
  end
33
31
 
34
- def [](key, in_process=false)
35
- key = key.is_a?(String) ? key.to_sym : key
36
- return super(key) if in_process
37
-
38
- process(self.send(:[], key, true)) # make sure to process reutrn values
39
- end
40
-
41
32
  def method_missing(method, *args)
42
33
  return super(method, *args) if @freeze
43
34
 
@@ -45,9 +36,10 @@ class ConfigHash < Hash
45
36
  if method =~ /^(.*)=$/ && args.length == 1
46
37
  key = method.to_s.tr('=', '').to_sym
47
38
  self[key] = args[0]
48
- self.instance_eval("def #{key}; process(self[:#{key}]); end")
49
- else
50
- nil # it's a non-defined value.
39
+
40
+ class << self; self; end.class_eval do
41
+ define_method(key) { self[key] }
42
+ end
51
43
  end
52
44
  end
53
45
 
@@ -55,7 +47,10 @@ class ConfigHash < Hash
55
47
  return super(key, &blk) if @freeze
56
48
 
57
49
  key = key.is_a?(String) ? key.to_sym : key
58
- instance_eval("undef #{key}") if respond_to?(key)
50
+ class << self; self; end.class_eval do
51
+ remove_method(key)
52
+ end
53
+
59
54
  super(key, &blk)
60
55
  end
61
56
 
@@ -67,21 +62,16 @@ class ConfigHash < Hash
67
62
 
68
63
  def construct(value)
69
64
  case value
70
- when ConfigHash then value
71
- when Hash then ConfigHash.new(
65
+ when ConfigHash then value
66
+ when Hash then ConfigHash.new(
72
67
  value,
73
68
  value.default,
74
69
  freeze: @freeze, processors: @processors,
75
70
  &value.default_proc
76
71
  )
77
- when Array then dup_if_appropriate(value).map { |sv| construct(sv) }
78
- else dup_if_appropriate(value)
72
+ when Array then value.map { |sv| construct(sv) }
73
+ when Class, Module, Proc, Method then value
74
+ else @processors.any? ? process(value) : value
79
75
  end.tap { |calced| calced.freeze if @freeze }
80
76
  end
81
-
82
- def dup_if_appropriate(v)
83
- # if it is a class, module, or proc, DO NOT DUP
84
- return v if [Class, Module, Proc].any? { |kls| v.instance_of?(kls) }
85
- v.dup rescue v # on symbol, integer just return value
86
- end
87
77
  end
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.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Lome
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-30 00:00:00.000000000 Z
11
+ date: 2017-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler