cicphash 1.1.0 → 2.0.0
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 +7 -0
- data/CHANGELOG +15 -0
- data/MIT-LICENSE +18 -0
- data/README.rdoc +44 -0
- data/cicphash.rb +114 -31
- data/test/test_cicphash.rb +240 -41
- metadata +31 -15
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f4fe93657198f00e131fef0523102674ee6e5be6d2befb4be55806402a5c04a0
|
4
|
+
data.tar.gz: bda72a57adfe99b16e557ac338d8e328eb7fd53364cdf6fb1fcc41cfbb34ddcb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b664f529e3e895191ea7499dad7d1957bf72551609aaedd0621a3f168fe9456b70d0d01b454c776a1a8c4c957a427bfa67ba47a0d662d8c89ae52995b4e63013
|
7
|
+
data.tar.gz: 101a4dc1918bbdbb3c16c6aa5554a1d9455bc653662af077f0318e7e8a01c1eb3ef73cb11746321494a1af4378706b54bb2c527ed381e85b3ee68c1ef864f590
|
data/CHANGELOG
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
=== 2.0.0 (2020-12-10)
|
2
|
+
|
3
|
+
* Support new Hash instance methods added in 1.9 - 3.0 (jeremyevans)
|
4
|
+
|
5
|
+
* Make sure CICPHash has the same interface as Hash in all Ruby versions (jeremyevans)
|
6
|
+
|
7
|
+
=== 1.1.0 (2012-09-04)
|
8
|
+
|
9
|
+
* Support new Hash instance methods added in 1.9 (jeremyevans)
|
10
|
+
|
11
|
+
* Fix dup and clone to return copies (jeremyevans)
|
12
|
+
|
13
|
+
=== 1.0.0 (2007-08-13)
|
14
|
+
|
15
|
+
* Initial release (jeremyevans)
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2007-2021 Jeremy Evans
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to
|
5
|
+
deal in the Software without restriction, including without limitation the
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
16
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
= CICPHash
|
2
|
+
|
3
|
+
ruby-cicphash is a library that adds the CICPHash class, which has the
|
4
|
+
same interface as Hash, but is case insensitive and case preserving.
|
5
|
+
Any value can be used as a key. However, you cannot have two keys in
|
6
|
+
the same CICPHash that would be the same if when converted to strings
|
7
|
+
would be equal or differing only in case.
|
8
|
+
|
9
|
+
For example, all of the following keys would be considered equalivalent:
|
10
|
+
'ab', 'Ab', 'AB', 'aB', :ab, :Ab, :AB, :aB
|
11
|
+
|
12
|
+
CICPHash uses a last match wins policy. If an key-value pair is added to
|
13
|
+
a CICPHash and a case insensitive variant of the key is already in the hash
|
14
|
+
the instance of the key in the hash becomes the same the most recently added
|
15
|
+
key (and the value is updated to reflect the new value).
|
16
|
+
|
17
|
+
Example:
|
18
|
+
|
19
|
+
require 'cicphash'
|
20
|
+
hash = CICPHash.new
|
21
|
+
hash[:AB] = 1
|
22
|
+
hash['ab'] # => 1
|
23
|
+
hash['ab'] = 2
|
24
|
+
hash # => {"ab"=>2}
|
25
|
+
|
26
|
+
= Installation
|
27
|
+
|
28
|
+
gem install cicphash
|
29
|
+
|
30
|
+
= Source Code
|
31
|
+
|
32
|
+
Source code is available on GitHub at https://github.com/jeremyevans/ruby-cicphash
|
33
|
+
|
34
|
+
= Reporting Bugs
|
35
|
+
|
36
|
+
The bug tracker is located at https://github.com/jeremyevans/ruby-cicphash/issues
|
37
|
+
|
38
|
+
= License
|
39
|
+
|
40
|
+
MIT
|
41
|
+
|
42
|
+
= Authors
|
43
|
+
|
44
|
+
Jeremy Evans <code@jeremyevans.net>
|
data/cicphash.rb
CHANGED
@@ -1,31 +1,6 @@
|
|
1
|
-
# CICPHash is a case insensitive case preserving hash for ruby.
|
2
|
-
#
|
3
|
-
# * RDoc: http://cicphash.rubyforge.org
|
4
|
-
# * Source: http://github.com/jeremyevans/ruby-cicphash
|
5
|
-
#
|
6
|
-
# Copyright (c) 2007, 2012 Jeremy Evans
|
7
|
-
#
|
8
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
-
# of this software and associated documentation files (the "Software"), to deal
|
10
|
-
# in the Software without restriction, including without limitation the rights
|
11
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
-
# copies of the Software, and to permit persons to whom the Software is
|
13
|
-
# furnished to do so, subject to the following conditions:
|
14
|
-
#
|
15
|
-
# The above copyright notice and this permission notice shall be included in
|
16
|
-
# all copies or substantial portions of the Software.
|
17
|
-
#
|
18
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24
|
-
# SOFTWARE.
|
25
|
-
|
26
1
|
# Case Insensitive Case Preserving Hash
|
27
2
|
#
|
28
|
-
# CICPHash has the
|
3
|
+
# CICPHash has the same interface as Hash, but is case insensitive
|
29
4
|
# and case preserving. Any value can be used as a key. However, you
|
30
5
|
# cannot have two keys in the same CICPHash that would be the same if when
|
31
6
|
# converted to strings would be equal or differing only in case.
|
@@ -36,7 +11,7 @@
|
|
36
11
|
# CICPHash uses a last match wins policy. If an key-value pair is added to
|
37
12
|
# a CICPHash and a case insensitive variant of the key is already in the hash
|
38
13
|
# the instance of the key in the hash becomes the same the most recently added
|
39
|
-
# key (and
|
14
|
+
# key (and the value is updated to reflect the new value).
|
40
15
|
#
|
41
16
|
# You can change the rules determining which keys are equal by modifying the
|
42
17
|
# private convert_key method. By default, it is set to key.to_s.downcase.
|
@@ -166,7 +141,8 @@ class CICPHash
|
|
166
141
|
elsif default.length == 1
|
167
142
|
default.first
|
168
143
|
else
|
169
|
-
|
144
|
+
error_class = RUBY_VERSION < '1.9' ? IndexError : KeyError
|
145
|
+
raise error_class, "key not found"
|
170
146
|
end
|
171
147
|
end
|
172
148
|
|
@@ -186,7 +162,10 @@ class CICPHash
|
|
186
162
|
def key(value)
|
187
163
|
@name_hash[@hash.key(value)]
|
188
164
|
end
|
189
|
-
|
165
|
+
|
166
|
+
if RUBY_VERSION < '3'
|
167
|
+
alias index key
|
168
|
+
end
|
190
169
|
else
|
191
170
|
def index(value)
|
192
171
|
@name_hash[@hash.index(value)]
|
@@ -304,10 +283,21 @@ class CICPHash
|
|
304
283
|
def values_at(*keys)
|
305
284
|
keys.collect{|key| self[key]}
|
306
285
|
end
|
307
|
-
|
308
|
-
|
286
|
+
|
287
|
+
if RUBY_VERSION < '1.9'
|
288
|
+
alias indexes values_at
|
289
|
+
alias indices values_at
|
290
|
+
end
|
309
291
|
|
310
292
|
if RUBY_VERSION >= '1.9'
|
293
|
+
def compare_by_identity
|
294
|
+
raise TypeError, "CICPHash cannot compare by identity, use regular Hash"
|
295
|
+
end
|
296
|
+
|
297
|
+
def compare_by_identity?
|
298
|
+
false
|
299
|
+
end
|
300
|
+
|
311
301
|
def assoc(obj)
|
312
302
|
obj = convert_key(obj)
|
313
303
|
each do |k, v|
|
@@ -355,7 +345,100 @@ class CICPHash
|
|
355
345
|
self if mod
|
356
346
|
end
|
357
347
|
end
|
348
|
+
|
349
|
+
if RUBY_VERSION >= '2.0'
|
350
|
+
alias to_h to_hash
|
351
|
+
end
|
358
352
|
|
353
|
+
if RUBY_VERSION >= '2.3'
|
354
|
+
def >(other)
|
355
|
+
to_hash > other.to_hash
|
356
|
+
end
|
357
|
+
|
358
|
+
def >=(other)
|
359
|
+
to_hash >= other.to_hash
|
360
|
+
end
|
361
|
+
|
362
|
+
def <(other)
|
363
|
+
to_hash < other.to_hash
|
364
|
+
end
|
365
|
+
|
366
|
+
def <=(other)
|
367
|
+
to_hash <= other.to_hash
|
368
|
+
end
|
369
|
+
|
370
|
+
def dig(arg, *a)
|
371
|
+
h = self[arg]
|
372
|
+
if a.empty?
|
373
|
+
h
|
374
|
+
elsif !h.nil?
|
375
|
+
raise TypeError, "#{h.class} does not have #dig method" unless h.respond_to?(:dig)
|
376
|
+
h.dig(*a)
|
377
|
+
end
|
378
|
+
end
|
379
|
+
|
380
|
+
def fetch_values(*a)
|
381
|
+
a.map{|x| fetch(x)}
|
382
|
+
end
|
383
|
+
|
384
|
+
def to_proc
|
385
|
+
lambda{|x| self[x]}
|
386
|
+
end
|
387
|
+
end
|
388
|
+
|
389
|
+
if RUBY_VERSION >= '2.4'
|
390
|
+
def compact
|
391
|
+
hash = dup
|
392
|
+
hash.compact!
|
393
|
+
hash
|
394
|
+
end
|
395
|
+
|
396
|
+
def compact!
|
397
|
+
hash = to_hash
|
398
|
+
replace(hash) if hash.compact!
|
399
|
+
end
|
400
|
+
|
401
|
+
def transform_values(&block)
|
402
|
+
dup.transform_values!(&block)
|
403
|
+
end
|
404
|
+
|
405
|
+
def transform_values!(&block)
|
406
|
+
replace(to_hash.transform_values!(&block))
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
if RUBY_VERSION >= '2.5'
|
411
|
+
def slice(*a)
|
412
|
+
h = self.class.new
|
413
|
+
a.each{|k| h[k] = self[k] if has_key?(k)}
|
414
|
+
h
|
415
|
+
end
|
416
|
+
|
417
|
+
def transform_keys(&block)
|
418
|
+
dup.transform_keys!(&block)
|
419
|
+
end
|
420
|
+
|
421
|
+
def transform_keys!(&block)
|
422
|
+
replace(to_hash.transform_keys!(&block))
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
if RUBY_VERSION >= '2.6'
|
427
|
+
alias filter! select!
|
428
|
+
end
|
429
|
+
|
430
|
+
if RUBY_VERSION >= '2.7'
|
431
|
+
alias deconstruct_keys to_hash
|
432
|
+
end
|
433
|
+
|
434
|
+
if RUBY_VERSION >= '3.0'
|
435
|
+
def except(*a)
|
436
|
+
h = dup
|
437
|
+
a.each{|k| h.delete(k)}
|
438
|
+
h
|
439
|
+
end
|
440
|
+
end
|
441
|
+
|
359
442
|
private
|
360
443
|
|
361
444
|
def convert_key(key)
|
data/test/test_cicphash.rb
CHANGED
@@ -1,14 +1,22 @@
|
|
1
|
-
#!/usr/local/bin/ruby
|
2
|
-
|
3
1
|
$: << File.dirname(File.dirname(File.expand_path(__FILE__)))
|
4
2
|
require 'cicphash'
|
5
|
-
|
3
|
+
ENV['MT_NO_PLUGINS'] = '1' # Work around stupid autoloading of plugins
|
4
|
+
require 'rubygems'
|
5
|
+
gem 'minitest'
|
6
|
+
require 'minitest/autorun'
|
6
7
|
|
7
|
-
class CICPHashTest < Test
|
8
|
+
class CICPHashTest < Minitest::Test
|
8
9
|
def setup
|
9
10
|
@h = CICPHash[]
|
10
11
|
@fh = CICPHash['AB'=>1, :cd=>2, 3=>4]
|
11
12
|
end
|
13
|
+
|
14
|
+
def test_public_interface
|
15
|
+
cicphash_methods = CICPHash.public_instance_methods.sort
|
16
|
+
hash_methods = Hash.public_instance_methods.sort
|
17
|
+
assert_empty(cicphash_methods - hash_methods)
|
18
|
+
assert_empty(hash_methods - cicphash_methods)
|
19
|
+
end
|
12
20
|
|
13
21
|
def test_constructors_and_equality
|
14
22
|
# Test CICPHash.[]
|
@@ -17,7 +25,7 @@ class CICPHashTest < Test::Unit::TestCase
|
|
17
25
|
assert_equal Hash[1=>2, 3=>4], CICPHash[1=>2, 3=>4]
|
18
26
|
assert_equal Hash[1,2,3,4], CICPHash[1,2,3,4]
|
19
27
|
assert_equal Hash[:ab,:c,:de,:f], CICPHash[:ab,:c,:de,:f]
|
20
|
-
|
28
|
+
refute_equal Hash[:AB,:c,:de,:f], CICPHash[:ab,:c,:de,:f]
|
21
29
|
assert_raises(ArgumentError){CICPHash[1]}
|
22
30
|
assert_raises(ArgumentError){CICPHash[1,2,3]}
|
23
31
|
|
@@ -53,7 +61,7 @@ class CICPHashTest < Test::Unit::TestCase
|
|
53
61
|
end
|
54
62
|
|
55
63
|
def test_store_and_retrieve
|
56
|
-
|
64
|
+
assert_nil @h[1]
|
57
65
|
@h[1] = 2
|
58
66
|
assert_equal 2, @h[1]
|
59
67
|
assert_equal 2, @h[:'1']
|
@@ -86,32 +94,32 @@ class CICPHashTest < Test::Unit::TestCase
|
|
86
94
|
end
|
87
95
|
|
88
96
|
def test_defaults
|
89
|
-
|
90
|
-
|
91
|
-
|
97
|
+
assert_nil @fh.default
|
98
|
+
assert_nil @fh.default_proc
|
99
|
+
assert_nil @fh[55]
|
92
100
|
assert_equal 3, CICPHash.new(3).default
|
93
|
-
|
101
|
+
assert_nil CICPHash.new(3).default_proc
|
94
102
|
assert_equal 3, CICPHash.new(3)[1]
|
95
103
|
|
96
104
|
@fh.default = 4
|
97
105
|
assert_equal 4, @fh.default
|
98
|
-
|
106
|
+
assert_nil @fh.default_proc
|
99
107
|
assert_equal 4, @fh[55]
|
100
108
|
|
101
109
|
h = CICPHash.new(5)
|
102
110
|
assert_equal 5, h.default
|
103
|
-
|
111
|
+
assert_nil h.default_proc
|
104
112
|
assert_equal 5, h[55]
|
105
113
|
|
106
114
|
h = CICPHash.new{|hash, key| 1234}
|
107
|
-
|
108
|
-
|
115
|
+
assert_nil h.default
|
116
|
+
refute_equal nil, h.default_proc
|
109
117
|
assert_equal 1234, h[55]
|
110
118
|
|
111
119
|
h = CICPHash.new{|hash, key| hash[key] = 1234; nil}
|
112
|
-
|
113
|
-
|
114
|
-
|
120
|
+
assert_nil h.default
|
121
|
+
refute_equal nil, h.default_proc
|
122
|
+
assert_nil h[55]
|
115
123
|
assert_equal 1234, h[55]
|
116
124
|
end
|
117
125
|
|
@@ -119,7 +127,7 @@ class CICPHashTest < Test::Unit::TestCase
|
|
119
127
|
assert_equal 3, @fh.length
|
120
128
|
assert_equal 1, @fh.delete(:ab)
|
121
129
|
assert_equal 2, @fh.length
|
122
|
-
|
130
|
+
assert_nil @fh.delete(:ab)
|
123
131
|
assert_equal 2, @fh.length
|
124
132
|
end
|
125
133
|
|
@@ -134,7 +142,7 @@ class CICPHashTest < Test::Unit::TestCase
|
|
134
142
|
assert_equal Hash[3=>4], hash
|
135
143
|
assert_equal 1, @fh.length
|
136
144
|
assert_equal Hash[3=>4], @fh
|
137
|
-
|
145
|
+
assert_nil @fh.reject!{|key, value| key.to_s.length >= 2}
|
138
146
|
hash = @fh.reject!{|key, value| key.to_s.length == 1}
|
139
147
|
assert_equal 0, hash.length
|
140
148
|
assert_equal Hash[], hash
|
@@ -235,12 +243,14 @@ class CICPHashTest < Test::Unit::TestCase
|
|
235
243
|
end
|
236
244
|
end
|
237
245
|
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
246
|
+
if RUBY_VERSION < '3'
|
247
|
+
def test_index
|
248
|
+
assert_nil @h.index(1)
|
249
|
+
assert_equal 'AB', @fh.index(1)
|
250
|
+
assert_equal :cd, @fh.index(2)
|
251
|
+
assert_nil @fh.index(3)
|
252
|
+
assert_equal 3, @fh.index(4)
|
253
|
+
end
|
244
254
|
end
|
245
255
|
|
246
256
|
def test_inspect_and_to_s
|
@@ -258,7 +268,7 @@ class CICPHashTest < Test::Unit::TestCase
|
|
258
268
|
assert_equal Hash[1=>2, 3=>4].invert, CICPHash[1=>2, 3=>4].invert
|
259
269
|
assert_equal Hash[1,2,3,4].invert, CICPHash[1,2,3,4].invert
|
260
270
|
assert_equal Hash[:ab,:c,:de,:f].invert, CICPHash[:ab,:c,:de,:f].invert
|
261
|
-
|
271
|
+
refute_equal Hash[:ab,:c,:de,:f].invert, CICPHash[:aB,:c,:de,:f].invert
|
262
272
|
assert [{2=>1},{2=>3}].include?(CICPHash[1,2,3,2].invert)
|
263
273
|
end
|
264
274
|
|
@@ -325,10 +335,10 @@ class CICPHashTest < Test::Unit::TestCase
|
|
325
335
|
assert_equal CICPHash['AB'=>1, :cd=>2, 3=>4, 'blah'=>23], @fh.rehash
|
326
336
|
x.replace("DIFF")
|
327
337
|
assert_equal 23, @fh['blah']
|
328
|
-
|
338
|
+
assert_nil @fh['DIFF']
|
329
339
|
assert_equal CICPHash['AB'=>1, :cd=>2, 3=>4, 'DIFF'=>23], @fh
|
330
340
|
assert_equal CICPHash['AB'=>1, :cd=>2, 3=>4, 'DIFF'=>23], @fh.rehash
|
331
|
-
|
341
|
+
assert_nil @fh['blah']
|
332
342
|
assert_equal 23, @fh['DIFF']
|
333
343
|
end
|
334
344
|
|
@@ -349,10 +359,10 @@ class CICPHashTest < Test::Unit::TestCase
|
|
349
359
|
end
|
350
360
|
|
351
361
|
def test_shift
|
352
|
-
|
362
|
+
assert_nil @h.shift
|
353
363
|
array = @fh.to_a
|
354
364
|
i = 3
|
355
|
-
|
365
|
+
while true
|
356
366
|
assert i >= 0
|
357
367
|
kv = @fh.shift
|
358
368
|
if kv.nil?
|
@@ -401,12 +411,27 @@ class CICPHashTest < Test::Unit::TestCase
|
|
401
411
|
assert_equal [4, 2, nil, 1], @fh.values_at('3', 'CD', 32, :ab)
|
402
412
|
end
|
403
413
|
|
414
|
+
if RUBY_VERSION < '1.9'
|
415
|
+
def test_indexes_indices
|
416
|
+
[:indexes, :indices].each do |meth|
|
417
|
+
assert_equal [], @h.send(meth)
|
418
|
+
assert_equal [nil], @h.send(meth, 1)
|
419
|
+
assert_equal [nil, nil], @h.send(meth, 1, 1)
|
420
|
+
assert_equal [], @fh.send(meth)
|
421
|
+
assert_equal [1], @fh.send(meth, :ab)
|
422
|
+
assert_equal [2, 1], @fh.send(meth, 'CD', :ab)
|
423
|
+
assert_equal [2, nil, 1], @fh.send(meth, 'CD', 32, :ab)
|
424
|
+
assert_equal [4, 2, nil, 1], @fh.send(meth, '3', 'CD', 32, :ab)
|
425
|
+
end
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
404
429
|
if RUBY_VERSION >= '1.9'
|
405
430
|
def test_assoc
|
406
|
-
|
431
|
+
assert_nil @h.assoc(1)
|
407
432
|
assert_equal ['AB', 1], @fh.assoc(:Ab)
|
408
433
|
assert_equal [:cd, 2], @fh.assoc('CD')
|
409
|
-
|
434
|
+
assert_nil @fh.assoc(4)
|
410
435
|
assert_equal [3, 4], @fh.assoc('3')
|
411
436
|
end
|
412
437
|
|
@@ -433,26 +458,200 @@ class CICPHashTest < Test::Unit::TestCase
|
|
433
458
|
end
|
434
459
|
|
435
460
|
def test_key
|
436
|
-
|
437
|
-
assert_equal 'AB', @fh.
|
438
|
-
assert_equal :cd, @fh.
|
439
|
-
|
440
|
-
assert_equal 3, @fh.
|
461
|
+
assert_nil @h.key(1)
|
462
|
+
assert_equal 'AB', @fh.key(1)
|
463
|
+
assert_equal :cd, @fh.key(2)
|
464
|
+
assert_nil @fh.key(3)
|
465
|
+
assert_equal 3, @fh.key(4)
|
441
466
|
end
|
442
467
|
|
443
468
|
def test_rassoc
|
444
|
-
|
469
|
+
assert_nil @h.rassoc(1)
|
445
470
|
assert_equal ['AB', 1], @fh.rassoc(1)
|
446
471
|
assert_equal [:cd, 2], @fh.rassoc(2)
|
447
|
-
|
472
|
+
assert_nil @fh.rassoc(3)
|
448
473
|
assert_equal [3, 4], @fh.rassoc(4)
|
449
474
|
end
|
450
475
|
|
451
476
|
def test_select!
|
452
|
-
|
453
|
-
|
477
|
+
assert_nil @h.select!{|k, v| true}
|
478
|
+
assert_nil @fh.select!{|k, v| true}
|
454
479
|
assert_equal @h, @fh.dup.select!{|k, v| false}
|
455
480
|
assert_equal CICPHash["AB"=>1], @fh.select!{|k, v| k == "AB"}
|
456
481
|
end
|
482
|
+
|
483
|
+
def test_compare_by_identity
|
484
|
+
assert_raises(TypeError){@fh.compare_by_identity}
|
485
|
+
end
|
486
|
+
|
487
|
+
def test_compare_by_identity?
|
488
|
+
assert_equal(false, @fh.compare_by_identity?)
|
489
|
+
end
|
490
|
+
end
|
491
|
+
|
492
|
+
if RUBY_VERSION >= '2.0'
|
493
|
+
def test_to_h
|
494
|
+
assert_equal Hash[], @h.to_h
|
495
|
+
assert_equal Hash[3,4,'AB',1,:cd,2], @fh.to_h
|
496
|
+
end
|
497
|
+
end
|
498
|
+
|
499
|
+
if RUBY_VERSION >= '2.3'
|
500
|
+
def test_gt
|
501
|
+
assert_equal(false, @fh > @fh)
|
502
|
+
assert_equal(true, @fh > CICPHash['AB'=>1, :cd=>2])
|
503
|
+
assert_equal(false, @fh > CICPHash['AB'=>1, :cd=>2, 3=>4, 5=>6])
|
504
|
+
assert_equal(false, @fh > CICPHash[:AB=>1, :cd=>2, 3=>4])
|
505
|
+
assert_equal(false, @fh > CICPHash[:AB=>1, :cd=>2])
|
506
|
+
assert_equal(false, @fh > CICPHash[:AB=>1, :cd=>2, 3=>4, 5=>6])
|
507
|
+
end
|
508
|
+
|
509
|
+
def test_gte
|
510
|
+
assert_equal(true, @fh >= @fh)
|
511
|
+
assert_equal(true, @fh >= CICPHash['AB'=>1, :cd=>2])
|
512
|
+
assert_equal(false, @fh >= CICPHash['AB'=>1, :cd=>2, 3=>4, 5=>6])
|
513
|
+
assert_equal(false, @fh >= CICPHash[:AB=>1, :cd=>2, 3=>4])
|
514
|
+
assert_equal(false, @fh >= CICPHash[:AB=>1, :cd=>2])
|
515
|
+
assert_equal(false, @fh >= CICPHash[:AB=>1, :cd=>2, 3=>4, 5=>6])
|
516
|
+
end
|
517
|
+
|
518
|
+
def test_lt
|
519
|
+
assert_equal(false, @fh < @fh)
|
520
|
+
assert_equal(false, @fh < CICPHash['AB'=>1, :cd=>2])
|
521
|
+
assert_equal(true, @fh < CICPHash['AB'=>1, :cd=>2, 3=>4, 5=>6])
|
522
|
+
assert_equal(false, @fh < CICPHash[:AB=>1, :cd=>2, 3=>4])
|
523
|
+
assert_equal(false, @fh < CICPHash[:AB=>1, :cd=>2])
|
524
|
+
assert_equal(false, @fh < CICPHash[:AB=>1, :cd=>2, 3=>4, 5=>6])
|
525
|
+
end
|
526
|
+
|
527
|
+
def test_lte
|
528
|
+
assert_equal(true, @fh <= @fh)
|
529
|
+
assert_equal(false, @fh <= CICPHash['AB'=>1, :cd=>2])
|
530
|
+
assert_equal(true, @fh <= CICPHash['AB'=>1, :cd=>2, 3=>4, 5=>6])
|
531
|
+
assert_equal(false, @fh <= CICPHash[:AB=>1, :cd=>2, 3=>4])
|
532
|
+
assert_equal(false, @fh <= CICPHash[:AB=>1, :cd=>2])
|
533
|
+
assert_equal(false, @fh <= CICPHash[:AB=>1, :cd=>2, 3=>4, 5=>6])
|
534
|
+
end
|
535
|
+
|
536
|
+
def test_dig
|
537
|
+
assert_equal(1, @fh.dig(:AB))
|
538
|
+
assert_equal(2, @fh.dig('cd'))
|
539
|
+
assert_equal(4, @fh.dig('3'))
|
540
|
+
assert_nil(@fh.dig(4))
|
541
|
+
|
542
|
+
assert_raises(TypeError){@fh.dig(:AB, 1)}
|
543
|
+
assert_raises(TypeError){@fh.dig('cd', 2)}
|
544
|
+
assert_raises(TypeError){@fh.dig('3', 3)}
|
545
|
+
assert_nil(@fh.dig(4, 5))
|
546
|
+
|
547
|
+
fh = CICPHash['AB'=>{1=>2}, 3=>CICPHash['CD'=>4]]
|
548
|
+
assert_equal(2, fh.dig(:AB, 1))
|
549
|
+
assert_nil(fh.dig(:AB, 2))
|
550
|
+
assert_raises(TypeError){fh.dig(:AB, 1, 3)}
|
551
|
+
assert_nil(fh.dig(:AB, 2, 3))
|
552
|
+
|
553
|
+
assert_equal(4, fh.dig('3', :cd))
|
554
|
+
assert_nil(fh.dig(3, 2))
|
555
|
+
assert_nil(fh.dig(4))
|
556
|
+
end
|
557
|
+
|
558
|
+
def test_fetch_values
|
559
|
+
assert_equal([1], @fh.fetch_values(:AB))
|
560
|
+
assert_equal([1, 2, 4], @fh.fetch_values(:AB, 'cd', '3'))
|
561
|
+
assert_raises(KeyError){@fh.fetch_values(:AB, 'cd', 4)}
|
562
|
+
end
|
563
|
+
|
564
|
+
def test_to_proc
|
565
|
+
pr = @fh.to_proc
|
566
|
+
assert_equal(1, pr[:AB])
|
567
|
+
assert_equal(2, pr['cd'])
|
568
|
+
assert_equal(4, pr['3'])
|
569
|
+
assert_nil(pr[4])
|
570
|
+
end
|
571
|
+
end
|
572
|
+
|
573
|
+
if RUBY_VERSION >= '2.4'
|
574
|
+
def test_compact
|
575
|
+
assert_equal(false, @fh.compact.equal?(@fh))
|
576
|
+
assert_equal(@fh, @fh.compact)
|
577
|
+
assert_equal(CICPHash['AB'=>1], CICPHash['AB'=>1, :cd=>nil].compact)
|
578
|
+
end
|
579
|
+
|
580
|
+
def test_compact!
|
581
|
+
fh = @fh.dup
|
582
|
+
assert_nil(@fh.compact!)
|
583
|
+
assert_equal(fh, @fh)
|
584
|
+
|
585
|
+
h = CICPHash['AB'=>1, :cd=>nil]
|
586
|
+
assert_equal(CICPHash['AB'=>1], h.compact!)
|
587
|
+
assert_equal(CICPHash['AB'=>1], h)
|
588
|
+
end
|
589
|
+
|
590
|
+
def test_transform_values
|
591
|
+
fh = @fh.transform_values{|v| v.to_s*2}
|
592
|
+
assert_equal(1, @fh[:AB])
|
593
|
+
assert_equal(CICPHash['AB'=>'11', :cd=>'22', 3=>'44'], fh)
|
594
|
+
assert_equal('11', fh[:AB])
|
595
|
+
end
|
596
|
+
|
597
|
+
def test_transform_values!
|
598
|
+
@fh.transform_values!{|v| v.to_s*2}
|
599
|
+
assert_equal('11', @fh[:AB])
|
600
|
+
assert_equal(CICPHash['AB'=>'11', :cd=>'22', 3=>'44'], @fh)
|
601
|
+
assert_equal('11', @fh[:AB])
|
602
|
+
end
|
603
|
+
end
|
604
|
+
|
605
|
+
if RUBY_VERSION >= '2.5'
|
606
|
+
def test_slice
|
607
|
+
assert_equal(CICPHash[:AB=>1, 'CD'=>2, '3'=>4], @fh.slice(:AB, 'CD', '3'))
|
608
|
+
assert_equal(CICPHash[:AB=>1, 'CD'=>2], @fh.slice(:AB, 'CD'))
|
609
|
+
assert_equal(1, @fh.slice(:AB, 'CD')['AB'])
|
610
|
+
end
|
611
|
+
|
612
|
+
def test_transform_keys
|
613
|
+
map = {'AB'=>:XY, :cd=>'DC', 3=>'5'}
|
614
|
+
dh = @fh.dup
|
615
|
+
fh = @fh.transform_keys{|k| map[k]}
|
616
|
+
assert_equal(dh, @fh)
|
617
|
+
assert_equal(1, fh['XY'])
|
618
|
+
assert_equal(2, fh[:dc])
|
619
|
+
assert_equal(4, fh[5])
|
620
|
+
end
|
621
|
+
|
622
|
+
def test_transform_keys!
|
623
|
+
map = {'AB'=>:XY, :cd=>'DC', 3=>'5'}
|
624
|
+
dh = @fh.dup
|
625
|
+
fh = @fh.transform_keys!{|k| map[k]}
|
626
|
+
assert_equal(false, dh == @fh)
|
627
|
+
assert_equal(1, @fh['XY'])
|
628
|
+
assert_equal(2, @fh[:dc])
|
629
|
+
assert_equal(4, @fh[5])
|
630
|
+
end
|
631
|
+
end
|
632
|
+
|
633
|
+
if RUBY_VERSION >= '2.6'
|
634
|
+
def test_filter!
|
635
|
+
assert_nil @h.filter!{|k, v| true}
|
636
|
+
assert_nil @fh.filter!{|k, v| true}
|
637
|
+
assert_equal @h, @fh.dup.filter!{|k, v| false}
|
638
|
+
assert_equal CICPHash["AB"=>1], @fh.filter!{|k, v| k == "AB"}
|
639
|
+
end
|
640
|
+
end
|
641
|
+
|
642
|
+
if RUBY_VERSION >= '2.7'
|
643
|
+
def test_deconstruct_keys
|
644
|
+
assert_equal(@fh.to_hash, @fh.deconstruct_keys)
|
645
|
+
assert_equal(Hash, @fh.deconstruct_keys.class)
|
646
|
+
end
|
647
|
+
end
|
648
|
+
|
649
|
+
if RUBY_VERSION >= '3.0'
|
650
|
+
def test_except
|
651
|
+
@fh = CICPHash['AB'=>1, :cd=>2, 3=>4]
|
652
|
+
assert_equal(@fh, @fh.except)
|
653
|
+
assert_equal(CICPHash[:cd=>2, 3=>4], @fh.except('AB', 5))
|
654
|
+
assert_equal(CICPHash['AB'=>1], @fh.except('CD', '3'))
|
655
|
+
end
|
457
656
|
end
|
458
657
|
end
|
metadata
CHANGED
@@ -1,47 +1,63 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cicphash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Jeremy Evans
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2021-12-10 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
13
|
description:
|
15
14
|
email: code@jeremyevans.net
|
16
15
|
executables: []
|
17
16
|
extensions: []
|
18
|
-
extra_rdoc_files:
|
17
|
+
extra_rdoc_files:
|
18
|
+
- README.rdoc
|
19
|
+
- CHANGELOG
|
20
|
+
- MIT-LICENSE
|
19
21
|
files:
|
22
|
+
- CHANGELOG
|
23
|
+
- MIT-LICENSE
|
24
|
+
- README.rdoc
|
20
25
|
- cicphash.rb
|
21
26
|
- test/test_cicphash.rb
|
22
|
-
homepage: http://cicphash.
|
23
|
-
licenses:
|
27
|
+
homepage: http://ruby-cicphash.jeremyevans.net
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
metadata:
|
31
|
+
bug_tracker_uri: https://github.com/jeremyevans/ruby-cicphash/issues
|
32
|
+
changelog_uri: https://github.com/jeremyevans/ruby-cicphash/blob/master/CHANGELOG
|
33
|
+
documentation_uri: http://ruby-cicphash.jeremyevans.net
|
34
|
+
mailing_list_uri: https://github.com/jeremyevans/ruby-cicphash/discussions
|
35
|
+
source_code_uri: https://github.com/jeremyevans/ruby-cicphash
|
24
36
|
post_install_message:
|
25
|
-
rdoc_options:
|
37
|
+
rdoc_options:
|
38
|
+
- "--quiet"
|
39
|
+
- "--line-numbers"
|
40
|
+
- "--inline-source"
|
41
|
+
- "--title"
|
42
|
+
- 'CICPHash: Case Insensitive Case Preserving Hash'
|
43
|
+
- "--main"
|
44
|
+
- README.rdoc
|
26
45
|
require_paths:
|
27
|
-
- .
|
46
|
+
- "."
|
28
47
|
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
-
none: false
|
30
48
|
requirements:
|
31
|
-
- -
|
49
|
+
- - ">="
|
32
50
|
- !ruby/object:Gem::Version
|
33
51
|
version: '0'
|
34
52
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
-
none: false
|
36
53
|
requirements:
|
37
|
-
- -
|
54
|
+
- - ">="
|
38
55
|
- !ruby/object:Gem::Version
|
39
56
|
version: '0'
|
40
57
|
requirements: []
|
41
|
-
|
42
|
-
rubygems_version: 1.8.23
|
58
|
+
rubygems_version: 3.2.32
|
43
59
|
signing_key:
|
44
|
-
specification_version:
|
60
|
+
specification_version: 4
|
45
61
|
summary: Case Insensitive Case Preserving Hash
|
46
62
|
test_files:
|
47
63
|
- test/test_cicphash.rb
|