config-hash 0.5.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/config-hash.gemspec +1 -1
- data/lib/config-hash.rb +56 -69
- 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: 18cc65d997214d22026cd64bd4a4a648e943d0f5aabe62e25633d583e8c1426c
|
4
|
+
data.tar.gz: 9b8de42e1c7428a97aae9e82338dd0e6dd00c23995d0078ca5be05d6d4d87213
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc9e93f97ad627f5ba663d444c35cf3266e0351d1fb5dedf01fe26912e26d0773368d80f6677f474e69905930f1f6d65fe978579840b2f0dbb20f566ada7157f
|
7
|
+
data.tar.gz: d612503adbdc102de8a4a81e2d60c45dca4ab8f21c997384b65263f0dee9cd7e4894d93f3dcbe9feb2f0ae5c6116c0906e44e962e42391cee364c112a3721245
|
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,61 +1,65 @@
|
|
1
|
-
Encoding.default_external = "UTF-8"
|
2
|
-
|
3
1
|
class Hash
|
4
|
-
def
|
5
|
-
|
6
|
-
end
|
7
|
-
def +@
|
8
|
-
ConfigHash.new(self)
|
9
|
-
end
|
2
|
+
def -@; NormalHash[self]; end
|
3
|
+
def +@; ConfigHash[self]; end
|
10
4
|
end
|
11
5
|
|
12
6
|
class NormalHash < Hash
|
13
7
|
end
|
14
8
|
|
15
9
|
class ConfigHash < Hash
|
16
|
-
SEPARATORS
|
10
|
+
SEPARATORS ||= %r|[./]|
|
17
11
|
|
18
|
-
def self.
|
12
|
+
def self.[](hash=nil)
|
13
|
+
new(hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.load(path="config.rb", list=nil, name: "config")
|
19
17
|
path = File.expand_path(path)
|
20
|
-
eval
|
21
|
-
#{
|
22
|
-
#{IO.read(path, encoding: 'utf-8') if File.
|
23
|
-
#{
|
18
|
+
data = eval <<~"end", binding, path, 0
|
19
|
+
#{name} ||= new
|
20
|
+
#{IO.read(path, encoding: 'utf-8') if File.readable?(path)}
|
21
|
+
#{name}
|
24
22
|
end
|
23
|
+
data.load(*list) if list && !list.empty?
|
24
|
+
data
|
25
25
|
end
|
26
26
|
|
27
27
|
def initialize(hash=nil)
|
28
28
|
super()
|
29
|
-
|
29
|
+
update(hash) if hash
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
32
|
+
def load(*list)
|
33
|
+
[list].each do |root, glob|
|
34
|
+
root = File.expand_path(root)
|
35
|
+
pref = root.size + 1
|
36
|
+
full = File.join([root, glob].compact)
|
37
|
+
list = Dir[full].sort {|a,b| [a.count('/'), a] <=> [b.count('/'), b]}
|
38
|
+
list.each do |path|
|
39
|
+
info = File.dirname(path[pref...] || '')
|
40
|
+
data = ConfigHash.load(path)
|
41
|
+
info == '.' ? update(data) : (self[info] = data)
|
42
|
+
end
|
39
43
|
end
|
40
44
|
self
|
41
45
|
end
|
42
46
|
|
47
|
+
def key?(key)
|
48
|
+
super(key.to_s)
|
49
|
+
end
|
50
|
+
|
43
51
|
def [](key)
|
52
|
+
our = self.class
|
44
53
|
key = key.to_s
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
if
|
49
|
-
|
50
|
-
elsif
|
51
|
-
|
52
|
-
elsif tag == "*" && val.size == 1
|
53
|
-
val = val[val.keys.first]
|
54
|
-
else
|
55
|
-
return super(key)
|
54
|
+
|
55
|
+
if !key?(key) && key =~ SEPARATORS && (ary = key.split SEPARATORS)
|
56
|
+
val = ary.inject(self) do |obj, sub|
|
57
|
+
if not our === obj then return super(key)
|
58
|
+
elsif obj.key?(sub) then obj[sub]
|
59
|
+
elsif sub == "*" then obj[obj.keys.first]
|
60
|
+
else return super(key)
|
56
61
|
end
|
57
62
|
end
|
58
|
-
val
|
59
63
|
else
|
60
64
|
super(key)
|
61
65
|
end
|
@@ -65,55 +69,38 @@ class ConfigHash < Hash
|
|
65
69
|
our = self.class
|
66
70
|
key = key.to_s
|
67
71
|
val = our.new(val) if val.instance_of?(Hash)
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
top = all.inject(self) do |top, tag|
|
74
|
-
if top.key?(tag) && (try = top[tag]).instance_of?(our)
|
75
|
-
top = try
|
76
|
-
else
|
77
|
-
top = top[tag] = our.new
|
78
|
-
end
|
72
|
+
|
73
|
+
if !key?(key) && key =~ SEPARATORS && (ary = key.split SEPARATORS)
|
74
|
+
key = ary.pop
|
75
|
+
obj = ary.inject(self) do |obj, sub|
|
76
|
+
obj.key?(sub) && our === (try = obj[sub]) ? try : (obj[sub] = our.new)
|
79
77
|
end
|
80
|
-
|
78
|
+
obj[key] = val
|
81
79
|
else
|
82
80
|
super(key, val)
|
83
81
|
end
|
84
82
|
end
|
85
83
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
end
|
91
|
-
|
92
|
-
def merge!(other_hash)
|
93
|
-
raise ArgumentError unless Hash === other_hash
|
94
|
-
other_hash.each do |k, v|
|
95
|
-
if block_given? && key?(k)
|
96
|
-
self[k] = yield(k, self[k], v)
|
97
|
-
else
|
98
|
-
self[k] = v
|
99
|
-
end
|
100
|
-
end
|
84
|
+
def update(hash, nuke=false)
|
85
|
+
raise ArgumentError unless Hash === hash
|
86
|
+
clear if nuke
|
87
|
+
hash.each {|key, val| self[key] = val}
|
101
88
|
self
|
102
89
|
end
|
103
90
|
|
104
|
-
|
91
|
+
def update!(hash)
|
92
|
+
update(hash, true)
|
93
|
+
end
|
105
94
|
|
106
95
|
def to_hash
|
107
96
|
Hash[self]
|
108
97
|
end
|
109
98
|
|
110
|
-
def method_missing(
|
111
|
-
|
112
|
-
self[$`] = args.first
|
113
|
-
|
114
|
-
|
115
|
-
else
|
116
|
-
super
|
99
|
+
def method_missing(name, *args, &code)
|
100
|
+
case
|
101
|
+
when name =~ /=$/ then self[$`] = args.first
|
102
|
+
when args.empty? then self[name]
|
103
|
+
else super
|
117
104
|
end
|
118
105
|
end
|
119
106
|
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.0
|
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-22 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: []
|