enumerate_hash_values 0.1.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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.rdoc +6 -0
- data/Rakefile +1 -0
- data/enumerate_hash_values.gemspec +21 -0
- data/lib/enumerate_hash_values/core_extensions.rb +93 -0
- data/lib/enumerate_hash_values/version.rb +3 -0
- data/lib/enumerate_hash_values.rb +2 -0
- metadata +75 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "enumerate_hash_values/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "enumerate_hash_values"
|
7
|
+
s.version = EnumerateHashValues::VERSION
|
8
|
+
s.authors = ["Clyde Law"]
|
9
|
+
s.email = ["claw@alum.mit.edu"]
|
10
|
+
s.homepage = %q{http://github.com/Umofomia/enumerate_hash_values}
|
11
|
+
s.summary = %q{Defines higher-order functions available in Enumerable that work over a Hash's values and return a Hash.}
|
12
|
+
s.description = %q{Defines higher-order functions available in Enumerable that work over a Hash's values and return a Hash.}
|
13
|
+
s.license = 'MIT'
|
14
|
+
|
15
|
+
s.rubyforge_project = "enumerate_hash_values"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
# Define values and values_with_keys versions of each method.
|
4
|
+
{
|
5
|
+
'values' => 'yield(value)',
|
6
|
+
'values_with_keys' => 'yield(key, value)'
|
7
|
+
}.each do |method_suffix, yield_call|
|
8
|
+
|
9
|
+
class_eval <<-ENUMERATE_ON_VALUES_METHODS, __FILE__, __LINE__ + 1
|
10
|
+
|
11
|
+
def collect_#{method_suffix}
|
12
|
+
inject_into_empty do |hash, (key, value)|
|
13
|
+
hash[key] = #{yield_call}
|
14
|
+
hash
|
15
|
+
end
|
16
|
+
end
|
17
|
+
alias_method :map_#{method_suffix}, :collect_#{method_suffix}
|
18
|
+
|
19
|
+
def collect_#{method_suffix}!
|
20
|
+
each do |key, value|
|
21
|
+
self[key] = #{yield_call}
|
22
|
+
end
|
23
|
+
self
|
24
|
+
end
|
25
|
+
alias_method :map_#{method_suffix}!, :collect_#{method_suffix}!
|
26
|
+
|
27
|
+
def reject_#{method_suffix}
|
28
|
+
inject_into_empty do |hash, (key, value)|
|
29
|
+
hash[key] = value unless #{yield_call}
|
30
|
+
hash
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def reject_#{method_suffix}!
|
35
|
+
delete_if { |key, value| #{yield_call} }
|
36
|
+
end
|
37
|
+
|
38
|
+
def select_#{method_suffix}
|
39
|
+
reject_values_with_keys { |key, value| !#{yield_call} }
|
40
|
+
end
|
41
|
+
|
42
|
+
def select_#{method_suffix}!
|
43
|
+
reject_values_with_keys! { |key, value| !#{yield_call} }
|
44
|
+
end
|
45
|
+
|
46
|
+
ENUMERATE_ON_VALUES_METHODS
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
# Shortcut to perform a shallow copy of the hash to a new instance.
|
51
|
+
def copy
|
52
|
+
collect_values { |value| value }
|
53
|
+
end
|
54
|
+
|
55
|
+
# Copy any traits (such as defaults) from the specified hash.
|
56
|
+
def copy_traits!(hash)
|
57
|
+
self.default = hash.default
|
58
|
+
|
59
|
+
# Unfortunately, we can only transfer Hash#default_proc in Ruby 1.9 because
|
60
|
+
# Hash#default_proc= is only defined in Ruby 1.9.
|
61
|
+
self.default_proc = hash.default_proc if respond_to?(:default_proc=)
|
62
|
+
|
63
|
+
self
|
64
|
+
end
|
65
|
+
|
66
|
+
# Shortcut to inject into an empty hash.
|
67
|
+
def inject_into_empty(hash_class = self.class, options = {})
|
68
|
+
raise ArgumentError.new("#{hash_class} is not a Hash") unless hash_class.ancestors.include?(Hash)
|
69
|
+
|
70
|
+
# If not explicitly specified, copy traits into the new hash if it is of
|
71
|
+
# the same class.
|
72
|
+
copy_traits = options.has_key?(:copy_traits) ? options[:copy_traits] : (hash_class == self.class)
|
73
|
+
|
74
|
+
new_hash_options = {}
|
75
|
+
new_hash_options[:traits_from] = self if copy_traits
|
76
|
+
new_hash = hash_class.new_empty(new_hash_options)
|
77
|
+
|
78
|
+
inject(new_hash) do |hash, key_value_pair|
|
79
|
+
yield hash, key_value_pair
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class << self
|
84
|
+
|
85
|
+
def new_empty(options = {})
|
86
|
+
new_hash = allocate
|
87
|
+
new_hash.copy_traits!(options[:traits_from]) if options[:traits_from]
|
88
|
+
new_hash
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enumerate_hash_values
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Clyde Law
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-01-30 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Defines higher-order functions available in Enumerable that work over a Hash's values and return a Hash.
|
23
|
+
email:
|
24
|
+
- claw@alum.mit.edu
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- enumerate_hash_values.gemspec
|
37
|
+
- lib/enumerate_hash_values.rb
|
38
|
+
- lib/enumerate_hash_values/core_extensions.rb
|
39
|
+
- lib/enumerate_hash_values/version.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/Umofomia/enumerate_hash_values
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
hash: 3
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project: enumerate_hash_values
|
70
|
+
rubygems_version: 1.6.2
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Defines higher-order functions available in Enumerable that work over a Hash's values and return a Hash.
|
74
|
+
test_files: []
|
75
|
+
|