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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d7d77612387f4c98acb30d2174f842e9bc686cee148dd35971a50b1af1ffc36
4
- data.tar.gz: 5be88d29ba90fb1f0076fb690969e642c8bfc5d7faa98f3235c592f059f7397d
3
+ metadata.gz: fccc52c0630d4228d74d63b73d63762e381b0d0d11700455288ef623f0e28774
4
+ data.tar.gz: 71dbc0fff1d2d2966e4ca3081635c3ca3e9d6c94c05c1c5436605b2a5b064321
5
5
  SHA512:
6
- metadata.gz: 945fb4e4c3fcadc7dc433f36a512ed26ef06bfc17349ea14dae2e6f5bfc063c9821649739fe9bdb85c4ea2f48e8a3ec8bd60ab52fcc79c8175dde38c89c37509
7
- data.tar.gz: 35a042c344fa0e43902ed11d7b36111af477bd37eeab5e155b35b1360e85f3fe7898df22a84da19e56415b5e9de262f02972eac1d39a3d657d9677555a4f827b
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
@@ -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 = "1.0.1"
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,47 +9,60 @@ end
11
9
  class ConfigHash < Hash
12
10
  SEPARATORS ||= %r|[./]|
13
11
 
14
- def self.load(path="config.rb", var="config")
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 <<-"end", binding, path, 0
17
- #{var} ||= new
18
- #{IO.read(path, encoding: 'utf-8') if File.exists?(path)}
19
- #{var}
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
- merge!(hash) if hash
32
+ update(hash) if hash
26
33
  end
27
34
 
28
- def import(root, glob)
29
- root = File.expand_path(root)
30
- pref = root.size + 1
31
- Dir[File.join(root, glob)].each do |path|
32
- keys = File.dirname(path[pref..-1])
33
- data = ConfigHash.load(path)
34
- self[keys] = data
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
- 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)
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
- 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
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
- top[key] = val
81
+ obj[key] = val
77
82
  else
78
83
  super(key, val)
79
84
  end
80
85
  end
81
86
 
82
- alias store []=
83
-
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
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
- alias update merge!
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(sym, *args, &block)
107
- if sym =~ /=$/
108
- self[$`] = args.first
109
- elsif args.empty?
110
- self[sym]
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.6.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: 2018-05-30 00:00:00.000000000 Z
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
- rubyforge_project:
45
- rubygems_version: 2.7.6
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: []