enumerate_hash_values 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +1,3 @@
1
- require "enumerate_hash_values/version"
2
- require "enumerate_hash_values/core_extensions"
1
+ require 'enumerate_hash_values/version'
2
+ require 'enumerate_hash_values/core_extensions/enumerable'
3
+ require 'enumerate_hash_values/core_extensions/hash'
@@ -0,0 +1,50 @@
1
+ module Enumerable # :nodoc:
2
+
3
+ # Wraps the common pattern of doing:
4
+ #
5
+ # enumerable.inject({}) do |hash, item|
6
+ # some_key = item.key
7
+ # some_value = item.value
8
+ # hash[some_key] = some_value
9
+ # hash
10
+ # end
11
+ #
12
+ # Using <tt>collect_to_hash</tt> instead:
13
+ #
14
+ # enumerable.collect_to_hash do |item|
15
+ # some_key = item.key
16
+ # some_value = item.value
17
+ # [some_key, some_value]
18
+ # end
19
+ #
20
+ # Examples:
21
+ #
22
+ # (1..3).collect_to_hash { |number| [number, number * 10] } # => { 1 => 10, 2 = > 20, 3 => 30 }
23
+ # %w(a b c).map_to_hash { |letter| [letter, letter[0]] } # => { 'a' => 97, 'b' => 98, 'c' => 99 }
24
+ #
25
+ # Aliases <tt>map_to_hash</tt>
26
+ def collect_to_hash(hash_class = Hash)
27
+ raise ArguementError.new("#{hash_class} is not a Hash") unless hash_class.ancestors.include?(Hash)
28
+
29
+ inject(hash_class.new) do |hash, item|
30
+ key, value = yield(item)
31
+ hash[key] = value
32
+ hash
33
+ end
34
+ end
35
+ alias_method :map_to_hash, :collect_to_hash
36
+
37
+ # Similar to <tt>collect_to_hash</tt>, but the specified block only needs to
38
+ # return the hash value for each enumerated item. The items themselves will
39
+ # serve as the hash keys.
40
+ #
41
+ # (1..3).collect_to_hash_values { |number| number * 10 } # => { 1 => 10, 2 = > 20, 3 => 30 }
42
+ # %w(a b c).map_to_hash_values(&:to_sym) # => { 'a' => :a, 'b' => :b, 'c' => :c }
43
+ #
44
+ # Aliases <tt>map_to_hash_values</tt>
45
+ def collect_to_hash_values(hash_class = Hash)
46
+ collect_to_hash(hash_class) { |item| [item, yield(item)] }
47
+ end
48
+ alias_method :map_to_hash_values, :collect_to_hash_values
49
+
50
+ end
@@ -1,4 +1,4 @@
1
- class Hash
1
+ class Hash # :nodoc:
2
2
 
3
3
  # Define values and values_with_keys versions of each method.
4
4
  {
@@ -1,3 +1,3 @@
1
1
  module EnumerateHashValues
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerate_hash_values
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Clyde Law
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-30 00:00:00 -08:00
18
+ date: 2012-02-21 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -35,7 +35,8 @@ files:
35
35
  - Rakefile
36
36
  - enumerate_hash_values.gemspec
37
37
  - lib/enumerate_hash_values.rb
38
- - lib/enumerate_hash_values/core_extensions.rb
38
+ - lib/enumerate_hash_values/core_extensions/enumerable.rb
39
+ - lib/enumerate_hash_values/core_extensions/hash.rb
39
40
  - lib/enumerate_hash_values/version.rb
40
41
  has_rdoc: true
41
42
  homepage: http://github.com/Umofomia/enumerate_hash_values