hashery 2.2.0 → 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 +4 -4
- data/HISTORY.md +40 -0
- data/lib/hashery/dictionary.rb +125 -46
- data/lib/hashery/path_hash.rb +120 -46
- data/lib/hashery/version.rb +1 -1
- data/test/case_dictionary.rb +270 -15
- data/test/case_ini_hash.rb +4 -2
- data/test/case_path_hash.rb +225 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0c7b1dd1acaad0bf94b26375d8b89edf9d7a6a16974c3a119258ef568bb751fc
|
|
4
|
+
data.tar.gz: 96bf33585b82497053d226118733daa5aa975c15bf2de95e618046c13632e8f2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c675066ca2fe40a9164a4ac073eab96799d2f7484abd6e91efaece52de4483076d6e6da6e0b01d4562646ad4bc4e82691452fb8593a576800b807159c1c7022c
|
|
7
|
+
data.tar.gz: e41776c5b3c31e93db0e3f6616a048e8e5b3fb9193e4c917c34fcfd3293a4ceef4cd1e0290314057f9aa7ed851242ad72fb91a6eac1c669b7820ae65b64d22b2
|
data/HISTORY.md
CHANGED
|
@@ -1,5 +1,45 @@
|
|
|
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
|
+
|
|
17
|
+
## 2.2.1 / 2026-11-17
|
|
18
|
+
|
|
19
|
+
This release fixes a number of bugs in Dictionary. Most notably, a
|
|
20
|
+
Dictionary kept in order with `order_by_key` or `order_by_value` could no
|
|
21
|
+
longer be read at all (not even inspected) once it held a key or value
|
|
22
|
+
that didn't compare with the others, such as `nil` (#7). Now sorting by
|
|
23
|
+
such an order raises and leaves the Dictionary as it was, and adding an
|
|
24
|
+
entry the order can't place raises without storing it. LRUHash is
|
|
25
|
+
unchanged.
|
|
26
|
+
|
|
27
|
+
Changes:
|
|
28
|
+
|
|
29
|
+
* Fix Dictionary sort orders that can't sort the entries (#7).
|
|
30
|
+
* Fix Dictionary#dup and #clone, which shared state with the original
|
|
31
|
+
(clone), or dropped the sort order and default (dup).
|
|
32
|
+
* Fix Dictionary#reject!, which returned a filtered copy instead of
|
|
33
|
+
removing entries.
|
|
34
|
+
* Fix Dictionary#shift on an empty Dictionary, and #shift and #pop with
|
|
35
|
+
a `nil` key.
|
|
36
|
+
* Fix Dictionary[hash] and #replace, which shared the given hash or
|
|
37
|
+
order instead of copying it.
|
|
38
|
+
* Fix Dictionary#insert with an existing key or an index past the end.
|
|
39
|
+
* Fix Dictionary#[]= with an index and a `nil` or `false` value.
|
|
40
|
+
* Fix Dictionary#delete removing 1.0 from the order along with 1.
|
|
41
|
+
* Fix Dictionary#== for a sorted Dictionary changed since it was read.
|
|
42
|
+
|
|
3
43
|
## 2.2.0 / 2026-11-16
|
|
4
44
|
|
|
5
45
|
This release concentrates on LRUHash, by far the most used part of the
|
data/lib/hashery/dictionary.rb
CHANGED
|
@@ -128,15 +128,25 @@ module Hashery
|
|
|
128
128
|
def initialize(*args, &blk)
|
|
129
129
|
@order = []
|
|
130
130
|
@order_by = nil
|
|
131
|
+
@block = blk
|
|
131
132
|
if blk
|
|
132
|
-
|
|
133
|
-
oblk = lambda{ |hsh, key| blk[dict,key] } # dictionary rather then just the interal hash.
|
|
134
|
-
@hash = Hash.new(*args, &oblk)
|
|
133
|
+
@hash = Hash.new(*args, &default_proc_for(blk))
|
|
135
134
|
else
|
|
136
135
|
@hash = Hash.new(*args)
|
|
137
136
|
end
|
|
138
137
|
end
|
|
139
138
|
|
|
139
|
+
#
|
|
140
|
+
# Make a copy independent of the original, as #dup and #clone do. It
|
|
141
|
+
# keeps the original's order, sort rule and default.
|
|
142
|
+
#
|
|
143
|
+
def initialize_copy(source)
|
|
144
|
+
super
|
|
145
|
+
@order = source.order.dup
|
|
146
|
+
@hash = @hash.dup
|
|
147
|
+
@hash.default_proc = default_proc_for(@block) if @block
|
|
148
|
+
end
|
|
149
|
+
|
|
140
150
|
#
|
|
141
151
|
# Order of keys.
|
|
142
152
|
#
|
|
@@ -148,16 +158,20 @@ module Hashery
|
|
|
148
158
|
end
|
|
149
159
|
|
|
150
160
|
#
|
|
151
|
-
# Keep dictionary sorted by a specific sort order.
|
|
161
|
+
# Keep dictionary sorted by a specific sort order. Without a block,
|
|
162
|
+
# stop sorting and keep the current order.
|
|
163
|
+
#
|
|
164
|
+
# If the entries can't be sorted this way (a +nil+ key among others,
|
|
165
|
+
# or keys of types that don't compare), it raises and the dictionary
|
|
166
|
+
# keeps its previous order. While a sort order is set, adding an entry
|
|
167
|
+
# it can't place raises too, and nothing is stored.
|
|
152
168
|
#
|
|
153
169
|
# block - Ordering procedure.
|
|
154
170
|
#
|
|
155
171
|
# Returns +self+.
|
|
156
172
|
#
|
|
157
173
|
def order_by( &block )
|
|
158
|
-
|
|
159
|
-
order
|
|
160
|
-
self
|
|
174
|
+
sort_with(block)
|
|
161
175
|
end
|
|
162
176
|
|
|
163
177
|
#
|
|
@@ -179,12 +193,10 @@ module Hashery
|
|
|
179
193
|
#
|
|
180
194
|
def order_by_key
|
|
181
195
|
if block_given?
|
|
182
|
-
|
|
196
|
+
sort_with(Proc.new{ |k,v| yield(k) })
|
|
183
197
|
else
|
|
184
|
-
|
|
198
|
+
sort_with(Proc.new{ |k,v| k })
|
|
185
199
|
end
|
|
186
|
-
order
|
|
187
|
-
self
|
|
188
200
|
end
|
|
189
201
|
|
|
190
202
|
#
|
|
@@ -202,12 +214,10 @@ module Hashery
|
|
|
202
214
|
#
|
|
203
215
|
def order_by_value
|
|
204
216
|
if block_given?
|
|
205
|
-
|
|
217
|
+
sort_with(Proc.new{ |k,v| yield(v) })
|
|
206
218
|
else
|
|
207
|
-
|
|
219
|
+
sort_with(Proc.new{ |k,v| v })
|
|
208
220
|
end
|
|
209
|
-
order
|
|
210
|
-
self
|
|
211
221
|
end
|
|
212
222
|
|
|
213
223
|
#
|
|
@@ -231,7 +241,7 @@ module Hashery
|
|
|
231
241
|
#
|
|
232
242
|
def ==(hsh2)
|
|
233
243
|
if hsh2.is_a?( Dictionary )
|
|
234
|
-
|
|
244
|
+
order == hsh2.order &&
|
|
235
245
|
@hash == hsh2.instance_variable_get("@hash")
|
|
236
246
|
else
|
|
237
247
|
false
|
|
@@ -261,16 +271,18 @@ module Hashery
|
|
|
261
271
|
#
|
|
262
272
|
# h[key,index] = value
|
|
263
273
|
#
|
|
264
|
-
def []=(k, i=nil, v=
|
|
265
|
-
if v
|
|
266
|
-
insert(i,k,v)
|
|
267
|
-
else
|
|
274
|
+
def []=(k, i=nil, v=NO_VALUE)
|
|
275
|
+
if NO_VALUE.equal?(v)
|
|
268
276
|
store(k,i)
|
|
277
|
+
else
|
|
278
|
+
insert(i,k,v)
|
|
269
279
|
end
|
|
270
280
|
end
|
|
271
281
|
|
|
272
282
|
#
|
|
273
|
-
# Insert entry into dictionary at specific index position.
|
|
283
|
+
# Insert entry into dictionary at specific index position. If +key+ is
|
|
284
|
+
# already present, it moves to that position. An index past the end
|
|
285
|
+
# adds the entry at the end.
|
|
274
286
|
#
|
|
275
287
|
# index - [Integer] Position of order placement.
|
|
276
288
|
# key - [Object] Key to associate with value.
|
|
@@ -279,6 +291,9 @@ module Hashery
|
|
|
279
291
|
# Returns `value` stored.
|
|
280
292
|
#
|
|
281
293
|
def insert(index, key, value)
|
|
294
|
+
check_order(key, value)
|
|
295
|
+
remove_from_order(key) if @hash.key?(key)
|
|
296
|
+
index = @order.size if index > @order.size
|
|
282
297
|
@order.insert(index, key)
|
|
283
298
|
@hash.store(key, value)
|
|
284
299
|
end
|
|
@@ -289,6 +304,7 @@ module Hashery
|
|
|
289
304
|
# Returns `value`.
|
|
290
305
|
#
|
|
291
306
|
def store(key, value)
|
|
307
|
+
check_order(key, value)
|
|
292
308
|
@order.push(key) unless @hash.has_key?(key)
|
|
293
309
|
@hash.store(key, value)
|
|
294
310
|
end
|
|
@@ -305,7 +321,7 @@ module Hashery
|
|
|
305
321
|
# Delete the entry with given +key+.
|
|
306
322
|
#
|
|
307
323
|
def delete(key)
|
|
308
|
-
@
|
|
324
|
+
remove_from_order(key) if @hash.key?(key)
|
|
309
325
|
@hash.delete(key)
|
|
310
326
|
end
|
|
311
327
|
|
|
@@ -385,41 +401,50 @@ module Hashery
|
|
|
385
401
|
end
|
|
386
402
|
|
|
387
403
|
#
|
|
388
|
-
#
|
|
389
|
-
#
|
|
390
|
-
# Returns [Hash] of rejected entries.
|
|
404
|
+
# Remove entries for which the block is true.
|
|
391
405
|
#
|
|
392
|
-
#
|
|
406
|
+
# Returns +self+, or +nil+ if nothing was removed.
|
|
393
407
|
#
|
|
394
408
|
def reject!( &block )
|
|
395
|
-
|
|
396
|
-
|
|
409
|
+
before = length
|
|
410
|
+
delete_if(&block)
|
|
411
|
+
length == before ? nil : self
|
|
397
412
|
end
|
|
398
413
|
|
|
399
414
|
#
|
|
400
|
-
# Replace dictionary entries with new table.
|
|
415
|
+
# Replace dictionary entries with new table. The dictionary copies the
|
|
416
|
+
# entries, so later changes to either one don't affect the other. If a
|
|
417
|
+
# sort order is set and can't sort the new entries, it raises and the
|
|
418
|
+
# dictionary keeps its old entries.
|
|
401
419
|
#
|
|
402
420
|
def replace(hsh2)
|
|
421
|
+
old_hash, old_order = @hash, @order
|
|
403
422
|
case hsh2
|
|
404
423
|
when Dictionary
|
|
405
|
-
@order = hsh2.order
|
|
424
|
+
@order = hsh2.order.dup
|
|
406
425
|
@hash = hsh2.to_h
|
|
407
426
|
when Hash
|
|
408
|
-
@hash = hsh2
|
|
427
|
+
@hash = hsh2.dup
|
|
409
428
|
@order = @hash.keys
|
|
410
429
|
else
|
|
411
430
|
@hash = hsh2.to_h
|
|
412
431
|
@order = @hash.keys
|
|
413
432
|
end
|
|
414
433
|
reorder
|
|
434
|
+
rescue Exception
|
|
435
|
+
@hash, @order = old_hash, old_order
|
|
436
|
+
raise
|
|
415
437
|
end
|
|
416
438
|
|
|
417
439
|
#
|
|
418
440
|
# Remove entry from the to top of dictionary.
|
|
419
441
|
#
|
|
442
|
+
# Returns the [key, value] pair, or +nil+ if the dictionary is empty.
|
|
443
|
+
#
|
|
420
444
|
def shift
|
|
445
|
+
return nil if empty?
|
|
421
446
|
key = order.first
|
|
422
|
-
|
|
447
|
+
[key,delete(key)]
|
|
423
448
|
end
|
|
424
449
|
|
|
425
450
|
#
|
|
@@ -427,6 +452,7 @@ module Hashery
|
|
|
427
452
|
#
|
|
428
453
|
def unshift( k,v )
|
|
429
454
|
unless @hash.include?( k )
|
|
455
|
+
check_order( k,v )
|
|
430
456
|
@order.unshift( k )
|
|
431
457
|
@hash.store( k,v )
|
|
432
458
|
true
|
|
@@ -447,6 +473,7 @@ module Hashery
|
|
|
447
473
|
#
|
|
448
474
|
def push(k,v)
|
|
449
475
|
unless @hash.include?( k )
|
|
476
|
+
check_order( k,v )
|
|
450
477
|
@order.push( k )
|
|
451
478
|
@hash.store( k,v )
|
|
452
479
|
true
|
|
@@ -458,9 +485,12 @@ module Hashery
|
|
|
458
485
|
#
|
|
459
486
|
# Pop entry off the bottom of dictionary.
|
|
460
487
|
#
|
|
488
|
+
# Returns the [key, value] pair, or +nil+ if the dictionary is empty.
|
|
489
|
+
#
|
|
461
490
|
def pop
|
|
491
|
+
return nil if empty?
|
|
462
492
|
key = order.last
|
|
463
|
-
|
|
493
|
+
[key,delete(key)]
|
|
464
494
|
end
|
|
465
495
|
|
|
466
496
|
#
|
|
@@ -474,17 +504,6 @@ module Hashery
|
|
|
474
504
|
'{' + ary.join(", ") + '}'
|
|
475
505
|
end
|
|
476
506
|
|
|
477
|
-
#
|
|
478
|
-
# Duplicate dictionary.
|
|
479
|
-
#
|
|
480
|
-
# Returns [Dictionary].
|
|
481
|
-
#
|
|
482
|
-
def dup
|
|
483
|
-
a = []
|
|
484
|
-
each{ |k,v| a << k; a << v }
|
|
485
|
-
self.class[*a]
|
|
486
|
-
end
|
|
487
|
-
|
|
488
507
|
#
|
|
489
508
|
# Update dictionary with other hash.
|
|
490
509
|
#
|
|
@@ -529,12 +548,15 @@ module Hashery
|
|
|
529
548
|
end
|
|
530
549
|
|
|
531
550
|
#
|
|
532
|
-
# Reverse the order of duplicte dictionary.
|
|
551
|
+
# Reverse the order of duplicte dictionary. The copy doesn't keep the
|
|
552
|
+
# sort order, if one is set, since that would undo the reversal.
|
|
533
553
|
#
|
|
534
554
|
# Returns [Dictionary].
|
|
535
555
|
#
|
|
536
556
|
def reverse
|
|
537
|
-
dup
|
|
557
|
+
copy = dup
|
|
558
|
+
copy.order_by
|
|
559
|
+
copy.reverse!
|
|
538
560
|
end
|
|
539
561
|
|
|
540
562
|
#
|
|
@@ -636,6 +658,63 @@ module Hashery
|
|
|
636
658
|
@hash
|
|
637
659
|
end
|
|
638
660
|
|
|
661
|
+
private
|
|
662
|
+
|
|
663
|
+
# Marks the value argument of #[]= as not given, since +nil+ and
|
|
664
|
+
# +false+ are values that can be stored.
|
|
665
|
+
NO_VALUE = Object.new.freeze
|
|
666
|
+
private_constant :NO_VALUE
|
|
667
|
+
|
|
668
|
+
#
|
|
669
|
+
# Default proc for the internal hash, which passes the block this
|
|
670
|
+
# dictionary rather than the internal hash, so automatic key entry
|
|
671
|
+
# goes through the dictionary.
|
|
672
|
+
#
|
|
673
|
+
def default_proc_for(blk)
|
|
674
|
+
dict = self
|
|
675
|
+
lambda{ |hsh, key| blk[dict,key] }
|
|
676
|
+
end
|
|
677
|
+
|
|
678
|
+
#
|
|
679
|
+
# Set the sort rule and sort by it. If sorting fails, put back the
|
|
680
|
+
# previous rule before raising, so the dictionary stays readable.
|
|
681
|
+
#
|
|
682
|
+
def sort_with(rule)
|
|
683
|
+
previous, @order_by = @order_by, rule
|
|
684
|
+
begin
|
|
685
|
+
order
|
|
686
|
+
rescue Exception
|
|
687
|
+
@order_by = previous
|
|
688
|
+
raise
|
|
689
|
+
end
|
|
690
|
+
self
|
|
691
|
+
end
|
|
692
|
+
|
|
693
|
+
#
|
|
694
|
+
# While a sort rule is set, raise before storing an entry the rule
|
|
695
|
+
# can't place among the others, since every read would fail after.
|
|
696
|
+
# Comparing with one other entry is enough to catch a +nil+, or a key
|
|
697
|
+
# of a type that doesn't compare with the rest, without sorting on
|
|
698
|
+
# every write.
|
|
699
|
+
#
|
|
700
|
+
def check_order(key, value)
|
|
701
|
+
return unless @order_by
|
|
702
|
+
i = @order.index{ |k| !k.eql?(key) }
|
|
703
|
+
return unless i
|
|
704
|
+
other = @order[i]
|
|
705
|
+
[[key, value], [other, @hash[other]]].sort_by(&@order_by)
|
|
706
|
+
nil
|
|
707
|
+
end
|
|
708
|
+
|
|
709
|
+
#
|
|
710
|
+
# Remove +key+ from the order, matching it as the hash does (with
|
|
711
|
+
# #eql?), so a key like 1.0 isn't taken for 1.
|
|
712
|
+
#
|
|
713
|
+
def remove_from_order(key)
|
|
714
|
+
i = @order.index{ |k| k.eql?(key) }
|
|
715
|
+
@order.delete_at(i) if i
|
|
716
|
+
end
|
|
717
|
+
|
|
639
718
|
end
|
|
640
719
|
|
|
641
720
|
end
|
data/lib/hashery/path_hash.rb
CHANGED
|
@@ -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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
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
|
-
|
|
80
|
-
if
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
106
|
-
return block.call(key)
|
|
107
|
-
|
|
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
|
|
118
|
-
@flat.
|
|
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,
|
|
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
|
|
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
|
-
#
|
|
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 =
|
|
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
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
|
183
|
-
@
|
|
240
|
+
when PathHash
|
|
241
|
+
@flat == other.flat
|
|
184
242
|
when Hash
|
|
185
|
-
self ==
|
|
243
|
+
self == PathHash.new(other)
|
|
186
244
|
else
|
|
187
|
-
|
|
245
|
+
false
|
|
188
246
|
end
|
|
189
247
|
end
|
|
190
248
|
|
|
191
249
|
private
|
|
192
250
|
|
|
193
|
-
|
|
194
|
-
|
|
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
|
-
|
|
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|
|
data/lib/hashery/version.rb
CHANGED
data/test/case_dictionary.rb
CHANGED
|
@@ -217,30 +217,38 @@ testcase Dictionary do
|
|
|
217
217
|
end
|
|
218
218
|
|
|
219
219
|
method :each_key do
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
220
|
+
test do
|
|
221
|
+
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
|
222
|
+
d.order_by_key
|
|
223
|
+
a = []
|
|
224
|
+
d.each_key{ |k| a << k }
|
|
225
|
+
a.assert == [:a, :b, :c]
|
|
226
|
+
end
|
|
225
227
|
end
|
|
226
228
|
|
|
227
229
|
method :each_value do
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
230
|
+
test do
|
|
231
|
+
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
|
232
|
+
d.order_by_value
|
|
233
|
+
a = []
|
|
234
|
+
d.each_value{ |v| a << v }
|
|
235
|
+
a.assert == [1, 2, 3]
|
|
236
|
+
end
|
|
233
237
|
end
|
|
234
238
|
|
|
235
239
|
method :clear do
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
240
|
+
test do
|
|
241
|
+
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
|
242
|
+
d.clear
|
|
243
|
+
d.to_a.assert == []
|
|
244
|
+
end
|
|
239
245
|
end
|
|
240
246
|
|
|
241
247
|
method :fetch do
|
|
242
|
-
|
|
243
|
-
|
|
248
|
+
test do
|
|
249
|
+
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
|
250
|
+
d.fetch(:a).assert == 1
|
|
251
|
+
end
|
|
244
252
|
end
|
|
245
253
|
|
|
246
254
|
method :key? do
|
|
@@ -322,6 +330,253 @@ testcase Dictionary do
|
|
|
322
330
|
s = d[:foo]
|
|
323
331
|
s.class.assert == Dictionary
|
|
324
332
|
end
|
|
333
|
+
|
|
334
|
+
test "copy fills itself, not the original" do
|
|
335
|
+
d = Dictionary.auto
|
|
336
|
+
d[:a][:b] = 1
|
|
337
|
+
c = d.dup
|
|
338
|
+
c[:x][:y] = 2
|
|
339
|
+
d.keys.assert == [:a]
|
|
340
|
+
c.keys.assert == [:a, :x]
|
|
341
|
+
end
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
# Sorting entries that don't compare (issue #7).
|
|
345
|
+
|
|
346
|
+
method :order_by_key do
|
|
347
|
+
test "with a nil key raises and leaves the dictionary usable" do
|
|
348
|
+
d = Dictionary.new
|
|
349
|
+
d[0] = 1
|
|
350
|
+
d[nil] = 2
|
|
351
|
+
ArgumentError.assert.raised? { d.order_by_key }
|
|
352
|
+
d.keys.assert == [0, nil]
|
|
353
|
+
d.inspect.assert == "{0=>1, nil=>2}"
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
test "with keys of different types raises and leaves the dictionary usable" do
|
|
357
|
+
d = Dictionary[:b, 1, 'a', 2]
|
|
358
|
+
ArgumentError.assert.raised? { d.order_by_key }
|
|
359
|
+
d.keys.assert == [:b, 'a']
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
method :order_by_value do
|
|
364
|
+
test "with a nil value raises and leaves the dictionary usable" do
|
|
365
|
+
d = Dictionary[:a, 2, :b, nil]
|
|
366
|
+
ArgumentError.assert.raised? { d.order_by_value }
|
|
367
|
+
d.values.assert == [2, nil]
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
method :order_by do
|
|
372
|
+
test "keeps the previous order if the new one fails" do
|
|
373
|
+
d = Dictionary[:b, 1, :a, nil]
|
|
374
|
+
d.order_by_key
|
|
375
|
+
ArgumentError.assert.raised? { d.order_by{ |k,v| v } }
|
|
376
|
+
d[:c] = 3
|
|
377
|
+
d.keys.assert == [:a, :b, :c]
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
test "without a block stops sorting" do
|
|
381
|
+
d = Dictionary[:b, 1, :a, 2].order_by_key
|
|
382
|
+
d.order_by
|
|
383
|
+
d[:"0"] = 3
|
|
384
|
+
d.keys.assert == [:a, :b, :"0"]
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
method :store do
|
|
389
|
+
test "refuses a key the sort order can't place" do
|
|
390
|
+
d = Dictionary.new.order_by_key
|
|
391
|
+
d.store(0, 1)
|
|
392
|
+
ArgumentError.assert.raised? { d.store(nil, 2) }
|
|
393
|
+
d.keys.assert == [0]
|
|
394
|
+
d.refute.key?(nil)
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
test "refuses a value the sort order can't place" do
|
|
398
|
+
d = Dictionary.new.order_by_value
|
|
399
|
+
d.store(:a, 1)
|
|
400
|
+
d.store(:b, 2)
|
|
401
|
+
ArgumentError.assert.raised? { d.store(:a, nil) }
|
|
402
|
+
d.to_a.assert == [[:a, 1], [:b, 2]]
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
method :push do
|
|
407
|
+
test "refuses a key the sort order can't place" do
|
|
408
|
+
d = Dictionary[0, 1].order_by_key
|
|
409
|
+
ArgumentError.assert.raised? { d.push(:a, 2) }
|
|
410
|
+
d.keys.assert == [0]
|
|
411
|
+
end
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
method :unshift do
|
|
415
|
+
test "refuses a key the sort order can't place" do
|
|
416
|
+
d = Dictionary[0, 1].order_by_key
|
|
417
|
+
ArgumentError.assert.raised? { d.unshift(:a, 2) }
|
|
418
|
+
d.keys.assert == [0]
|
|
419
|
+
end
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
method :insert do
|
|
423
|
+
test "refuses a key the sort order can't place" do
|
|
424
|
+
d = Dictionary[0, 1].order_by_key
|
|
425
|
+
ArgumentError.assert.raised? { d.insert(0, :a, 2) }
|
|
426
|
+
d.keys.assert == [0]
|
|
427
|
+
end
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# Other fixes.
|
|
431
|
+
|
|
432
|
+
method :insert do
|
|
433
|
+
test "moves an existing key" do
|
|
434
|
+
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
|
435
|
+
d.insert(0, 'c', 9)
|
|
436
|
+
d.keys.assert == ['c', 'a', 'b']
|
|
437
|
+
d['c'].assert == 9
|
|
438
|
+
end
|
|
439
|
+
|
|
440
|
+
test "past the end adds at the end" do
|
|
441
|
+
d = Dictionary['a', 1, 'b', 2]
|
|
442
|
+
d.insert(5, 'c', 3)
|
|
443
|
+
d.keys.assert == ['a', 'b', 'c']
|
|
444
|
+
end
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
method :[]= do
|
|
448
|
+
test "with an index and a nil value" do
|
|
449
|
+
d = Dictionary[:a, 1]
|
|
450
|
+
d[:b, 0] = nil
|
|
451
|
+
d.to_a.assert == [[:b, nil], [:a, 1]]
|
|
452
|
+
end
|
|
453
|
+
end
|
|
454
|
+
|
|
455
|
+
method :delete do
|
|
456
|
+
test "matches keys as the hash does" do
|
|
457
|
+
d = Dictionary[1, :a, 1.0, :b]
|
|
458
|
+
d.delete(1)
|
|
459
|
+
d.keys.assert == [1.0]
|
|
460
|
+
d.length.assert == 1
|
|
461
|
+
end
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
method :shift do
|
|
465
|
+
test "when empty" do
|
|
466
|
+
Dictionary.new.shift.assert == nil
|
|
467
|
+
end
|
|
468
|
+
|
|
469
|
+
test "with a nil key" do
|
|
470
|
+
d = Dictionary[nil, 1, :a, 2]
|
|
471
|
+
d.shift.assert == [nil, 1]
|
|
472
|
+
d.keys.assert == [:a]
|
|
473
|
+
end
|
|
474
|
+
end
|
|
475
|
+
|
|
476
|
+
method :pop do
|
|
477
|
+
test "when empty" do
|
|
478
|
+
Dictionary.new.pop.assert == nil
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
test "with a nil key" do
|
|
482
|
+
d = Dictionary[:a, 1, nil, 2]
|
|
483
|
+
d.pop.assert == [nil, 2]
|
|
484
|
+
d.keys.assert == [:a]
|
|
485
|
+
end
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
method :reject! do
|
|
489
|
+
test "removes entries in place" do
|
|
490
|
+
d = Dictionary[:a, 1, :b, 2]
|
|
491
|
+
d.reject!{ |k,v| k == :a }.assert.equal?(d)
|
|
492
|
+
d.to_a.assert == [[:b, 2]]
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
test "returns nil when nothing is removed" do
|
|
496
|
+
d = Dictionary[:a, 1]
|
|
497
|
+
d.reject!{ false }.assert == nil
|
|
498
|
+
end
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
class_method :[] do
|
|
502
|
+
test "copies a hash argument" do
|
|
503
|
+
h = {:a=>1}
|
|
504
|
+
d = Dictionary[h]
|
|
505
|
+
d[:b] = 2
|
|
506
|
+
h.assert == {:a=>1}
|
|
507
|
+
end
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
method :replace do
|
|
511
|
+
test "copies another dictionary's order" do
|
|
512
|
+
a = Dictionary[:x, 1]
|
|
513
|
+
d = Dictionary.new
|
|
514
|
+
d.replace(a)
|
|
515
|
+
d[:y] = 2
|
|
516
|
+
a.keys.assert == [:x]
|
|
517
|
+
end
|
|
518
|
+
|
|
519
|
+
test "keeps the old entries if the sort order can't place the new ones" do
|
|
520
|
+
d = Dictionary[0, 1].order_by_key
|
|
521
|
+
ArgumentError.assert.raised? { d.replace(:a=>1, 2=>3) }
|
|
522
|
+
d.to_a.assert == [[0, 1]]
|
|
523
|
+
end
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
method :dup do
|
|
527
|
+
test "keeps the sort order" do
|
|
528
|
+
d = Dictionary.new.order_by{ |k,v| -v }
|
|
529
|
+
d[:a] = 1
|
|
530
|
+
d[:b] = 2
|
|
531
|
+
c = d.dup
|
|
532
|
+
c[:c] = 3
|
|
533
|
+
c.keys.assert == [:c, :b, :a]
|
|
534
|
+
end
|
|
535
|
+
|
|
536
|
+
test "keeps the default" do
|
|
537
|
+
Dictionary.new(0).dup[:x].assert == 0
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
test "is independent of the original" do
|
|
541
|
+
d = Dictionary[:a, 1]
|
|
542
|
+
c = d.dup
|
|
543
|
+
c[:b] = 2
|
|
544
|
+
c.delete(:a)
|
|
545
|
+
d.to_a.assert == [[:a, 1]]
|
|
546
|
+
end
|
|
547
|
+
end
|
|
548
|
+
|
|
549
|
+
method :clone do
|
|
550
|
+
test "is independent of the original" do
|
|
551
|
+
d = Dictionary[:a, 1]
|
|
552
|
+
c = d.clone
|
|
553
|
+
c[:b] = 2
|
|
554
|
+
d.keys.assert == [:a]
|
|
555
|
+
end
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
method :merge do
|
|
559
|
+
test "keeps the sort order" do
|
|
560
|
+
d = Dictionary[:b, 1].order_by_key
|
|
561
|
+
d.merge(:a=>2).keys.assert == [:a, :b]
|
|
562
|
+
end
|
|
563
|
+
end
|
|
564
|
+
|
|
565
|
+
method :reverse do
|
|
566
|
+
test "of a sorted dictionary" do
|
|
567
|
+
d = Dictionary[:a, 1, :b, 2].order_by_key
|
|
568
|
+
d.reverse.keys.assert == [:b, :a]
|
|
569
|
+
d.keys.assert == [:a, :b]
|
|
570
|
+
end
|
|
571
|
+
end
|
|
572
|
+
|
|
573
|
+
method :== do
|
|
574
|
+
test "with a sorted dictionary changed since it was last read" do
|
|
575
|
+
d = Dictionary.new.order_by_key
|
|
576
|
+
d[:b] = 1
|
|
577
|
+
d[:a] = 2
|
|
578
|
+
d.assert == Dictionary[:a, 2, :b, 1]
|
|
579
|
+
end
|
|
325
580
|
end
|
|
326
581
|
|
|
327
582
|
end
|
data/test/case_ini_hash.rb
CHANGED
|
@@ -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.
|
|
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
|