bihash 0.1.0 → 1.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 +4 -4
- data/lib/bihash.rb +46 -3
- data/lib/bihash/unimplemented_methods.rb +31 -0
- data/lib/bihash/version.rb +1 -1
- data/spec/bihash_spec.rb +224 -31
- data/spec/spec_helper.rb +0 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4950baf55de5c2d039c99642d21299374071b08
|
4
|
+
data.tar.gz: dd607c38dbe190d3cc1c230d3b85c47329d24d77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e78ef70b4e47ef0ea27c40b337fde30a21840ff426216280d6fb58922b3a5cf6acac1d70e8185b0567a8aa3210e67655e62663af7a11f59e95ad9362c2db9d28
|
7
|
+
data.tar.gz: 8d5bbda0bd6d9326314398d521e1db3fd3d52263cbaf0c4dacf3e7760180e7b8b65e90564507b9a5f72c939ea6f9019dbf5c93fbec13365c4bef9076da408415
|
data/lib/bihash.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'forwardable'
|
2
2
|
require 'bihash/version'
|
3
|
+
require 'bihash/unimplemented_methods'
|
3
4
|
|
4
5
|
class Bihash
|
5
6
|
include Enumerable
|
@@ -14,8 +15,28 @@ class Bihash
|
|
14
15
|
h && new_from_hash(h)
|
15
16
|
end
|
16
17
|
|
18
|
+
def <(rhs)
|
19
|
+
raise_error_unless_bihash(rhs)
|
20
|
+
merged_hash_attrs < rhs.send(:merged_hash_attrs)
|
21
|
+
end
|
22
|
+
|
23
|
+
def <=(rhs)
|
24
|
+
raise_error_unless_bihash(rhs)
|
25
|
+
merged_hash_attrs <= rhs.send(:merged_hash_attrs)
|
26
|
+
end
|
27
|
+
|
17
28
|
def ==(rhs)
|
18
|
-
rhs.is_a?(self.class) && rhs.send(:merged_hash_attrs)
|
29
|
+
rhs.is_a?(self.class) && merged_hash_attrs == rhs.send(:merged_hash_attrs)
|
30
|
+
end
|
31
|
+
|
32
|
+
def >(rhs)
|
33
|
+
raise_error_unless_bihash(rhs)
|
34
|
+
merged_hash_attrs > rhs.send(:merged_hash_attrs)
|
35
|
+
end
|
36
|
+
|
37
|
+
def >=(rhs)
|
38
|
+
raise_error_unless_bihash(rhs)
|
39
|
+
merged_hash_attrs >= rhs.send(:merged_hash_attrs)
|
19
40
|
end
|
20
41
|
|
21
42
|
def [](key)
|
@@ -47,6 +68,14 @@ class Bihash
|
|
47
68
|
self
|
48
69
|
end
|
49
70
|
|
71
|
+
def compact
|
72
|
+
dup.tap { |d| d.compact! }
|
73
|
+
end
|
74
|
+
|
75
|
+
def compact!
|
76
|
+
reject! { |k1, k2| k1.nil? || k2.nil? }
|
77
|
+
end
|
78
|
+
|
50
79
|
def compare_by_identity
|
51
80
|
raise_error_if_frozen
|
52
81
|
@forward.compare_by_identity
|
@@ -112,6 +141,10 @@ class Bihash
|
|
112
141
|
end
|
113
142
|
end
|
114
143
|
|
144
|
+
def dig(*keys)
|
145
|
+
(@forward.key?(keys[0]) ? @forward : @reverse).dig(*keys)
|
146
|
+
end
|
147
|
+
|
115
148
|
def each(&block)
|
116
149
|
if block_given?
|
117
150
|
@forward.each(&block)
|
@@ -131,6 +164,10 @@ class Bihash
|
|
131
164
|
(@forward.key?(key) ? @forward : @reverse).fetch(key, *default, &block)
|
132
165
|
end
|
133
166
|
|
167
|
+
def fetch_values(*keys)
|
168
|
+
keys.map { |key| fetch(key) }
|
169
|
+
end
|
170
|
+
|
134
171
|
def_delegator :@forward, :flatten
|
135
172
|
|
136
173
|
def has_key?(arg)
|
@@ -186,7 +223,7 @@ class Bihash
|
|
186
223
|
|
187
224
|
def reject(&block)
|
188
225
|
if block_given?
|
189
|
-
dup.
|
226
|
+
dup.tap { |d| d.reject!(&block) }
|
190
227
|
else
|
191
228
|
to_enum(:reject)
|
192
229
|
end
|
@@ -213,7 +250,7 @@ class Bihash
|
|
213
250
|
|
214
251
|
def select(&block)
|
215
252
|
if block_given?
|
216
|
-
dup.
|
253
|
+
dup.tap { |d| d.select!(&block) }
|
217
254
|
else
|
218
255
|
to_enum(:select)
|
219
256
|
end
|
@@ -248,6 +285,12 @@ class Bihash
|
|
248
285
|
@forward.dup
|
249
286
|
end
|
250
287
|
|
288
|
+
alias :to_hash :to_h
|
289
|
+
|
290
|
+
def to_proc
|
291
|
+
method(:[]).to_proc
|
292
|
+
end
|
293
|
+
|
251
294
|
alias :to_s :inspect
|
252
295
|
|
253
296
|
alias :update :merge!
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
class Bihash
|
4
|
+
UNIMPLEMENTED_METHODS = Set[
|
5
|
+
# expected to only deal with half the hash: keys or values
|
6
|
+
'keys',
|
7
|
+
'values',
|
8
|
+
'each_key',
|
9
|
+
'each_value',
|
10
|
+
'transform_values',
|
11
|
+
'transform_values!',
|
12
|
+
# O(n) reverse lookups
|
13
|
+
'key',
|
14
|
+
'index',
|
15
|
+
'rassoc',
|
16
|
+
'value?',
|
17
|
+
'has_value?',
|
18
|
+
# meaningless on bihash as both sides already hashed
|
19
|
+
'invert'
|
20
|
+
]
|
21
|
+
|
22
|
+
def respond_to?(method, private = false)
|
23
|
+
UNIMPLEMENTED_METHODS.include?(method.to_s) ? false : super
|
24
|
+
end
|
25
|
+
|
26
|
+
UNIMPLEMENTED_METHODS.each do |method|
|
27
|
+
define_method(method) do |*|
|
28
|
+
raise NoMethodError, "Bihash##{method} not implemented"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/bihash/version.rb
CHANGED
data/spec/bihash_spec.rb
CHANGED
@@ -5,6 +5,17 @@ describe Bihash do
|
|
5
5
|
Bihash.must_include Enumerable
|
6
6
|
end
|
7
7
|
|
8
|
+
Bihash::UNIMPLEMENTED_METHODS.each do |method|
|
9
|
+
it "should report that it does not respond to ##{method}" do
|
10
|
+
Bihash.new.respond_to?(method).must_equal false
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should raise NoMethodError if ##{method} is called" do
|
14
|
+
error = -> { Bihash.new.send(method) }.must_raise NoMethodError
|
15
|
+
error.message.must_equal "Bihash##{method} not implemented"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
8
19
|
describe '::[]' do
|
9
20
|
it 'should be able to create an empty bihash' do
|
10
21
|
bh = Bihash[]
|
@@ -66,7 +77,7 @@ describe Bihash do
|
|
66
77
|
bh = Bihash.new
|
67
78
|
bh.must_be_instance_of Bihash
|
68
79
|
bh.must_be_empty
|
69
|
-
bh[:not_a_key].
|
80
|
+
bh[:not_a_key].must_be_nil
|
70
81
|
end
|
71
82
|
|
72
83
|
it 'should create an empty bihash with a default if given an object arg' do
|
@@ -111,8 +122,13 @@ describe Bihash do
|
|
111
122
|
bh[2].must_equal :k2
|
112
123
|
end
|
113
124
|
|
125
|
+
it 'should convert a bihash to a bihash' do
|
126
|
+
bh = Bihash[:key => 'value']
|
127
|
+
Bihash.try_convert(bh).must_equal bh
|
128
|
+
end
|
129
|
+
|
114
130
|
it 'should return nil if the object does not respond to #to_hash' do
|
115
|
-
Bihash.try_convert(Object.new).
|
131
|
+
Bihash.try_convert(Object.new).must_be_nil
|
116
132
|
end
|
117
133
|
|
118
134
|
it 'should not accept a hash with duplicate values' do
|
@@ -120,6 +136,42 @@ describe Bihash do
|
|
120
136
|
end
|
121
137
|
end
|
122
138
|
|
139
|
+
describe '#<' do
|
140
|
+
it 'should raise an error if the right hand side is not a bihash' do
|
141
|
+
-> { Bihash[a: 1, b: 2] < {a: 1, b: 2, c: 3} }.must_raise TypeError
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'should return true when the argument is a strict subset of self' do
|
145
|
+
(Bihash[a: 1, b: 2] < Bihash[a: 1, b: 2, c: 3]).must_equal true
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'should return false when the argument is equal to self' do
|
149
|
+
(Bihash[a: 1, b: 2] < Bihash[a: 1, b: 2]).must_equal false
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'should return false when the argument is not a subset of self' do
|
153
|
+
(Bihash[a: 1, b: 2, c: 3] < Bihash[a: 1, b: 2]).must_equal false
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe '#<=' do
|
158
|
+
it 'should raise an error if the right hand side is not a bihash' do
|
159
|
+
-> { Bihash[a: 1, b: 2] <= {a: 1, b: 2, c: 3} }.must_raise TypeError
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'should return true when the argument is a strict subset of self' do
|
163
|
+
(Bihash[a: 1, b: 2] <= Bihash[a: 1, b: 2, c: 3]).must_equal true
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'should return true when the argument is equal to self' do
|
167
|
+
(Bihash[a: 1, b: 2] <= Bihash[a: 1, b: 2]).must_equal true
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'should return false when the argument is not a subset of self' do
|
171
|
+
(Bihash[a: 1, b: 2, c: 3] <= Bihash[a: 1, b: 2]).must_equal false
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
123
175
|
describe '#==' do
|
124
176
|
it 'should return true when two bihashes have the same pairs' do
|
125
177
|
bh1, bh2 = Bihash[:k1 => 1, :k2 => 2], Bihash[2 => :k2, 1 => :k1]
|
@@ -137,6 +189,42 @@ describe Bihash do
|
|
137
189
|
end
|
138
190
|
end
|
139
191
|
|
192
|
+
describe '#>' do
|
193
|
+
it 'should raise an error if the right hand side is not a bihash' do
|
194
|
+
-> { Bihash[a: 1, b: 2] > {a: 1, b: 2, c: 3} }.must_raise TypeError
|
195
|
+
end
|
196
|
+
|
197
|
+
it 'should return true when the argument is a strict superset of self' do
|
198
|
+
(Bihash[a: 1, b: 2, c: 3] > Bihash[a: 1, b: 2]).must_equal true
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'should return false when the argument is equal to self' do
|
202
|
+
(Bihash[a: 1, b: 2] > Bihash[a: 1, b: 2]).must_equal false
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'should return false when the argument is not a superset of self' do
|
206
|
+
(Bihash[a: 1, b: 2] > Bihash[a: 1, b: 2, c: 3]).must_equal false
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
describe '#>=' do
|
211
|
+
it 'should raise an error if the right hand side is not a bihash' do
|
212
|
+
-> { Bihash[a: 1, b: 2] >= {a: 1, b: 2, c: 3} }.must_raise TypeError
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'should return true when the argument is a strict superset of self' do
|
216
|
+
(Bihash[a: 1, b: 2, c: 3] >= Bihash[a: 1, b: 2]).must_equal true
|
217
|
+
end
|
218
|
+
|
219
|
+
it 'should return true when the argument is equal to self' do
|
220
|
+
(Bihash[a: 1, b: 2] >= Bihash[a: 1, b: 2]).must_equal true
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'should return false when the argument is not a superset of self' do
|
224
|
+
(Bihash[a: 1, b: 2] >= Bihash[a: 1, b: 2, c: 3]).must_equal false
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
140
228
|
describe '#[]' do
|
141
229
|
it 'should return the other pair' do
|
142
230
|
bh = Bihash[:key => 'value']
|
@@ -147,10 +235,10 @@ describe Bihash do
|
|
147
235
|
it 'should return falsey values correctly' do
|
148
236
|
bh1 = Bihash[nil => false]
|
149
237
|
bh1[nil].must_equal false
|
150
|
-
bh1[false].
|
238
|
+
bh1[false].must_be_nil
|
151
239
|
|
152
240
|
bh2 = Bihash[false => nil]
|
153
|
-
bh2[false].
|
241
|
+
bh2[false].must_be_nil
|
154
242
|
bh2[nil].must_equal false
|
155
243
|
end
|
156
244
|
end
|
@@ -201,14 +289,57 @@ describe Bihash do
|
|
201
289
|
|
202
290
|
it 'should return nil if the argument is not a key' do
|
203
291
|
bh = Bihash.new(404)
|
204
|
-
bh.assoc(:not_a_key).
|
292
|
+
bh.assoc(:not_a_key).must_be_nil
|
205
293
|
end
|
206
294
|
|
207
295
|
it 'should find the key using #==' do
|
208
296
|
bh = Bihash[[] => 'array']
|
209
297
|
bh['array'] << 'modified'
|
210
298
|
bh.assoc(['modified']).must_equal [['modified'], 'array']
|
211
|
-
bh.assoc([]).
|
299
|
+
bh.assoc([]).must_be_nil
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
describe '#compact' do
|
304
|
+
describe 'when any pairs contain a nil key' do
|
305
|
+
it 'should return a new bihash with any pairs containing nil removed' do
|
306
|
+
bh = Bihash[1 => :one, 2 => nil, 3 => :three]
|
307
|
+
bh.compact.must_equal Bihash[1 => :one, 3 => :three]
|
308
|
+
bh.must_equal Bihash[1 => :one, 2 => nil, 3 => :three]
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
describe 'no pairs contain a nil key' do
|
313
|
+
it 'should return a copy of the original bihash' do
|
314
|
+
bh = Bihash[1 => :one, 2 => :two, 3 => :three]
|
315
|
+
compacted_bh = bh.compact
|
316
|
+
compacted_bh.must_equal Bihash[1 => :one, 2=> :two, 3 => :three]
|
317
|
+
compacted_bh.object_id.wont_equal bh.object_id
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
describe '#compact!' do
|
323
|
+
it 'should delete any pairs containing nil' do
|
324
|
+
bh1 = Bihash[1 => :one, 2 => nil, 3 => :three]
|
325
|
+
bh1_id = bh1.object_id
|
326
|
+
bh1.compact!.object_id.must_equal bh1_id
|
327
|
+
bh1.must_equal Bihash[1 => :one, 3 => :three]
|
328
|
+
|
329
|
+
bh2 = Bihash[1 => :one, 2 => nil, 3 => :three]
|
330
|
+
bh2_id = bh2.object_id
|
331
|
+
bh2.compact!.object_id.must_equal bh2_id
|
332
|
+
bh2.must_equal Bihash[1 => :one, 3 => :three]
|
333
|
+
end
|
334
|
+
|
335
|
+
it 'should return nil if no changes were made to the bihash' do
|
336
|
+
bh = Bihash[1 => :one, 2 => :two, 3 => :three, 4 => :four]
|
337
|
+
bh.compact!.must_be_nil
|
338
|
+
bh.must_equal Bihash[1 => :one, 2 => :two, 3 => :three, 4 => :four]
|
339
|
+
end
|
340
|
+
|
341
|
+
it 'should raise RuntimeError if called on a frozen bihash' do
|
342
|
+
-> { Bihash.new.freeze.compact! }.must_raise RuntimeError
|
212
343
|
end
|
213
344
|
end
|
214
345
|
|
@@ -229,7 +360,7 @@ describe Bihash do
|
|
229
360
|
bh = Bihash[1 => :one]
|
230
361
|
clone = bh.clone
|
231
362
|
clone[2] = :two
|
232
|
-
bh[2].
|
363
|
+
bh[2].must_be_nil
|
233
364
|
end
|
234
365
|
end
|
235
366
|
|
@@ -238,8 +369,8 @@ describe Bihash do
|
|
238
369
|
bh = Bihash.new.compare_by_identity
|
239
370
|
key1, key2 = 'key', 'value'
|
240
371
|
bh[key1] = key2
|
241
|
-
bh['key'].
|
242
|
-
bh['value'].
|
372
|
+
bh['key'].must_be_nil
|
373
|
+
bh['value'].must_be_nil
|
243
374
|
bh[key1].must_equal 'value'
|
244
375
|
bh[key2].must_equal 'key'
|
245
376
|
end
|
@@ -264,9 +395,9 @@ describe Bihash do
|
|
264
395
|
describe 'when there is not a default proc' do
|
265
396
|
it 'should return the default' do
|
266
397
|
bh1 = Bihash[:key => 'value']
|
267
|
-
bh1.default.
|
268
|
-
bh1.default(:not_a_key).
|
269
|
-
bh1.default(:key).
|
398
|
+
bh1.default.must_be_nil
|
399
|
+
bh1.default(:not_a_key).must_be_nil
|
400
|
+
bh1.default(:key).must_be_nil
|
270
401
|
|
271
402
|
bh2 = Bihash.new(404)
|
272
403
|
bh2[:key] = 'value'
|
@@ -278,7 +409,7 @@ describe Bihash do
|
|
278
409
|
|
279
410
|
describe 'when there is a default proc' do
|
280
411
|
it 'should return the default if called with no argument' do
|
281
|
-
Bihash.new { 'proc called' }.default.
|
412
|
+
Bihash.new { 'proc called' }.default.must_be_nil
|
282
413
|
end
|
283
414
|
|
284
415
|
it 'should call the default proc when called with an argument' do
|
@@ -317,8 +448,8 @@ describe Bihash do
|
|
317
448
|
end
|
318
449
|
|
319
450
|
it 'should return nil if there is no default proc' do
|
320
|
-
Bihash.new.default_proc.
|
321
|
-
Bihash.new(404).default_proc.
|
451
|
+
Bihash.new.default_proc.must_be_nil
|
452
|
+
Bihash.new(404).default_proc.must_be_nil
|
322
453
|
end
|
323
454
|
end
|
324
455
|
|
@@ -333,8 +464,8 @@ describe Bihash do
|
|
333
464
|
it 'should set the default value to nil if argument is nil' do
|
334
465
|
bh = Bihash.new(:default_object)
|
335
466
|
bh[:not_a_key].must_equal :default_object
|
336
|
-
(bh.default_proc = nil).
|
337
|
-
bh[:not_a_key].
|
467
|
+
(bh.default_proc = nil).must_be_nil
|
468
|
+
bh[:not_a_key].must_be_nil
|
338
469
|
end
|
339
470
|
|
340
471
|
it 'should raise TypeError if not given a non-proc (except nil)' do
|
@@ -397,12 +528,35 @@ describe Bihash do
|
|
397
528
|
end
|
398
529
|
end
|
399
530
|
|
531
|
+
describe '#dig' do
|
532
|
+
it 'should traverse nested bihashes' do
|
533
|
+
bh = Bihash[foo: Bihash[bar: Bihash[baz: 4]]]
|
534
|
+
bh.dig(:foo, :bar, :baz).must_equal 4
|
535
|
+
bh.dig(:foo, :bar, 4).must_equal :baz
|
536
|
+
end
|
537
|
+
|
538
|
+
it 'should traverse nested hashes' do
|
539
|
+
bh = Bihash[foo: {bar: {baz: 4}}]
|
540
|
+
bh.dig(:foo, :bar, :baz).must_equal 4
|
541
|
+
end
|
542
|
+
|
543
|
+
it 'should traverse nested arrays' do
|
544
|
+
bh = Bihash[foo: [[4]]]
|
545
|
+
bh.dig(:foo, 0, 0).must_equal 4
|
546
|
+
end
|
547
|
+
|
548
|
+
it 'should return nil if any intermediate step is nil' do
|
549
|
+
bh = Bihash[foo: Bihash[bar: Bihash[baz: 4]]]
|
550
|
+
bh.dig(:foo, :bur, :boz).must_be_nil
|
551
|
+
end
|
552
|
+
end
|
553
|
+
|
400
554
|
describe '#dup' do
|
401
555
|
it 'should make a copy of the bihash' do
|
402
556
|
bh = Bihash[1 => :one]
|
403
557
|
dup = bh.dup
|
404
558
|
dup[2] = :two
|
405
|
-
bh[2].
|
559
|
+
bh[2].must_be_nil
|
406
560
|
end
|
407
561
|
end
|
408
562
|
|
@@ -448,10 +602,10 @@ describe Bihash do
|
|
448
602
|
it 'should return falsey values correctly' do
|
449
603
|
bh1 = Bihash[nil => false]
|
450
604
|
bh1.fetch(nil).must_equal false
|
451
|
-
bh1.fetch(false).
|
605
|
+
bh1.fetch(false).must_be_nil
|
452
606
|
|
453
607
|
bh2 = Bihash[false => nil]
|
454
|
-
bh2.fetch(false).
|
608
|
+
bh2.fetch(false).must_be_nil
|
455
609
|
bh2.fetch(nil).must_equal false
|
456
610
|
end
|
457
611
|
|
@@ -470,8 +624,28 @@ describe Bihash do
|
|
470
624
|
end
|
471
625
|
end
|
472
626
|
|
627
|
+
describe '#fetch_values' do
|
628
|
+
it 'should return an array of values corresponding to the given keys' do
|
629
|
+
Bihash[1 => :one, 2 => :two].fetch_values(1, 2).must_equal [:one, :two]
|
630
|
+
Bihash[1 => :one, 2 => :two].fetch_values(:one, :two).must_equal [1, 2]
|
631
|
+
Bihash[1 => :one, 2 => :two].fetch_values(1, :two).must_equal [:one, 2]
|
632
|
+
end
|
633
|
+
|
634
|
+
it 'should raise a KeyError if any key is not found' do
|
635
|
+
-> { Bihash.new.fetch_values(404) }.must_raise KeyError
|
636
|
+
end
|
637
|
+
|
638
|
+
it 'should not duplicate entries if a key equals its value' do
|
639
|
+
Bihash[:key => :key].fetch_values(:key).must_equal [:key]
|
640
|
+
end
|
641
|
+
|
642
|
+
it 'should return an empty array with no args' do
|
643
|
+
Bihash[:key => 'value'].fetch_values.must_equal []
|
644
|
+
end
|
645
|
+
end
|
646
|
+
|
473
647
|
describe '#flatten' do
|
474
|
-
it 'extract the pairs into an array' do
|
648
|
+
it 'should extract the pairs into an array' do
|
475
649
|
Bihash[:k1 => 'v1', :k2 => 'v2'].flatten.must_equal [:k1, 'v1', :k2, 'v2']
|
476
650
|
end
|
477
651
|
|
@@ -574,7 +748,7 @@ describe Bihash do
|
|
574
748
|
it 'should recompute all key hash values and return the bihash' do
|
575
749
|
bh = Bihash[[] => :array]
|
576
750
|
bh[:array] << 1
|
577
|
-
bh[[1]].
|
751
|
+
bh[[1]].must_be_nil
|
578
752
|
bh.rehash[[1]].must_equal :array
|
579
753
|
bh[[1]].must_equal :array
|
580
754
|
end
|
@@ -591,14 +765,16 @@ describe Bihash do
|
|
591
765
|
end
|
592
766
|
|
593
767
|
describe '#reject' do
|
594
|
-
describe '
|
595
|
-
it '
|
768
|
+
describe 'when some items are rejected' do
|
769
|
+
it 'should return a bihash with items not rejected by the block' do
|
596
770
|
bh = Bihash[1 => :one, 2 => :two, 3 => :three, 4 => :four]
|
597
771
|
bh.reject { |k1,k2| k1.even? }.must_equal Bihash[1 => :one, 3 => :three]
|
598
772
|
bh.must_equal Bihash[1 => :one, 2 => :two, 3 => :three, 4 => :four]
|
599
773
|
end
|
774
|
+
end
|
600
775
|
|
601
|
-
|
776
|
+
describe 'when no items are rejected' do
|
777
|
+
it 'should return a bihash with items not rejected by the block' do
|
602
778
|
bh = Bihash[1 => :one, 3 => :three, 5 => :five, 7 => :seven]
|
603
779
|
bh.reject { |k1,k2| k1.even? }.must_equal bh
|
604
780
|
bh.must_equal bh
|
@@ -622,7 +798,7 @@ describe Bihash do
|
|
622
798
|
|
623
799
|
it 'should return nil if no changes were made to the bihash' do
|
624
800
|
bh = Bihash[1 => :one, 2 => :two, 3 => :three, 4 => :four]
|
625
|
-
bh.reject! { |key1, key2| key1 > 5 }.
|
801
|
+
bh.reject! { |key1, key2| key1 > 5 }.must_be_nil
|
626
802
|
bh.must_equal Bihash[1 => :one, 2 => :two, 3 => :three, 4 => :four]
|
627
803
|
end
|
628
804
|
|
@@ -658,14 +834,16 @@ describe Bihash do
|
|
658
834
|
end
|
659
835
|
|
660
836
|
describe '#select' do
|
661
|
-
describe '
|
662
|
-
it '
|
837
|
+
describe 'when only some items are selected' do
|
838
|
+
it 'should return a bihash with items selected by the block' do
|
663
839
|
bh = Bihash[1 => :one, 2 => :two, 3 => :three, 4 => :four]
|
664
840
|
bh.select { |k1,k2| k1.even? }.must_equal Bihash[2 => :two, 4 => :four]
|
665
841
|
bh.must_equal Bihash[1 => :one, 2 => :two, 3 => :three, 4 => :four]
|
666
842
|
end
|
843
|
+
end
|
667
844
|
|
668
|
-
|
845
|
+
describe 'when all items are selected' do
|
846
|
+
it 'should return a bihash with items selected by the block' do
|
669
847
|
bh = Bihash[2 => :two, 4 => :four, 6 => :six, 8 => :eight]
|
670
848
|
bh.select { |k1,k2| k1.even? }.must_equal bh
|
671
849
|
bh.must_equal bh
|
@@ -689,7 +867,7 @@ describe Bihash do
|
|
689
867
|
|
690
868
|
it 'should return nil if no changes were made to the bihash' do
|
691
869
|
bh = Bihash[1 => :one, 2 => :two, 3 => :three, 4 => :four]
|
692
|
-
bh.select! { |key1, key2| key1 < 5 }.
|
870
|
+
bh.select! { |key1, key2| key1 < 5 }.must_be_nil
|
693
871
|
bh.must_equal Bihash[1 => :one, 2 => :two, 3 => :three, 4 => :four]
|
694
872
|
end
|
695
873
|
|
@@ -712,7 +890,7 @@ describe Bihash do
|
|
712
890
|
end
|
713
891
|
|
714
892
|
it 'should return the default value if bihash is empty' do
|
715
|
-
Bihash.new.shift.
|
893
|
+
Bihash.new.shift.must_be_nil
|
716
894
|
Bihash.new(404).shift.must_equal 404
|
717
895
|
Bihash.new { 'd3f4u17' }.shift.must_equal 'd3f4u17'
|
718
896
|
end
|
@@ -736,6 +914,21 @@ describe Bihash do
|
|
736
914
|
h.delete(:key1)
|
737
915
|
bh.must_include :key1
|
738
916
|
end
|
917
|
+
|
918
|
+
it 'should be an alias of #to_hash' do
|
919
|
+
bh = Bihash.new
|
920
|
+
bh.method(:to_hash).must_equal bh.method(:to_h)
|
921
|
+
end
|
922
|
+
end
|
923
|
+
|
924
|
+
describe '#to_proc' do
|
925
|
+
it 'should convert the bihash to a proc' do
|
926
|
+
Bihash[].to_proc.must_be_instance_of Proc
|
927
|
+
end
|
928
|
+
|
929
|
+
it 'should call #[] on the bihash when the proc is called' do
|
930
|
+
Bihash[:key => 'value'].to_proc.call(:key).must_equal 'value'
|
931
|
+
end
|
739
932
|
end
|
740
933
|
|
741
934
|
describe '#to_s' do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bihash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cohen Carlisle
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- bin/console
|
85
85
|
- bin/setup
|
86
86
|
- lib/bihash.rb
|
87
|
+
- lib/bihash/unimplemented_methods.rb
|
87
88
|
- lib/bihash/version.rb
|
88
89
|
- spec/bihash_spec.rb
|
89
90
|
- spec/spec_helper.rb
|
@@ -107,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
108
|
version: '0'
|
108
109
|
requirements: []
|
109
110
|
rubyforge_project:
|
110
|
-
rubygems_version: 2.5.
|
111
|
+
rubygems_version: 2.5.2
|
111
112
|
signing_key:
|
112
113
|
specification_version: 4
|
113
114
|
summary: Bidirectional Hash
|