config-hash 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d7d77612387f4c98acb30d2174f842e9bc686cee148dd35971a50b1af1ffc36
4
- data.tar.gz: 5be88d29ba90fb1f0076fb690969e642c8bfc5d7faa98f3235c592f059f7397d
3
+ metadata.gz: f34e652d639c677341b14b1ae82c77eb72aa1e2618f5de14c0853089eb12fb7e
4
+ data.tar.gz: f428443a9f4d33fad40f29352ea2b8da536dfacfe4e8671eeb170b9b175cda0e
5
5
  SHA512:
6
- metadata.gz: 945fb4e4c3fcadc7dc433f36a512ed26ef06bfc17349ea14dae2e6f5bfc063c9821649739fe9bdb85c4ea2f48e8a3ec8bd60ab52fcc79c8175dde38c89c37509
7
- data.tar.gz: 35a042c344fa0e43902ed11d7b36111af477bd37eeab5e155b35b1360e85f3fe7898df22a84da19e56415b5e9de262f02972eac1d39a3d657d9677555a4f827b
6
+ metadata.gz: f8e828acf1e07a92a1e03e40e9b1fe8eb4e81eacfa6f1fac8de58606d899b86ece8ac01d4160d80a4f36d249b7c5b3397928e1ec7c03afb2fb78082f825db83e
7
+ data.tar.gz: aef9036c64d8baa3b6a18a0748aa39d039b31b2b8cce4f919b8de1166a9e48dce8e19f1c8f6ef52cebc90eef19b06faad3030e37503421149c02bc703fb7d7d6
data/config-hash.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "config-hash"
5
- s.version = "0.6.0"
5
+ s.version = "0.7.0"
6
6
  s.author = "Steve Shreeve"
7
7
  s.email = "steve.shreeve@gmail.com"
8
8
  s.summary = "A safe, homoiconic, Ruby hash supporting dot notation"
data/lib/config-hash.rb CHANGED
@@ -1,5 +1,3 @@
1
- Encoding.default_external = "UTF-8"
2
-
3
1
  class Hash
4
2
  def -@; NormalHash[self]; end
5
3
  def +@; ConfigHash[self]; end
@@ -11,6 +9,10 @@ end
11
9
  class ConfigHash < Hash
12
10
  SEPARATORS ||= %r|[./]|
13
11
 
12
+ def self.[](hash=nil)
13
+ new(hash)
14
+ end
15
+
14
16
  def self.load(path="config.rb", var="config")
15
17
  path = File.expand_path(path)
16
18
  eval <<-"end", binding, path, 0
@@ -22,7 +24,7 @@ class ConfigHash < Hash
22
24
 
23
25
  def initialize(hash=nil)
24
26
  super()
25
- merge!(hash) if hash
27
+ update(hash) if hash
26
28
  end
27
29
 
28
30
  def import(root, glob)
@@ -36,22 +38,22 @@ class ConfigHash < Hash
36
38
  self
37
39
  end
38
40
 
41
+ def key?(key)
42
+ super(key.to_s)
43
+ end
44
+
39
45
  def [](key)
46
+ our = self.class
40
47
  key = key.to_s
41
- if !key?(key) && key =~ SEPARATORS
42
- val = self
43
- key.split(SEPARATORS).each do |tag|
44
- if !val.instance_of?(self.class)
45
- return super(key)
46
- elsif val.key?(tag)
47
- val = val[tag]
48
- elsif tag == "*" && val.size == 1
49
- val = val[val.keys.first]
50
- else
51
- return super(key)
48
+
49
+ if !key?(key) && key =~ SEPARATORS && (ary = key.split SEPARATORS)
50
+ val = ary.inject(self) do |obj, sub|
51
+ if not our === obj then return super(key)
52
+ elsif obj.key?(sub) then obj[sub]
53
+ elsif sub == "*" then obj[obj.keys.first]
54
+ else return super(key)
52
55
  end
53
56
  end
54
- val
55
57
  else
56
58
  super(key)
57
59
  end
@@ -61,55 +63,37 @@ class ConfigHash < Hash
61
63
  our = self.class
62
64
  key = key.to_s
63
65
  val = our.new(val) if val.instance_of?(Hash)
64
- if val.instance_of?(NormalHash)
65
- super(key, val)
66
- elsif key =~ SEPARATORS
67
- all = key.split(SEPARATORS)
68
- key = all.pop
69
- top = all.inject(self) do |top, tag|
70
- if top.key?(tag) && (try = top[tag]).instance_of?(our)
71
- top = try
72
- else
73
- top = top[tag] = our.new
74
- end
66
+
67
+ if !key?(key) && key =~ SEPARATORS && (ary = key.split SEPARATORS)
68
+ key = ary.pop
69
+ obj = ary.inject(self) do |obj, sub|
70
+ obj.key?(sub) && our === (try = obj[sub]) ? try : (obj[sub] = our.new)
75
71
  end
76
- top[key] = val
72
+ obj[key] = val
77
73
  else
78
74
  super(key, val)
79
75
  end
80
76
  end
81
77
 
82
- alias store []=
78
+ alias_method :store, :[]=
83
79
 
84
- def key?(key)
85
- super(key.to_s)
86
- end
87
-
88
- def merge!(other_hash)
89
- raise ArgumentError unless Hash === other_hash
90
- other_hash.each do |k, v|
91
- if block_given? && key?(k)
92
- self[k] = yield(k, self[k], v)
93
- else
94
- self[k] = v
95
- end
96
- end
80
+ def update(hash)
81
+ raise ArgumentError unless Hash === hash
82
+ hash.each {|key, val| self[key] = val}
97
83
  self
98
84
  end
99
85
 
100
- alias update merge!
86
+ alias_method :merge!, :update
101
87
 
102
88
  def to_hash
103
89
  Hash[self]
104
90
  end
105
91
 
106
- def method_missing(sym, *args, &block)
107
- if sym =~ /=$/
108
- self[$`] = args.first
109
- elsif args.empty?
110
- self[sym]
111
- else
112
- super
92
+ def method_missing(name, *args, &code)
93
+ case
94
+ when name =~ /=$/ then self[$`] = args.first
95
+ when args.empty? then self[name]
96
+ else super
113
97
  end
114
98
  end
115
99
  end
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: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve