bihash 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -2
- data/README.md +14 -0
- data/lib/bihash/unimplemented_methods.rb +6 -1
- data/lib/bihash/version.rb +1 -1
- data/lib/bihash.rb +8 -8
- data/spec/bihash_spec.rb +13 -43
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 267f9af4575dbcca9216e1c780c6d5bd91f291be
|
4
|
+
data.tar.gz: 249056135bb1a35e4f90c7b4f6453f317dfc410a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fb2d4c5807667407e8feff5f189e52ee899900cb1c85d025ebb25b439d570f8f0b60530bfd1b13fa9bb45f8c2c1f0d49e9ae4ac4648af16421aedb24bb2b2e9
|
7
|
+
data.tar.gz: dd4c378857235311be4573879a1aea3932e73ff55edf40052bbdb6e3c8c5ee2f63170677a88fb5eb5b0c84347e2cbb19bb9b58282a8c2099ab769456b292c568
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/Cohen-Carlisle/bihash.svg?branch=master)](https://travis-ci.org/Cohen-Carlisle/bihash)
|
1
2
|
# Bihash
|
2
3
|
|
3
4
|
A simple gem that implements a bidrectional hash
|
@@ -6,6 +7,19 @@ A simple gem that implements a bidrectional hash
|
|
6
7
|
|
7
8
|
Use as a hash, except that keys and values are interchangeable.
|
8
9
|
|
10
|
+
```ruby
|
11
|
+
abbreviations = Bihash["abbr" => "abbreviation"]
|
12
|
+
# => Bihash["abbr"=>"abbreviation"]
|
13
|
+
abbreviations["lol"] = "laugh out loud"
|
14
|
+
# => "laugh out loud"
|
15
|
+
puts abbreviations
|
16
|
+
# => Bihash["abbr"=>"abbreviation", "lol"=>"laugh out loud"]
|
17
|
+
abbreviations["lol"]
|
18
|
+
# => "laugh out loud"
|
19
|
+
abbreviations["laugh out loud"]
|
20
|
+
# => "lol"
|
21
|
+
```
|
22
|
+
|
9
23
|
## Development
|
10
24
|
|
11
25
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
@@ -7,6 +7,8 @@ class Bihash
|
|
7
7
|
'values',
|
8
8
|
'each_key',
|
9
9
|
'each_value',
|
10
|
+
'transform_keys',
|
11
|
+
'transform_keys!',
|
10
12
|
'transform_values',
|
11
13
|
'transform_values!',
|
12
14
|
# O(n) reverse lookups
|
@@ -16,7 +18,10 @@ class Bihash
|
|
16
18
|
'value?',
|
17
19
|
'has_value?',
|
18
20
|
# meaningless on bihash as both sides already hashed
|
19
|
-
'invert'
|
21
|
+
'invert',
|
22
|
+
# mass removal of nil, but a bihash can have only one pair containing nil
|
23
|
+
'compact',
|
24
|
+
'compact!'
|
20
25
|
]
|
21
26
|
|
22
27
|
def respond_to?(method, private = false)
|
data/lib/bihash/version.rb
CHANGED
data/lib/bihash.rb
CHANGED
@@ -68,14 +68,6 @@ class Bihash
|
|
68
68
|
self
|
69
69
|
end
|
70
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
|
-
|
79
71
|
def compare_by_identity
|
80
72
|
raise_error_if_frozen
|
81
73
|
@forward.compare_by_identity
|
@@ -279,6 +271,14 @@ class Bihash
|
|
279
271
|
|
280
272
|
def_delegator :@forward, :size
|
281
273
|
|
274
|
+
def slice(*args)
|
275
|
+
self.class.new.tap do |bh|
|
276
|
+
args.each do |arg|
|
277
|
+
bh[arg] = self[arg] if key?(arg)
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
282
|
alias :store :[]=
|
283
283
|
|
284
284
|
def to_h
|
data/spec/bihash_spec.rb
CHANGED
@@ -300,49 +300,6 @@ describe Bihash do
|
|
300
300
|
end
|
301
301
|
end
|
302
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
|
343
|
-
end
|
344
|
-
end
|
345
|
-
|
346
303
|
describe '#clear' do
|
347
304
|
it 'should remove all pairs and return the bihash' do
|
348
305
|
bh = Bihash[:key => 'value']
|
@@ -906,6 +863,19 @@ describe Bihash do
|
|
906
863
|
end
|
907
864
|
end
|
908
865
|
|
866
|
+
describe '#slice' do
|
867
|
+
it 'should return a new bihash with only the pairs that are in the args' do
|
868
|
+
bh = Bihash[1 => :one, 2 => :two, 3 => :three]
|
869
|
+
bh.slice(1, :one, :two, "nope").must_equal Bihash[1 => :one, 2 => :two]
|
870
|
+
bh.must_equal Bihash[1 => :one, 2 => :two, 3 => :three]
|
871
|
+
end
|
872
|
+
|
873
|
+
it 'should return a vanilla bihash without default values, etc.' do
|
874
|
+
sliced_bh = Bihash.new(404).slice
|
875
|
+
sliced_bh.default.must_be_nil
|
876
|
+
end
|
877
|
+
end
|
878
|
+
|
909
879
|
describe '#to_h' do
|
910
880
|
it 'should return a copy of the forward hash' do
|
911
881
|
bh = Bihash[:key1 => 'val1', :key2 => 'val2']
|
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: 1.
|
4
|
+
version: 1.1.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: 2018-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
108
|
version: '0'
|
109
109
|
requirements: []
|
110
110
|
rubyforge_project:
|
111
|
-
rubygems_version: 2.5.2
|
111
|
+
rubygems_version: 2.5.2.2
|
112
112
|
signing_key:
|
113
113
|
specification_version: 4
|
114
114
|
summary: Bidirectional Hash
|