hash-polyfill 0.1.20170629

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d867362eafcd7f18aaca64f5681913c05c914422
4
+ data.tar.gz: 39f2ae9706458974ef5918019b01b1c929987548
5
+ SHA512:
6
+ metadata.gz: 7e0ab771ba9e87ac863daeba2ee849323e444a5fbdf3b98fd1e80a57c8488d23562aa1345c09ff1e895da2d36d8f294389985cbbe4fc47b62c7848f5dbda29fe
7
+ data.tar.gz: 9463ed9777a5b319239b839222cf9f265ea4e37070e3897a135bfa78932ea61552191ad5f618374b387b324bb55194a811d513f3adbc76b0108175581df1243e
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ Implements some `Hash` methods which ruby will undoubtedly add in the future, possibly with different names.
2
+
3
+ ## Usage
4
+
5
+ ```ruby
6
+ a = {a: 1, b: 2, c: 3, d: 4}
7
+
8
+ a.select_values(&even?) # => {b: 2, d: 4}
9
+ a.reject_values(&even?) # => {a: 1, c: 3}
10
+ a.transform_values{|v| v * 10} # => {a: 10, b: 20, c: 30, d: 40}
11
+
12
+ b = {a: 1, b: 2, c: nil, d: false}
13
+ b.compact # => {a: 1, b: 2, d: false}
14
+ ```
15
+
16
+ The gem only adds the methods if they don't already exist. In particular if you're using ruby 2.4, `Hash#transform_values` is already provided.
17
+
18
+ There are also `!` versions of all these methods.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ task "default" => "spec"
2
+ task "test" => "spec"
3
+
4
+ desc "Run tests"
5
+ task "spec" do
6
+ system "rspec"
7
+ end
8
+
9
+ desc "Clean up"
10
+ task "clean" do
11
+ system "trash hash-polyfill-*.gem"
12
+ end
13
+
14
+ desc "Build gem"
15
+ task "gem:build" do
16
+ system "gem build hash-polyfill.gemspec"
17
+ end
18
+
19
+ desc "Upload gem"
20
+ task "gem:push" => "gem:build" do
21
+ gem_file = Dir["hash-polyfill-*.gem"][-1] or raise "No gem found"
22
+ system "gem", "push", gem_file
23
+ end
@@ -0,0 +1,58 @@
1
+ class Hash
2
+ def compact
3
+ reject do |key, value|
4
+ value.nil?
5
+ end
6
+ end unless method_defined? :compact
7
+
8
+ def compact!
9
+ reject! do |key, value|
10
+ value.nil?
11
+ end
12
+ end unless method_defined? :compact!
13
+
14
+ def select_values
15
+ return enum_for(:select_values) unless block_given?
16
+ select do |key, value|
17
+ yield(value)
18
+ end
19
+ end unless method_defined? :select_values
20
+
21
+ def select_values!
22
+ return enum_for(:select_values!) unless block_given?
23
+ select! do |key, value|
24
+ yield(value)
25
+ end
26
+ end unless method_defined? :select_values!
27
+
28
+ def reject_values
29
+ return enum_for(:reject_values) unless block_given?
30
+ reject do |key, value|
31
+ yield(value)
32
+ end
33
+ end unless method_defined? :reject_values
34
+
35
+ def reject_values!
36
+ return enum_for(:reject_values!) unless block_given?
37
+ reject! do |key, value|
38
+ yield(value)
39
+ end
40
+ end unless method_defined? :reject_values!
41
+
42
+ def transform_values
43
+ return enum_for(:transform_values) unless block_given?
44
+ result = {}
45
+ each do |key, value|
46
+ result[key] = yield(value)
47
+ end
48
+ result
49
+ end unless method_defined? :transform_values
50
+
51
+ def transform_values!
52
+ return enum_for(:transform_values!) unless block_given?
53
+ each do |key, value|
54
+ self[key] = yield(value)
55
+ end
56
+ self
57
+ end unless method_defined? :transform_values!
58
+ end
@@ -0,0 +1,116 @@
1
+ describe "Hash polyfill" do
2
+ it "compact" do
3
+ a = {x: true, y: nil, z: false}
4
+ b = a.compact
5
+ expect(a).to eq({x: true, y: nil, z: false})
6
+ expect(b).to eq({x: true, z: false})
7
+ end
8
+
9
+ it "compact!" do
10
+ a = {x: true, y: nil, z: false}
11
+ expect(a.compact!).to eq(a)
12
+ expect(a.compact!).to eq(nil)
13
+ expect(a).to eq({x: true, z: false})
14
+ end
15
+
16
+ it "select_values - with block" do
17
+ a = {a: 2, b: 3, c: 4, d: 5}
18
+ b = a.select_values(&:even?)
19
+ c = a.select_values(&:odd?)
20
+ expect(a).to eq({a: 2, b: 3, c: 4, d: 5})
21
+ expect(b).to eq({a: 2, c: 4})
22
+ expect(c).to eq({b: 3, d: 5})
23
+ end
24
+
25
+ it "select_values! - with block" do
26
+ a = {a: 2, b: 3, c: 4, d: 5}
27
+ expect(a.select_values!(&:even?)).to eq(a)
28
+ expect(a.select_values!(&:even?)).to eq(nil)
29
+ expect(a).to eq({a: 2, c: 4})
30
+ end
31
+
32
+ it "reject_values - with block" do
33
+ a = {a: 2, b: 3, c: 4, d: 5}
34
+ b = a.reject_values(&:even?)
35
+ c = a.reject_values(&:odd?)
36
+ expect(a).to eq({a: 2, b: 3, c: 4, d: 5})
37
+ expect(b).to eq({b: 3, d: 5})
38
+ expect(c).to eq({a: 2, c: 4})
39
+ end
40
+
41
+ it "reject_values! - with block" do
42
+ a = {a: 2, b: 3, c: 4, d: 5}
43
+ expect(a.reject_values!(&:even?)).to eq(a)
44
+ expect(a.reject_values!(&:even?)).to eq(nil)
45
+ expect(a).to eq({b: 3, d: 5})
46
+ end
47
+
48
+ # Doing anything more than .with_index or .each with them is going to be confusing
49
+ it "select_values - no block yields Enumerator" do
50
+ a = {a: 2, b: 3, c: 4, d: 5}
51
+ expect(a.select_values).to be_instance_of(Enumerator)
52
+ b = a.select_values.each(&:even?)
53
+ expect(a).to eq({a: 2, b: 3, c: 4, d: 5})
54
+ expect(b).to eq({a: 2, c: 4})
55
+ b = a.select_values.map(&:even?)
56
+ expect(b).to eq([true, false, true, false])
57
+ end
58
+
59
+ it "select_values! - no block yields Enumerator" do
60
+ a = {a: 2, b: 3, c: 4, d: 5}
61
+ expect(a.select_values!).to be_instance_of(Enumerator)
62
+ b = a.select_values!.each(&:even?)
63
+ expect(a).to eq({a: 2, c: 4})
64
+ expect(b).to eq(a)
65
+ end
66
+
67
+ it "reject_values - no block yields Enumerator" do
68
+ a = {a: 2, b: 3, c: 4, d: 5}
69
+ expect(a.reject_values).to be_instance_of(Enumerator)
70
+ b = a.reject_values.each(&:even?)
71
+ expect(a).to eq({a: 2, b: 3, c: 4, d: 5})
72
+ expect(b).to eq({b: 3, d: 5})
73
+ b = a.reject_values.map(&:even?)
74
+ expect(b).to eq([true, false, true, false])
75
+ end
76
+
77
+ it "reject_values! - no block yields Enumerator" do
78
+ a = {a: 2, b: 3, c: 4, d: 5}
79
+ expect(a.reject_values!).to be_instance_of(Enumerator)
80
+ b = a.reject_values!.each(&:even?)
81
+ expect(a).to eq({b: 3, d: 5})
82
+ expect(b).to eq(a)
83
+ end
84
+
85
+ # Ruby 2.4 polyfill
86
+ # map! always returns self, which is different from select!/reject! which return self or nil
87
+ it "transform_values - with block" do
88
+ a = {a: 2, b: 3, c: 4, d: 5}
89
+ b = a.transform_values(&:next)
90
+ expect(a).to eq({a: 2, b: 3, c: 4, d: 5})
91
+ expect(b).to eq({a: 3, b: 4, c: 5, d: 6})
92
+ end
93
+
94
+ it "transform_values! - with block" do
95
+ a = {a: 2, b: 3, c: 4, d: 5}
96
+ expect(a.transform_values!(&:next)).to eq(a)
97
+ expect(a.transform_values!(&:itself)).to eq(a)
98
+ expect(a).to eq({a: 3, b: 4, c: 5, d: 6})
99
+ end
100
+
101
+ it "transform_values - no block yields Enumerator" do
102
+ a = {a: 2, b: 3, c: 4, d: 5}
103
+ expect(a.transform_values).to be_instance_of(Enumerator)
104
+ b = a.transform_values.each(&:next)
105
+ expect(a).to eq({a: 2, b: 3, c: 4, d: 5})
106
+ expect(b).to eq({a: 3, b: 4, c: 5, d: 6})
107
+ end
108
+
109
+ it "transform_values! - no block yields Enumerator" do
110
+ a = {a: 2, b: 3, c: 4, d: 5}
111
+ expect(a.transform_values!).to be_instance_of(Enumerator)
112
+ expect(a.transform_values!.each(&:next)).to eq(a)
113
+ expect(a.transform_values!.each(&:itself)).to eq(a)
114
+ expect(a).to eq({a: 3, b: 4, c: 5, d: 6})
115
+ end
116
+ end
@@ -0,0 +1,4 @@
1
+ require_relative "../lib/hash-polyfill"
2
+
3
+ RSpec.configure do |config|
4
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash-polyfill
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.20170629
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Wegrzanowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Hash#transform_values from 2.4 and well as Hash convenience methods from
56
+ the future
57
+ email: Tomasz.Wegrzanowski@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - README.md
64
+ - Rakefile
65
+ - lib/hash-polyfill.rb
66
+ - spec/hash_polyfill_spec.rb
67
+ - spec/spec_helper.rb
68
+ homepage: https://github.com/taw/hash-zip
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.5.2
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Polyfill for Hash methods ruby will get in the future
92
+ test_files: []