corefines 1.8.0 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1470ef3188042ed178a8c0204e54046b3ea6a3d
4
- data.tar.gz: 6a5e024a2f6e81e59aade31d49b09e59d3be57df
3
+ metadata.gz: eac6f98f15a93c813142d0b65ce5864599e7d585
4
+ data.tar.gz: a83d450c24803a0ff5363c3f4eab79a94157cbaf
5
5
  SHA512:
6
- metadata.gz: 2d5d5921ebd1dcbd39cd5a499ef9f82a4c4847cfdabea32df8c302db256a20cc6c1a45bf62e2923e052136ebb375a7839c598b2094830da22f488e74846edf30
7
- data.tar.gz: 66952a116b6e578b37745a5da4275e1f913c397265ae3f51ea0047bb2ed963fa2b94016cce45c4d976577bfa5b907688799644f25fc39b3a3bad1cb2082d0ed1
6
+ metadata.gz: 31fba38ae1940a1902a25792db99b61a828fceffdad0f8afaebe5e4c275f55ec451c9e83df2d0b2b29868c90b2522da72c566a3922b0eb06698f5ece3a744d32
7
+ data.tar.gz: 59f91237d105623b8483882c1e3abd1c07d4134ff7211f4de3913fcc340e77724eca37eeba895fd21573a7ff97a58a0d3bca4aa82e5671d234cd63e154b4eb23
@@ -3,9 +3,15 @@
3
3
  :doc-base-url: http://www.rubydoc.info/github/jirutka/corefines/Corefines
4
4
  :issue-uri: {repo-uri}/issues
5
5
 
6
+
7
+ == 1.9.0 (2016-02-03)
8
+
9
+ * Add new refinement {doc-base-url}/Enumerable/MapBy[Enumerable#map_by].
10
+
11
+
6
12
  == 1.8.0 (2015-07-06)
7
13
 
8
- * Add new refinement {doc-base-url}/Class/descendants[Class#descendants].
14
+ * Add new refinement {doc-base-url}/Class/Descendants[Class#descendants].
9
15
 
10
16
 
11
17
  == 1.7.0 (2015-07-05)
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License
2
2
 
3
- Copyright 2015 Jakub Jirutka <jakub@jirutka.cz>.
3
+ Copyright 2015-2016 Jakub Jirutka <jakub@jirutka.cz>.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -41,12 +41,12 @@ TODO
41
41
  Add this line to your application’s Gemfile:
42
42
 
43
43
  [source]
44
- gem 'corefines', '~> 1.8'
44
+ gem 'corefines', '~> 1.9'
45
45
 
46
46
  or to your gemspec:
47
47
 
48
48
  [source]
49
- s.add_runtime_dependency 'corefines', '~> 1.8'
49
+ s.add_runtime_dependency 'corefines', '~> 1.9'
50
50
 
51
51
  and then execute:
52
52
 
@@ -138,6 +138,7 @@ Not ideal indeed, but probably the best of what we can achieve.
138
138
  * {doc-base-url}/Enumerable[Enumerable]
139
139
  ** {doc-base-url}/Enumerable/IndexBy[#index_by]
140
140
  ** {doc-base-url}/Enumerable/Many[#many?]
141
+ ** {doc-base-url}/Enumerable/MapBy[#map_by]
141
142
  ** {doc-base-url}/Enumerable/MapSend[#map_send]
142
143
  ** {doc-base-url}/Enumerable/MapTo[#map_to]
143
144
  * {doc-base-url}/Hash[Hash]
@@ -77,6 +77,45 @@ module Corefines
77
77
  end
78
78
  end
79
79
 
80
+ ##
81
+ # @!method map_by(&block)
82
+ # Converts enumerable into a Hash, iterating over each element where the
83
+ # provided block must return an array of two elements: a key and a value
84
+ # to be added into an array that corresponds to that key.
85
+ #
86
+ # It's similar to {::Enumerable#group_by}, but allows to map the value
87
+ # being added into a group.
88
+ #
89
+ # @example
90
+ # a = [1, 2, 3, 4, 5]
91
+ # a.map_by { |e| [e % 2, e + 1] }
92
+ # #=> { 0 => [3, 5], 1 => [2, 4, 6] }
93
+ #
94
+ # @example
95
+ # h = { "Lucy" => 86, "Ruby" => 98, "Drew" => 94, "Lisa" => 54 }
96
+ # h.map_by { |k, v| [score(v), k] }
97
+ # #=> { "A" => ["Ruby", "Drew"], "B" => ["Lucy"], "F" => ["Lisa"] }
98
+ #
99
+ # @yield [obj] gives each element to the block.
100
+ # @yieldreturn [Array] an array with the key and the value.
101
+ # @return [Hash]
102
+ #
103
+ module MapBy
104
+ Support.classes_including_module(::Enumerable) do |klass|
105
+
106
+ refine klass do
107
+ def map_by
108
+ res = {}
109
+ each do |e|
110
+ k, v = yield(*e)
111
+ (res[k] ||= []) << v
112
+ end
113
+ res
114
+ end
115
+ end
116
+ end
117
+ end
118
+
80
119
  ##
81
120
  # @!method map_send(method_name, *args, &block)
82
121
  # Sends a message to each element and collects the result.
@@ -1,3 +1,3 @@
1
1
  module Corefines
2
- VERSION = '1.8.0'
2
+ VERSION = '1.9.0'
3
3
  end
@@ -0,0 +1,28 @@
1
+ describe Enumerable do
2
+ using Corefines::Enumerable::map_by
3
+
4
+ describe '#map_by' do
5
+
6
+ context Array do
7
+ let(:input) { [1, 2, 3, 4, 5] }
8
+ let(:expected) { { 0 => [3, 5], 1 => [2, 4, 6] } }
9
+
10
+ it "returns hash with the elements grouped and mapped by result of the block" do
11
+ expect(input.map_by { |e| [e % 2, e + 1] }).to eq expected
12
+ end
13
+ end
14
+
15
+ context Hash do
16
+ let(:input) { { "Lucy" => 86, "Ruby" => 98, "Drew" => 94, "Lisa" => 54 } }
17
+ let(:expected) { { "A" => ["Ruby", "Drew"], "B" => ["Lucy"], "F" => ["Lisa"] } }
18
+
19
+ it "returns hash with the elements grouped and mapped by result of the block" do
20
+ expect(input.map_by { |k, v| [score(v), k] }).to eq expected
21
+ end
22
+
23
+ def score(n)
24
+ n < 70 ? 'F' : n < 80 ? 'C' : n < 90 ? 'B' : 'A'
25
+ end
26
+ end
27
+ end
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: corefines
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Jirutka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-06 00:00:00.000000000 Z
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -127,6 +127,7 @@ files:
127
127
  - spec/class/descendants_spec.rb
128
128
  - spec/enumerable/index_by_spec.rb
129
129
  - spec/enumerable/many_spec.rb
130
+ - spec/enumerable/map_by_spec.rb
130
131
  - spec/enumerable/map_send_spec.rb
131
132
  - spec/enumerable/map_to_spec.rb
132
133
  - spec/hash/compact_spec.rb
@@ -182,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
183
  version: '0'
183
184
  requirements: []
184
185
  rubyforge_project:
185
- rubygems_version: 2.4.5
186
+ rubygems_version: 2.4.5.1
186
187
  signing_key:
187
188
  specification_version: 4
188
189
  summary: A collection of refinements for Ruby core classes.