config-hash 0.6.0 → 1.0.1
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.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/config-hash.gemspec +1 -1
- data/lib/config-hash.rb +56 -62
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fccc52c0630d4228d74d63b73d63762e381b0d0d11700455288ef623f0e28774
|
4
|
+
data.tar.gz: 71dbc0fff1d2d2966e4ca3081635c3ca3e9d6c94c05c1c5436605b2a5b064321
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41cbbb51d5045f488f9590c2a7fc9ef5d15dd520e8bcfd5954b11e46627fa35920ac374bd2954772784975de27ffe9750dc798056e3a8b94b6946761ec6162ec
|
7
|
+
data.tar.gz: ebe99dd0e5af37842514fdc04f9dfcb1b597f259d9b5ed1948f4677e2141ce90cfec1ada91d605118973a1545e1ba4259358d44e54c8926300e350a55ea67b99
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.5
|
1
|
+
2.6.5
|
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,47 +9,60 @@ end
|
|
11
9
|
class ConfigHash < Hash
|
12
10
|
SEPARATORS ||= %r|[./]|
|
13
11
|
|
14
|
-
|
12
|
+
# allow "obj.zip" (for a zip code, etc.)
|
13
|
+
undef_method :zip
|
14
|
+
|
15
|
+
def self.[](hash=nil)
|
16
|
+
new(hash)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.load(path="config.rb", list=nil, name: "config")
|
15
20
|
path = File.expand_path(path)
|
16
|
-
eval
|
17
|
-
#{
|
18
|
-
#{IO.read(path, encoding: 'utf-8') if File.
|
19
|
-
#{
|
21
|
+
data = eval <<~"end", binding, path, 0
|
22
|
+
#{name} ||= new
|
23
|
+
#{IO.read(path, encoding: 'utf-8') if File.readable?(path)}
|
24
|
+
#{name}
|
20
25
|
end
|
26
|
+
data.load(*list) if list && !list.empty?
|
27
|
+
data
|
21
28
|
end
|
22
29
|
|
23
30
|
def initialize(hash=nil)
|
24
31
|
super()
|
25
|
-
|
32
|
+
update(hash) if hash
|
26
33
|
end
|
27
34
|
|
28
|
-
def
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
+
def load(*list)
|
36
|
+
[list].each do |root, glob|
|
37
|
+
root = File.expand_path(root)
|
38
|
+
pref = root.size + 1
|
39
|
+
full = File.join([root, glob].compact)
|
40
|
+
list = Dir[full].sort {|a,b| [a.count('/'), a] <=> [b.count('/'), b]}
|
41
|
+
list.each do |path|
|
42
|
+
info = File.dirname(path[pref...] || '')
|
43
|
+
data = ConfigHash.load(path)
|
44
|
+
info == '.' ? update(data) : (self[info] = data)
|
45
|
+
end
|
35
46
|
end
|
36
47
|
self
|
37
48
|
end
|
38
49
|
|
50
|
+
def key?(key)
|
51
|
+
super(key.to_s)
|
52
|
+
end
|
53
|
+
|
39
54
|
def [](key)
|
55
|
+
our = self.class
|
40
56
|
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)
|
57
|
+
|
58
|
+
if !key?(key) && key =~ SEPARATORS && (ary = key.split SEPARATORS)
|
59
|
+
val = ary.inject(self) do |obj, sub|
|
60
|
+
if not our === obj then return super(key)
|
61
|
+
elsif obj.key?(sub) then obj[sub]
|
62
|
+
elsif sub == "*" then obj[obj.keys.first]
|
63
|
+
else return super(key)
|
52
64
|
end
|
53
65
|
end
|
54
|
-
val
|
55
66
|
else
|
56
67
|
super(key)
|
57
68
|
end
|
@@ -61,55 +72,38 @@ class ConfigHash < Hash
|
|
61
72
|
our = self.class
|
62
73
|
key = key.to_s
|
63
74
|
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
|
75
|
+
|
76
|
+
if !key?(key) && key =~ SEPARATORS && (ary = key.split SEPARATORS)
|
77
|
+
key = ary.pop
|
78
|
+
obj = ary.inject(self) do |obj, sub|
|
79
|
+
obj.key?(sub) && our === (try = obj[sub]) ? try : (obj[sub] = our.new)
|
75
80
|
end
|
76
|
-
|
81
|
+
obj[key] = val
|
77
82
|
else
|
78
83
|
super(key, val)
|
79
84
|
end
|
80
85
|
end
|
81
86
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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
|
87
|
+
def update(hash, nuke=false)
|
88
|
+
raise ArgumentError unless Hash === hash
|
89
|
+
clear if nuke
|
90
|
+
hash.each {|key, val| self[key] = val}
|
97
91
|
self
|
98
92
|
end
|
99
93
|
|
100
|
-
|
94
|
+
def update!(hash)
|
95
|
+
update(hash, true)
|
96
|
+
end
|
101
97
|
|
102
98
|
def to_hash
|
103
99
|
Hash[self]
|
104
100
|
end
|
105
101
|
|
106
|
-
def method_missing(
|
107
|
-
|
108
|
-
self[$`] = args.first
|
109
|
-
|
110
|
-
|
111
|
-
else
|
112
|
-
super
|
102
|
+
def method_missing(name, *args, &code)
|
103
|
+
case
|
104
|
+
when name =~ /=$/ then self[$`] = args.first
|
105
|
+
when args.empty? then self[name]
|
106
|
+
else super
|
113
107
|
end
|
114
108
|
end
|
115
109
|
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: 0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Shreeve
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: This gem makes it easy to work with configuration data.
|
14
14
|
email: steve.shreeve@gmail.com
|
@@ -26,7 +26,7 @@ homepage: https://github.com/shreeve/config-hash
|
|
26
26
|
licenses:
|
27
27
|
- MIT
|
28
28
|
metadata: {}
|
29
|
-
post_install_message:
|
29
|
+
post_install_message:
|
30
30
|
rdoc_options: []
|
31
31
|
require_paths:
|
32
32
|
- lib
|
@@ -41,9 +41,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
41
|
- !ruby/object:Gem::Version
|
42
42
|
version: '0'
|
43
43
|
requirements: []
|
44
|
-
|
45
|
-
|
46
|
-
signing_key:
|
44
|
+
rubygems_version: 3.2.16
|
45
|
+
signing_key:
|
47
46
|
specification_version: 4
|
48
47
|
summary: A safe, homoiconic, Ruby hash supporting dot notation
|
49
48
|
test_files: []
|