with_reverse_lookup 0.0.1
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.
- data/README.textile +24 -0
- data/Rakefile +5 -0
- data/lib/with_reverse_lookup.rb +22 -0
- data/test/with_reverse_lookup_test.rb +49 -0
- metadata +70 -0
data/README.textile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
h1. With Reverse Lookup
|
2
|
+
|
3
|
+
Extends a hash to be able to look up keys by value (for bi-directional hashes).
|
4
|
+
|
5
|
+
h2. Sample
|
6
|
+
|
7
|
+
<pre>
|
8
|
+
table = {
|
9
|
+
:foo => "Foo",
|
10
|
+
:bar => "Bar",
|
11
|
+
:baz => "Baz"
|
12
|
+
}.extend(WithReverseLookup)
|
13
|
+
|
14
|
+
table[:foo] #=> "Foo"
|
15
|
+
table["Foo"] #=> :foo
|
16
|
+
</pre>
|
17
|
+
|
18
|
+
h2. License
|
19
|
+
|
20
|
+
MIT
|
21
|
+
|
22
|
+
h2. Copyright
|
23
|
+
|
24
|
+
Copyright (C) 2010 Matt Todd.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Useful for defining a hash a single way and being able to look up by key or
|
2
|
+
# value. For instance:
|
3
|
+
#
|
4
|
+
# hash = {
|
5
|
+
# 1 => :foo,
|
6
|
+
# 2 => :bar
|
7
|
+
# }.extend(WithReverseLookup)
|
8
|
+
#
|
9
|
+
# hash[1] #=> :foo
|
10
|
+
# hash[:foo] #=> 1
|
11
|
+
#
|
12
|
+
module WithReverseLookup
|
13
|
+
|
14
|
+
def [](key)
|
15
|
+
if self.keys.include?(key)
|
16
|
+
super
|
17
|
+
else
|
18
|
+
return self.index(key)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..'))
|
2
|
+
require 'lib/with_reverse_lookup'
|
3
|
+
|
4
|
+
puts "Running tests..."
|
5
|
+
puts ""
|
6
|
+
|
7
|
+
# setup test data
|
8
|
+
hash = {
|
9
|
+
1 => :yes,
|
10
|
+
0 => :no
|
11
|
+
}
|
12
|
+
|
13
|
+
puts "Test data: #{hash.inspect}"
|
14
|
+
puts ""
|
15
|
+
|
16
|
+
# extend hash
|
17
|
+
hash.extend(WithReverseLookup)
|
18
|
+
|
19
|
+
# run test 1
|
20
|
+
puts "1. Should find hash[1] as :yes"
|
21
|
+
puts hash[1] == :yes ? 'PASS' : 'FAIL'
|
22
|
+
puts ""
|
23
|
+
|
24
|
+
# run test 2
|
25
|
+
puts "2. Should find hash[0] as :no"
|
26
|
+
puts hash[0] == :no ? 'PASS' : 'FAIL'
|
27
|
+
puts ""
|
28
|
+
|
29
|
+
# run test 3
|
30
|
+
puts "3. Should find hash[2] as nil"
|
31
|
+
puts hash[2] == nil ? 'PASS' : 'FAIL'
|
32
|
+
puts ""
|
33
|
+
|
34
|
+
# run test 4
|
35
|
+
puts "4. Should find hash[:yes] as 1"
|
36
|
+
puts hash[:yes] == 1 ? 'PASS' : 'FAIL'
|
37
|
+
puts ""
|
38
|
+
|
39
|
+
# run test 5
|
40
|
+
puts "5. Should find hash[:no] as 0"
|
41
|
+
puts hash[:no] == 0 ? 'PASS' : 'FAIL'
|
42
|
+
puts ""
|
43
|
+
|
44
|
+
# run test 4
|
45
|
+
puts "4. Should find hash[:foo] as nil"
|
46
|
+
puts hash[:foo] == nil ? 'PASS' : 'FAIL'
|
47
|
+
puts ""
|
48
|
+
|
49
|
+
puts "Finished."
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: with_reverse_lookup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matt Todd
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-01 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: " Provides a simple modification to existing hashes to provide key lookup\n by value in addition to value lookup by key.\n \n For example:\n \n TABLE = { :key => 1 }.extend(WithReverseLookup)\n \n TABLE[:key] #=> 1\n TABLE[1] #=> :key\n \n This is mostly useful for lookup tables.\n"
|
23
|
+
email: mtodd@highgroove.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- README.textile
|
32
|
+
- Rakefile
|
33
|
+
- lib/with_reverse_lookup.rb
|
34
|
+
- test/with_reverse_lookup_test.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/mtodd/with_reverse_lookup
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
hash: 3
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
version: "0"
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.3.7
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Bidirectional key/value lookup for hashes
|
69
|
+
test_files: []
|
70
|
+
|