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 +4 -4
- data/config-hash.gemspec +1 -1
- data/lib/config-hash.rb +33 -49
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f34e652d639c677341b14b1ae82c77eb72aa1e2618f5de14c0853089eb12fb7e
|
4
|
+
data.tar.gz: f428443a9f4d33fad40f29352ea2b8da536dfacfe4e8671eeb170b9b175cda0e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8e828acf1e07a92a1e03e40e9b1fe8eb4e81eacfa6f1fac8de58606d899b86ece8ac01d4160d80a4f36d249b7c5b3397928e1ec7c03afb2fb78082f825db83e
|
7
|
+
data.tar.gz: aef9036c64d8baa3b6a18a0748aa39d039b31b2b8cce4f919b8de1166a9e48dce8e19f1c8f6ef52cebc90eef19b06faad3030e37503421149c02bc703fb7d7d6
|
data/config-hash.gemspec
CHANGED
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
|
-
|
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
|
-
|
42
|
-
|
43
|
-
|
44
|
-
if
|
45
|
-
|
46
|
-
elsif
|
47
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
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
|
-
|
72
|
+
obj[key] = val
|
77
73
|
else
|
78
74
|
super(key, val)
|
79
75
|
end
|
80
76
|
end
|
81
77
|
|
82
|
-
|
78
|
+
alias_method :store, :[]=
|
83
79
|
|
84
|
-
def
|
85
|
-
|
86
|
-
|
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
|
-
|
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(
|
107
|
-
|
108
|
-
self[$`] = args.first
|
109
|
-
|
110
|
-
|
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
|