multihash 0.1.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/multihash/multihash.rb +30 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dbead59572ba7080786ff23f566971f2c5323d0d
4
+ data.tar.gz: 21d075eb849f528c9849ec17ecba9d87ca51a5e3
5
+ SHA512:
6
+ metadata.gz: 8d2aa119dabc2d5980152b50a1a7b53bfb205b51c98db9c9e67b4ebbb035969bf327b5983d053b6b5257b00f840818e0a22d301175590aeeb5d7535d75db2a92
7
+ data.tar.gz: abb7eff1d113406685f45e47fade2b826a6a2118cd244e94796c04b4479d59990ffcee0ecca4e44742f61715037dc1d617aa56908f0e86ad0e0763a1c98ad99e
@@ -0,0 +1,30 @@
1
+ # This class provides a very simple interface for a hash with multiple keys
2
+ # that reference the same value. This is convenient for lookup tables that have
3
+ # several mappings to a single value, without doing anything particularly
4
+ # gross with Array#include? lookups.
5
+ class MultiHash
6
+ # Initialize an immutable multihash with the provided hash as its source.
7
+ # For example, we might want to look up the nation which produces different
8
+ # types of cars:
9
+ #
10
+ # MultiHash.new (
11
+ # %w{Honda Nissan} => :japan,
12
+ # %w{Ford Chrysler} => :us,
13
+ # 'BMW' => :germany
14
+ # )
15
+ #
16
+ # @param original_hash [Hash<Array|Object, Object>] the hash to convert into
17
+ # a multihash, with arrays or single objects as keys.
18
+ def initialize(original_hash)
19
+ @_hash = Hash.new
20
+
21
+ original_hash.each do |orig_key, value|
22
+ key_array = orig_key.is_a?(Array) && orig_key || [orig_key]
23
+ key_array.each { |k| @_hash[k] = value }
24
+ end
25
+ end
26
+
27
+ def [](key)
28
+ @_hash[key]
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multihash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Blake Hyde
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: MultiHash allows multiple keys to the same value, concisely.
14
+ email: syrion@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/multihash/multihash.rb
20
+ homepage: https://github.com/asthasr/multihash
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: A many-to-one, immutable hash.
44
+ test_files: []