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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7f537363101a24d151beb368c314f42bc796eb78ae656b35653ca2499674ed3
4
- data.tar.gz: 8c8ff79e58f1801c8e930beb1edf645b9d7761ca98c9a76b043af37a3e12ba65
3
+ metadata.gz: 18cc65d997214d22026cd64bd4a4a648e943d0f5aabe62e25633d583e8c1426c
4
+ data.tar.gz: 9b8de42e1c7428a97aae9e82338dd0e6dd00c23995d0078ca5be05d6d4d87213
5
5
  SHA512:
6
- metadata.gz: 326d2911fe5917de0b68c5041e82cd70a80deb2b4bff3c506185d19075105129039a8125e7fa7089068c374b56061df508697877f805a62d501f5011f7a9c7aa
7
- data.tar.gz: a43d76d8d41c52e002328d3484a8b77aa48eac43e3068f3eaaf967c66ab2d164917e617bf069fafdf25531f0c60560572430b48617c8e9e99f81dfa162f0a3d7
6
+ metadata.gz: fc9e93f97ad627f5ba663d444c35cf3266e0351d1fb5dedf01fe26912e26d0773368d80f6677f474e69905930f1f6d65fe978579840b2f0dbb20f566ada7157f
7
+ data.tar.gz: d612503adbdc102de8a4a81e2d60c45dca4ab8f21c997384b65263f0dee9cd7e4894d93f3dcbe9feb2f0ae5c6116c0906e44e962e42391cee364c112a3721245
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.5.0
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.5.0"
5
+ s.version = "1.0.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,61 +1,65 @@
1
- Encoding.default_external = "UTF-8"
2
-
3
1
  class Hash
4
- def -@
5
- NormalHash.new.merge!(self)
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 = %r|[./]|
10
+ SEPARATORS ||= %r|[./]|
17
11
 
18
- def self.load(path="config.rb", var="config")
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 <<-"end", binding, path, 0
21
- #{var} ||= new
22
- #{IO.read(path, encoding: 'utf-8') if File.exists?(path)}
23
- #{var}
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
- merge!(hash) if hash
29
+ update(hash) if hash
30
30
  end
31
31
 
32
- def import(root, glob)
33
- root = File.expand_path(root)
34
- pref = root.size + 1
35
- Dir[File.join(root, glob)].each do |path|
36
- keys = File.dirname(path[pref..-1])
37
- data = ConfigHash.load(path)
38
- self[keys] = data
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
- if !key?(key) && key =~ SEPARATORS
46
- val = self
47
- key.split(SEPARATORS).each do |tag|
48
- if !val.instance_of?(self.class)
49
- return super(key)
50
- elsif val.key?(tag)
51
- val = val[tag]
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
- if val.instance_of?(NormalHash)
69
- super(key, val)
70
- elsif key =~ SEPARATORS
71
- all = key.split(SEPARATORS)
72
- key = all.pop
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
- top[key] = val
78
+ obj[key] = val
81
79
  else
82
80
  super(key, val)
83
81
  end
84
82
  end
85
83
 
86
- alias store []=
87
-
88
- def key?(key)
89
- super(key.to_s)
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
- alias update merge!
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(sym, *args, &block)
111
- if sym =~ /=$/
112
- self[$`] = args.first
113
- elsif args.empty? # or... key?(sym)
114
- self[sym]
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.5.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: 2018-05-25 00:00:00.000000000 Z
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
- 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: []