hashery 2.2.1 → 2.2.2

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: 8caa9de54771dfc677bf6210b91ca3e9c4df285d0d802d4add8aefd52e82310b
4
- data.tar.gz: 125886b904e00ca54165212bd3336ec5a8e2507d9c6ede8f4a528eaac83317d8
3
+ metadata.gz: 0c7b1dd1acaad0bf94b26375d8b89edf9d7a6a16974c3a119258ef568bb751fc
4
+ data.tar.gz: 96bf33585b82497053d226118733daa5aa975c15bf2de95e618046c13632e8f2
5
5
  SHA512:
6
- metadata.gz: acbece4eae105e972288fd55cd5f11c2f7169e62d6c801efae73d345ce65a639bac5ffcccedad27d454d8808faa5a510f08fb795f200deb5ec8138976b296800
7
- data.tar.gz: cc310523d225dd004b4fb597c1798c4f02d4cd9f7e9c23471c34b3cbca2c33a853dfa1211cfce4f2f089a49bd5585f519193f03163e4d3f93427604bd3d45f07
6
+ metadata.gz: c675066ca2fe40a9164a4ac073eab96799d2f7484abd6e91efaece52de4483076d6e6da6e0b01d4562646ad4bc4e82691452fb8593a576800b807159c1c7022c
7
+ data.tar.gz: e41776c5b3c31e93db0e3f6616a048e8e5b3fb9193e4c917c34fcfd3293a4ceef4cd1e0290314057f9aa7ed851242ad72fb91a6eac1c669b7820ae65b64d22b2
data/HISTORY.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # RELEASE HISTORY
2
2
 
3
+ ## 2.2.2 / 2026-11-17
4
+
5
+ This release fixes PathHash, which had been broken for years: reading any
6
+ key raised NameError. PathHash also has tests of its own now. LRUHash is
7
+ unchanged.
8
+
9
+ Changes:
10
+
11
+ * Fix PathHash. `[]`, `==`, `expand`, `ordered!` and `index` raised
12
+ errors; `size`, `each`, `key?`, `values` and `to_a` saw no entries;
13
+ copies shared entries with the original; a key also matched longer keys
14
+ that started with it (`a` matched `ab`); `fetch` ignored its block; and
15
+ `delete` returned an empty PathHash instead of nil for a missing key.
16
+
3
17
  ## 2.2.1 / 2026-11-17
4
18
 
5
19
  This release fixes a number of bugs in Dictionary. Most notably, a
@@ -23,6 +23,13 @@ module Hashery
23
23
  #
24
24
  class PathHash < Hash
25
25
 
26
+ #
27
+ # Create a PathHash from a Hash, or from key-value pairs as Hash[] takes.
28
+ #
29
+ def self.[](*args)
30
+ new(Hash[*args])
31
+ end
32
+
26
33
  #
27
34
  # Initialize PathHash.
28
35
  #
@@ -30,10 +37,19 @@ module Hashery
30
37
  #
31
38
  def initialize(hsh={})
32
39
  raise ArgumentError, "must be a hash or array of slashed values" unless hsh.is_a?(Hash) || hsh.is_a?(Array)
40
+ hsh = hsh.expand if hsh.is_a?(PathHash)
33
41
  @constructor = hsh.is_a?(Hash) ? hsh.class : Hash
34
42
  @flat = flatten_to_hash(hsh)
35
43
  end
36
44
 
45
+ #
46
+ # Copies get their own table, so changing one doesn't change the other.
47
+ #
48
+ def initialize_copy(source)
49
+ super
50
+ @flat = @flat.dup
51
+ end
52
+
37
53
  # Standard Hash methods, plus the overwritten ones
38
54
  #include StandardHashMethodsInRuby
39
55
 
@@ -44,21 +60,18 @@ module Hashery
44
60
  # hash['c/d']
45
61
  #
46
62
  def [](key)
47
- rg = Regexp.new("^#{key}/?")
48
- start_obj = if @constructor == OrderedHash
49
- @constructor.new((@flat.instance_variable_get(:@keys_in_order) || []).collect {|e| e.gsub(rg,'')})
50
- else
51
- @constructor.new
52
- end
53
- v = @flat.has_key?(key) ? @flat[key] : self.class.new(@flat.reject {|k,v| !(k == key || k =~ rg)}.inject(start_obj) {|h,(k,v)| h[k.gsub(rg,'')] = v; h})
54
- v.is_a?(self.class) && v.empty? ? nil : v
63
+ key = key.to_s
64
+ return @flat[key] if @flat.has_key?(key)
65
+ sub = subtree(key)
66
+ sub.empty? ? nil : self.class.new(sub)
55
67
  end
56
68
 
57
69
  #
58
70
  # Same as above, except sets value rather than retrieving it.
59
71
  #
60
72
  def []=(key,value)
61
- @flat.reject! {|k,v| k == key || k =~ Regexp.new("^#{key}/")}
73
+ key = key.to_s
74
+ @flat.reject! {|k,v| under?(k, key)}
62
75
  if value.is_a?(Hash)
63
76
  flatten_to_hash(value).each do |hk,hv|
64
77
  @flat[key.to_s+'/'+hk.to_s] = hv
@@ -70,27 +83,20 @@ module Hashery
70
83
 
71
84
  def clear # :nodoc:
72
85
  @flat.clear
86
+ self
73
87
  end
74
88
 
75
89
  #
76
- #
90
+ # Like Hash#fetch, but slashed keys can be used here, too.
77
91
  #
78
92
  def fetch(key,default=:ehisehoah0928309q98y30,&block) # :nodoc:
79
- value = @flat.has_key?(key) ? @flat[key] : self.class.new(@flat.reject {|k,v| !(k == key || k =~ Regexp.new("^#{key}/"))}.inject({}) {|h,(k,v)| h[k.split('/',2)[1]] = v; h})
80
- if value.is_a?(self.class) && value.empty?
81
- if default == :ehisehoah0928309q98y30
82
- if block_given?
83
- block.call(key)
84
- else
85
- raise IndexError
86
- end
87
- value
88
- else
89
- default
90
- end
91
- else
92
- value
93
- end
93
+ key = key.to_s
94
+ return @flat[key] if @flat.has_key?(key)
95
+ sub = subtree(key)
96
+ return self.class.new(sub) unless sub.empty?
97
+ return block.call(key) if block
98
+ return default unless default == :ehisehoah0928309q98y30
99
+ raise KeyError, "key not found: #{key.inspect}"
94
100
  end
95
101
 
96
102
  #
@@ -102,9 +108,10 @@ module Hashery
102
108
  # Returns delete value.
103
109
  #
104
110
  def delete(key,&block)
105
- value = @flat.has_key?(key) ? @flat[key] : self.class.new(@flat.reject {|k,v| !(k == key || k =~ Regexp.new("^#{key}/"))}.inject({}) {|h,(k,v)| h[k.split('/',2)[1]] = v; h})
106
- return block.call(key) if value.is_a?(self.class) && value.empty? && block_given?
107
- @flat.keys.reject {|k| !(k == key || k =~ Regexp.new("^#{key}/"))}.each {|k| @flat.delete(k)}
111
+ key = key.to_s
112
+ return block ? block.call(key) : nil unless key?(key)
113
+ value = self[key]
114
+ @flat.delete_if {|k,v| under?(k, key)}
108
115
  return value
109
116
  end
110
117
 
@@ -113,9 +120,51 @@ module Hashery
113
120
  @flat.empty?
114
121
  end
115
122
 
123
+ #
124
+ # Is there a value at this key, or below it? Slashed keys can be used.
125
+ #
126
+ def key?(key)
127
+ key = key.to_s
128
+ @flat.any? {|k,v| under?(k, key)}
129
+ end
130
+ alias_method :has_key?, :key?
131
+ alias_method :include?, :key?
132
+ alias_method :member?, :key?
133
+
116
134
  # This gives you the slashed key of the value, no matter where the value is in the tree.
117
- def index(value)
118
- @flat.index(value)
135
+ def key(value)
136
+ @flat.key(value)
137
+ end
138
+ alias_method :index, :key
139
+
140
+ #
141
+ # Iterate over the top-level keys and their values, as #[] gives them.
142
+ #
143
+ def each
144
+ return to_enum(:each) { size } unless block_given?
145
+ keys.each {|k| yield k, self[k]}
146
+ self
147
+ end
148
+ alias_method :each_pair, :each
149
+
150
+ #
151
+ # The number of top-level keys.
152
+ #
153
+ def size
154
+ keys.size
155
+ end
156
+ alias_method :length, :size
157
+
158
+ #
159
+ # The values of the top-level keys, as #[] gives them.
160
+ #
161
+ def values
162
+ keys.map {|k| self[k]}
163
+ end
164
+
165
+ #
166
+ def to_a
167
+ keys.map {|k| [k, self[k]]}
119
168
  end
120
169
 
121
170
  #
@@ -132,6 +181,7 @@ module Hashery
132
181
  # end up with an array holding several hashes.
133
182
  def rehash # :nodoc:
134
183
  @flat.rehash
184
+ self
135
185
  end
136
186
 
137
187
  # Gives a list of all keys in all levels in the multi-level hash, joined by slashes.
@@ -145,8 +195,9 @@ module Hashery
145
195
 
146
196
  # Expands the whole hash to Hash objects ... not useful very often, it seems.
147
197
  def expand
148
- inject({}) {|h,(k,v)| h[k] = v.is_a?(SlashedHash) ? v.expand : v; h}
198
+ keys.inject({}) {|h,k| v = self[k]; h[k] = v.is_a?(PathHash) ? v.expand : v; h}
149
199
  end
200
+ alias_method :to_h, :expand
150
201
 
151
202
  def to_string_array
152
203
  flatten_to_array(flat,[])
@@ -156,45 +207,68 @@ module Hashery
156
207
  self
157
208
  end
158
209
 
159
- # Same as ordered! but returns a new SlashedHash object instead of modifying the same.
210
+ # Same as ordered! but returns a new PathHash object instead of modifying the same.
160
211
  def ordered(*keys_in_order)
161
212
  dup.ordered!(*keys_in_order)
162
213
  end
163
214
 
164
- # Sets the SlashedArray as ordered. The *keys_in_order must be a flat array
165
- # of slashed keys that specify the order for each level:
215
+ # Puts the entries in the given order. The *keys_in_order must be a flat
216
+ # array of slashed keys that specify the order for each level; entries
217
+ # not named keep their order, after those that are:
166
218
  #
167
- # s = {'a'=>{'b'=>'c', 'c'=>'d'}, 'b'=>'c'}.slashed
219
+ # s = PathHash['a'=>{'b'=>'c', 'c'=>'d'}, 'b'=>'c']
168
220
  # s.ordered!('b', 'a/c', 'a/b')
169
221
  # s.expand # => {'b'=>'c', 'a'=>{'c'=>'d', 'b'=>'c'}}
170
- # # Note that the expanded hashes will *still* be ordered!
171
222
  #
172
223
  def ordered!(*keys_in_order)
173
- return self if @constructor == OrderedHash
174
- @constructor = OrderedHash
175
- @flat = @flat.ordered(*keys_in_order)
224
+ ordered = @flat.class.new
225
+ keys_in_order.each do |key|
226
+ key = key.to_s
227
+ @flat.each {|k,v| ordered[k] = v if under?(k, key) && !ordered.has_key?(k)}
228
+ end
229
+ @flat.each {|k,v| ordered[k] = v unless ordered.has_key?(k)}
230
+ @flat = ordered
176
231
  self
177
232
  end
178
233
 
234
+ #
235
+ # A PathHash equals another with the same entries, or a Hash that would
236
+ # make one.
179
237
  #
180
238
  def ==(other)
181
239
  case other
182
- when SlashedHash
183
- @slashed == other.instance_variable_get(:@slashed)
240
+ when PathHash
241
+ @flat == other.flat
184
242
  when Hash
185
- self == SlashedHash.new(other)
243
+ self == PathHash.new(other)
186
244
  else
187
- raise TypeError, "Cannot compare #{other.class.name} with SlashedHash"
245
+ false
188
246
  end
189
247
  end
190
248
 
191
249
  private
192
250
 
193
- def flatten_to_hash(hsh)
194
- flat = @constructor.new
251
+ # Is the flat key +k+ the key +key+, or below it?
252
+ def under?(k, key)
253
+ k == key || k.start_with?(key + '/')
254
+ end
255
+
256
+ # The entries below +key+, with +key+ taken off the front of their keys.
257
+ def subtree(key)
258
+ prefix = key + '/'
259
+ @flat.each_with_object(@constructor.new) do |(k,v),h|
260
+ h[k[prefix.size..-1]] = v if k.start_with?(prefix)
261
+ end
262
+ end
263
+
264
+ def flatten_to_hash(hsh, flat=@constructor.new)
195
265
  if hsh.is_a?(Array)
196
266
  hsh.each do |e|
197
- flat.merge!(flatten_to_hash(e))
267
+ if e.is_a?(Hash) || e.is_a?(Array)
268
+ flat.merge!(flatten_to_hash(e))
269
+ else
270
+ flatten_to_hash(e, flat)
271
+ end
198
272
  end
199
273
  elsif hsh.is_a?(Hash)
200
274
  hsh.each do |k,v|
@@ -1,3 +1,3 @@
1
1
  module Hashery
2
- VERSION = "2.2.1"
2
+ VERSION = "2.2.2"
3
3
  end
@@ -0,0 +1,225 @@
1
+ require 'helper'
2
+ require 'hashery/path_hash'
3
+
4
+ test_case PathHash do
5
+
6
+ class_method :[] do
7
+ test "makes a PathHash from a Hash" do
8
+ s = PathHash['a' => 'b', 'c' => {'d' => :e}]
9
+ s.flat.assert == {'a' => 'b', 'c/d' => :e}
10
+ end
11
+ end
12
+
13
+ class_method :new do
14
+ test "flattens nested hashes" do
15
+ s = PathHash.new('a' => 'b', 'c' => {'d' => :e})
16
+ s.flat.assert == {'a' => 'b', 'c/d' => :e}
17
+ end
18
+
19
+ test "collects slashed values from an array" do
20
+ s = PathHash.new(['a/b', 'a/c'])
21
+ s.flat.assert == {'a' => ['b', 'c']}
22
+ end
23
+
24
+ test "copies another PathHash" do
25
+ s = PathHash.new('c' => {'d' => :e})
26
+ PathHash.new(s).flat.assert == {'c/d' => :e}
27
+ end
28
+ end
29
+
30
+ method :[] do
31
+ test "reads top-level and slashed keys" do
32
+ s = PathHash['a' => 'b', 'c' => {'d' => :e}]
33
+ s['a'].assert == 'b'
34
+ s['c/d'].assert == :e
35
+ s['c']['d'].assert == :e
36
+ s['c'].assert.is_a?(PathHash)
37
+ end
38
+
39
+ test "returns nil for a missing key" do
40
+ PathHash['a' => 1]['x'].assert == nil
41
+ end
42
+
43
+ test "doesn't match keys that only start the same" do
44
+ s = PathHash['ab' => 1, 'a' => {'b' => 2}]
45
+ s['a'].flat.assert == {'b' => 2}
46
+ end
47
+
48
+ test "takes keys literally, not as patterns" do
49
+ s = PathHash['a.b' => 1, 'axb' => {'c' => 2}]
50
+ s['a.b'].assert == 1
51
+ end
52
+
53
+ test "converts keys to strings" do
54
+ PathHash['a' => 1][:a].assert == 1
55
+ end
56
+ end
57
+
58
+ method :[]= do
59
+ test "stores with a slashed key" do
60
+ s = PathHash.new
61
+ s['a/b'] = 1
62
+ s['a']['b'].assert == 1
63
+ end
64
+
65
+ test "flattens a Hash value" do
66
+ s = PathHash.new
67
+ s['a'] = {'b' => 1}
68
+ s.flat.assert == {'a/b' => 1}
69
+ end
70
+
71
+ test "replaces what was below the key" do
72
+ s = PathHash['a' => {'b' => 1}]
73
+ s['a'] = 2
74
+ s.flat.assert == {'a' => 2}
75
+ end
76
+ end
77
+
78
+ method :fetch do
79
+ test "reads slashed keys" do
80
+ s = PathHash['c' => {'d' => {'e' => 1}}]
81
+ s.fetch('c/d/e').assert == 1
82
+ s.fetch('c/d').flat.assert == {'e' => 1}
83
+ end
84
+
85
+ test "returns the default or the block's value for a missing key" do
86
+ s = PathHash['a' => 1]
87
+ s.fetch('x', 0).assert == 0
88
+ s.fetch('x'){ |k| k * 2 }.assert == 'xx'
89
+ end
90
+
91
+ test "raises KeyError for a missing key" do
92
+ KeyError.assert.raised? do
93
+ PathHash['a' => 1].fetch('x')
94
+ end
95
+ end
96
+ end
97
+
98
+ method :delete do
99
+ test "removes a key and what is below it" do
100
+ s = PathHash['a' => 1, 'c' => {'d' => 2}]
101
+ s.delete('c').flat.assert == {'d' => 2}
102
+ s.flat.assert == {'a' => 1}
103
+ end
104
+
105
+ test "returns nil, or the block's value, for a missing key" do
106
+ s = PathHash['a' => 1]
107
+ s.delete('x').assert == nil
108
+ s.delete('x'){ |k| "no #{k}" }.assert == 'no x'
109
+ end
110
+ end
111
+
112
+ method :key? do
113
+ test "finds top-level and slashed keys" do
114
+ s = PathHash['a' => 1, 'c' => {'d' => 2}]
115
+ s.assert.key?('a')
116
+ s.assert.key?('c')
117
+ s.assert.key?('c/d')
118
+ s.refute.key?('x')
119
+ s.refute.key?('c/x')
120
+ end
121
+ end
122
+
123
+ method :key do
124
+ test "gives the slashed key of a value" do
125
+ PathHash['c' => {'d' => :e}].key(:e).assert == 'c/d'
126
+ end
127
+ end
128
+
129
+ method :index do
130
+ test "is the same as key" do
131
+ PathHash['c' => {'d' => :e}].index(:e).assert == 'c/d'
132
+ end
133
+ end
134
+
135
+ method :keys do
136
+ test "lists the top-level keys" do
137
+ PathHash['a' => 1, 'c' => {'d' => 2, 'e' => 3}].keys.assert == ['a', 'c']
138
+ end
139
+ end
140
+
141
+ method :size do
142
+ test "counts the top-level keys" do
143
+ PathHash['a' => 1, 'c' => {'d' => 2, 'e' => 3}].size.assert == 2
144
+ end
145
+ end
146
+
147
+ method :each do
148
+ test "yields the top-level keys and their values" do
149
+ s = PathHash['a' => 1, 'c' => {'d' => 2}]
150
+ pairs = []
151
+ s.each{ |k, v| pairs << [k, v] }
152
+ pairs.map(&:first).assert == ['a', 'c']
153
+ pairs[1][1].flat.assert == {'d' => 2}
154
+ end
155
+
156
+ test "returns an Enumerator without a block" do
157
+ PathHash['a' => 1].each.to_a.assert == [['a', 1]]
158
+ end
159
+ end
160
+
161
+ method :expand do
162
+ test "gives back nested hashes" do
163
+ h = {'a' => 'b', 'c' => {'d' => :e}}
164
+ PathHash.new(h).expand.assert == h
165
+ end
166
+ end
167
+
168
+ method :to_string_array do
169
+ test "lists each value with its slashed key" do
170
+ PathHash['a' => 'b', 'c' => {'d' => :e}].to_string_array.assert == ['a/b', 'c/d/e']
171
+ end
172
+ end
173
+
174
+ method :ordered! do
175
+ test "puts the entries in the given order" do
176
+ s = PathHash['a' => {'b' => 'c', 'c' => 'd'}, 'b' => 'c']
177
+ s.ordered!('b', 'a/c', 'a/b')
178
+ s.expand.to_a.assert == [['b', 'c'], ['a', {'c' => 'd', 'b' => 'c'}]]
179
+ s.expand['a'].keys.assert == ['c', 'b']
180
+ end
181
+ end
182
+
183
+ method :ordered do
184
+ test "orders a copy" do
185
+ s = PathHash['a' => 1, 'b' => 2]
186
+ s.ordered('b').keys.assert == ['b', 'a']
187
+ s.keys.assert == ['a', 'b']
188
+ end
189
+ end
190
+
191
+ method :== do
192
+ test "compares entries" do
193
+ s = PathHash['a' => 1, 'c' => {'d' => 2}]
194
+ s.assert == PathHash['a' => 1, 'c' => {'d' => 2}]
195
+ s.refute == PathHash['a' => 1]
196
+ end
197
+
198
+ test "compares with a Hash" do
199
+ s = PathHash['a' => 1, 'c' => {'d' => 2}]
200
+ s.assert == {'a' => 1, 'c' => {'d' => 2}}
201
+ end
202
+
203
+ test "is false for anything else" do
204
+ PathHash['a' => 1].refute == 1
205
+ end
206
+ end
207
+
208
+ method :dup do
209
+ test "makes an independent copy" do
210
+ s = PathHash['a' => 1]
211
+ d = s.dup
212
+ d['a'] = 2
213
+ s['a'].assert == 1
214
+ end
215
+ end
216
+
217
+ method :clear do
218
+ test "removes everything" do
219
+ s = PathHash['a' => 1]
220
+ s.clear.assert.equal?(s)
221
+ s.assert.empty?
222
+ end
223
+ end
224
+
225
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashery
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trans
@@ -89,6 +89,7 @@ files:
89
89
  - test/case_lru_hash.rb
90
90
  - test/case_open_cascade.rb
91
91
  - test/case_open_hash.rb
92
+ - test/case_path_hash.rb
92
93
  - test/case_property_hash.rb
93
94
  - test/case_query_hash.rb
94
95
  - test/fixture/example.ini