keyword_curry 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a54423b4a5b808a084833e30c8f50d31aa8ccb11
4
+ data.tar.gz: 3a166e85ec33b095389a1f6ee441751edcfa4f63
5
+ SHA512:
6
+ metadata.gz: 79fe88752ad3916ea19c8a8ce5942306eaf5cf801a820f74981e95a615d4b20b6438c7ee8eecd229e066d8319e69cb8b7327f09db165b3e8695207e14cc37358
7
+ data.tar.gz: efcc0192d88858ff6b6ed41048d0be3b6603bacc4f2adc34c29ab9acdf63bbf113f3ef12c548c1c4f9b8a885862d966edc728a3e6a37447fed7d8c932d287fd0
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1 @@
1
+ 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in keyword_curry.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Stephen Best
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ # KeywordCurry
2
+
3
+ Augments Ruby currying to handle MRI 2.1 required keyword arguments.
4
+
5
+ Proc like objects can be curried until all their required keywords have been received
6
+
7
+ Please note **monkey patching is optional**
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'keyword_curry'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install keyword_curry
22
+
23
+ ## Usage
24
+
25
+ ### Mnkey patching is optional
26
+
27
+ This gem merely adds a couple of modules and it's up to you whether you would
28
+ like all of your Procs monkey patched.
29
+
30
+ The monkey patch version
31
+ ```ruby
32
+ require "keyword_curry"
33
+
34
+ Proc.prepend(KeywordCurry::KeywordArgumentCurrying)
35
+ ```
36
+
37
+ You may wish for this behaviour only on special Procs
38
+ ```ruby
39
+ require "keyword_curry"
40
+
41
+ class SpecialProc < Proc
42
+ include KeywordCurry::KeywordArgumentCurrying
43
+
44
+ # ...
45
+ end
46
+ ```
47
+
48
+ ### Currying Examples
49
+
50
+ ```ruby
51
+ ```
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'keyword_curry/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "keyword_curry"
8
+ spec.version = KeywordCurry::VERSION
9
+ spec.authors = ["Stephen Best"]
10
+ spec.email = ["bestie@gmail.com"]
11
+ spec.summary = %q{Augments Ruby currying to handle required keyword arguments. Proc style objects can be curried until all its required keywords have been received}
12
+ spec.description = spec.summary + %q{Proc like objects can be curried until all their required keywords have been received}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.required_ruby_version = "2.1.0"
17
+
18
+ spec.files = `git ls-files`.split($/)
19
+ spec.executables = []
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rspec", "~> 2.14"
25
+ end
@@ -0,0 +1,5 @@
1
+ require "keyword_curry/version"
2
+
3
+ module KeywordCurry
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,54 @@
1
+ module KeywordArgumentCurrying
2
+ def curry(arity = arity)
3
+ required_keywords = parameters
4
+ .select { |param| param.first == :keyreq }
5
+ .map { |param| param.last }
6
+
7
+ if required_keywords.empty?
8
+ super
9
+ else
10
+ curried_proc = self
11
+ collected_positional_args = []
12
+ collected_keyword_args = {}
13
+
14
+ keyword_currier = Proc.new { |keywords|
15
+ curried_proc.call(keywords) unless keywords.is_a?(Hash)
16
+
17
+ collected_keyword_args.merge!(keywords)
18
+
19
+ outstanding_keywords = required_keywords - collected_keyword_args.keys
20
+
21
+ if outstanding_keywords.empty?
22
+ curried_proc.call(*collected_positional_args, collected_keyword_args)
23
+ else
24
+ keyword_currier
25
+ end
26
+ }
27
+
28
+ positional_argument_handler = Proc.new { |*args|
29
+ if arity == -1
30
+ keyword_args = args.last if args.last.is_a?(Hash)
31
+
32
+ if keyword_args
33
+ positional_args = args[0..-2]
34
+ else
35
+ positional_args = args
36
+ keyword_args = {}
37
+ end
38
+ else
39
+ positional_args = args.take(curried_proc.arity)
40
+ keyword_args = args[curried_proc.arity..-1].first || {}
41
+ end
42
+
43
+ collected_positional_args += positional_args
44
+ keyword_currier.call(keyword_args)
45
+ }
46
+
47
+ if arity == 0
48
+ keyword_currier
49
+ else
50
+ positional_argument_handler.curry(arity)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module KeywordCurry
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,308 @@
1
+ require 'spec_helper'
2
+
3
+ require 'keyword_curry/keyword_argument_currying'
4
+
5
+ describe KeywordArgumentCurrying do
6
+ before(:all) {
7
+ Proc.prepend(KeywordArgumentCurrying)
8
+ }
9
+
10
+ describe "#curry" do
11
+ let(:spy) {
12
+ double(:spy, :proc_was_called => "return value")
13
+ }
14
+
15
+ let(:proc_return_value) { double(:proc_return_value) }
16
+
17
+ let(:pos1) { double(:pos1) }
18
+ let(:pos2) { double(:pos2) }
19
+ let(:key1) { double(:key1) }
20
+ let(:key2) { double(:key2) }
21
+
22
+ context "with positional args" do
23
+ let(:proc_to_curry) {
24
+ Proc.new { |pos1, pos2|
25
+ spy.proc_was_called(pos1, pos2)
26
+ proc_return_value
27
+ }
28
+ }
29
+
30
+ context "when less then the required args have been passed" do
31
+ it "does not call the proc_to_curry" do
32
+ proc_to_curry.curry.call(pos1)
33
+
34
+ expect(spy).not_to have_received(:proc_was_called)
35
+ end
36
+
37
+ it "returns another proc_to_curry" do
38
+ curried = proc_to_curry.curry.call(pos1)
39
+
40
+ expect(curried).to be_a Proc
41
+ end
42
+ end
43
+
44
+ context "when all required arguments are passed together" do
45
+ it "immediately calls the proc_to_curry" do
46
+ proc_to_curry.curry.call(pos1, pos2)
47
+
48
+ expect(spy).to have_received(:proc_was_called).with(pos1, pos2)
49
+ end
50
+
51
+ it "returns the proc_to_curry's return value" do
52
+ return_value = proc_to_curry.curry.call(pos1, pos2)
53
+
54
+ expect(return_value).to be proc_return_value
55
+ end
56
+ end
57
+
58
+ context "when all required args are passed separately" do
59
+ it "calls the proc_to_curry" do
60
+ proc_to_curry.curry
61
+ .call(pos1)
62
+ .call(pos2)
63
+
64
+ expect(spy).to have_received(:proc_was_called).with(pos1, pos2)
65
+ end
66
+
67
+ it "returns the proc_to_curry's return value" do
68
+ return_value = proc_to_curry.curry
69
+ .call(pos1)
70
+ .call(pos2)
71
+
72
+ expect(return_value).to be proc_return_value
73
+ end
74
+ end
75
+ end
76
+
77
+ context "with keyword arguments" do
78
+ context "with only optional keyword arguments" do
79
+ let(:proc_to_curry) {
80
+ Proc.new { |key1: nil|
81
+ spy.proc_was_called(key1: key1)
82
+ proc_return_value
83
+ }
84
+ }
85
+
86
+ it "immediately calls the proc_to_curry" do
87
+ proc_to_curry.curry.call(key1: key1)
88
+
89
+ expect(spy).to have_received(:proc_was_called).with(key1: key1)
90
+ end
91
+
92
+ it "returns the proc_to_curry's return value" do
93
+ return_value = proc_to_curry.curry.call(key1: key1)
94
+
95
+ expect(return_value).to be proc_return_value
96
+ end
97
+ end
98
+
99
+ context "with required keyword arguments" do
100
+ let(:proc_to_curry) {
101
+ Proc.new { |key1:, key2:|
102
+ spy.proc_was_called(key1: key1, key2: key2)
103
+ proc_return_value
104
+ }
105
+ }
106
+
107
+ context "before all required keywords are received" do
108
+ it "does not call the proc_to_curry" do
109
+ proc_to_curry.curry.call(key1: key1)
110
+
111
+ expect(spy).not_to have_received(:proc_was_called)
112
+ end
113
+
114
+ it "returns another proc_to_curry" do
115
+ curried_proc_to_curry = proc_to_curry.curry.call(key1: key1)
116
+
117
+ expect(curried_proc_to_curry).to be_a Proc
118
+ end
119
+ end
120
+
121
+ context "when all required keywords are received together" do
122
+ it "calls the proc_to_curry with all keyword arguments" do
123
+ proc_to_curry.curry.call(key1: key1, key2: key2)
124
+
125
+ expect(spy).to have_received(:proc_was_called)
126
+ .with(key1: key1, key2: key2)
127
+ end
128
+
129
+ it "returns the proc_to_curry's return value" do
130
+ return_value = proc_to_curry.curry.call(key1: key1, key2: key2)
131
+
132
+ expect(return_value).to be proc_return_value
133
+ end
134
+ end
135
+
136
+ context "when all required args are passed separately" do
137
+ it "calls the proc_to_curry" do
138
+ proc_to_curry.curry
139
+ .call(key1: key1)
140
+ .call(key2: key2)
141
+
142
+ expect(spy).to have_received(:proc_was_called)
143
+ .with(key1: key1, key2: key2)
144
+ end
145
+
146
+ it "returns the proc_to_curry's return value" do
147
+ return_value = proc_to_curry.curry
148
+ .call(key1: key1)
149
+ .call(key2: key2)
150
+
151
+ expect(return_value).to be proc_return_value
152
+ end
153
+ end
154
+ end
155
+ end
156
+
157
+ context "with mixed positional and keyword args" do
158
+ let(:proc_to_curry) {
159
+ Proc.new { |pos1, pos2, key1:, key2:|
160
+ spy.proc_was_called(pos1, pos2, key1: key1, key2: key2)
161
+ proc_return_value
162
+ }
163
+ }
164
+
165
+ let(:pos1) { double(:pos1) }
166
+ let(:pos2) { double(:pos2) }
167
+ let(:key1) { double(:key1) }
168
+ let(:key2) { double(:key2) }
169
+
170
+ context "when all args are passed together" do
171
+ it "immediately calls the proc_to_curry" do
172
+ proc_to_curry.curry
173
+ .call(pos1, pos2, key1: key1, key2: key2)
174
+
175
+ expect(spy).to have_received(:proc_was_called)
176
+ .with(pos1, pos2, key1: key1, key2: key2)
177
+ end
178
+
179
+ it "returns the proc_to_curry's return value" do
180
+ return_value = proc_to_curry.curry
181
+ .call(pos1, pos2, key1: key1, key2: key2)
182
+
183
+ expect(return_value).to be proc_return_value
184
+ end
185
+ end
186
+
187
+ context "when all positional arguments are passed" do
188
+ context "but no keywords are passed" do
189
+ it "does not call the proc_to_curry" do
190
+ proc_to_curry.curry.call(pos1, pos2)
191
+
192
+ expect(spy).not_to have_received(:proc_was_called)
193
+ end
194
+
195
+ it "returns another proc_to_curry" do
196
+ curried_proc_to_curry = proc_to_curry.curry.call(pos1, pos2)
197
+
198
+ expect(curried_proc_to_curry).to be_a Proc
199
+ end
200
+ end
201
+
202
+ context "before all required keywords have been received" do
203
+ it "does not call the proc_to_curry" do
204
+ proc_to_curry.curry
205
+ .call(pos1, pos2)
206
+ .call(key1: key1)
207
+
208
+ expect(spy).not_to have_received(:proc_was_called)
209
+ end
210
+
211
+ it "returns another proc_to_curry" do
212
+ curried_proc_to_curry = proc_to_curry.curry
213
+ .call(pos1, pos2)
214
+ .call(key1: key1)
215
+
216
+ expect(curried_proc_to_curry).to be_a Proc
217
+ end
218
+ end
219
+
220
+ context "and all required keywords have been received" do
221
+ it "calls the proc_to_curry" do
222
+ proc_to_curry.curry
223
+ .call(pos1, pos2)
224
+ .call(key1: key1)
225
+
226
+ expect(spy).not_to have_received(:proc_was_called)
227
+ end
228
+
229
+ it "returns the proc_to_curry's return value" do
230
+ return_value = curried_proc_to_curry = proc_to_curry.curry
231
+ .call(pos1, pos2)
232
+ .call(key1: key1)
233
+ .call(key2: key2)
234
+
235
+ expect(return_value).to be proc_return_value
236
+ end
237
+ end
238
+ end
239
+ end
240
+
241
+ context "with splatted positional arguments and keyword arguments" do
242
+ let(:proc_to_curry) {
243
+ Proc.new { |*positional_args, key1:, key2:|
244
+ spy.proc_was_called(*positional_args, key1: key1, key2: key2)
245
+ proc_return_value
246
+ }
247
+ }
248
+
249
+ it "only accepts positional args on the first call" do
250
+ curred_with_pos1 = proc_to_curry.curry.call(pos1)
251
+
252
+ expect {
253
+ curred_with_pos1.call(pos2)
254
+ }.to raise_error(/missing keywords: key1, key2/)
255
+ end
256
+
257
+ it "will accept any number of positional arguemnts on the first call" do
258
+ curried_with_positional_args = proc_to_curry.curry.call(
259
+ pos1, pos2, "something else"
260
+ )
261
+
262
+ curried_with_positional_args.call(key1: key1, key2: key2)
263
+
264
+ expect(spy).to have_received(:proc_was_called)
265
+ .with(pos1, pos2, "something else", key1: key1, key2: key2)
266
+ end
267
+
268
+ it "can curry keywords with the first call" do
269
+ curred_with_pos1_key1 = proc_to_curry.curry.call(pos1, key1: key1)
270
+
271
+ curred_with_pos1_key1.call(key2: key2)
272
+
273
+ expect(spy).to have_received(:proc_was_called)
274
+ .with(pos1, key1: key1, key2: key2)
275
+ end
276
+
277
+ it "can curry keywords after first call" do
278
+ curred_with_pos1 = proc_to_curry.curry.call(pos1)
279
+
280
+ curred_with_pos1
281
+ .call(key1: key1)
282
+ .call(key2: key2)
283
+
284
+ expect(spy).to have_received(:proc_was_called)
285
+ .with(pos1, key1: key1, key2: key2)
286
+ end
287
+
288
+ context "when two positional and all keyword arguments are passed together" do
289
+ it "immediately calls the proc with all positional and keyword arguments" do
290
+ proc_to_curry.curry
291
+ .call(pos1, pos2, key1: key1, key2: key2)
292
+
293
+ expect(spy).to have_received(:proc_was_called)
294
+ .with(pos1, pos2, key1: key1, key2: key2)
295
+ end
296
+ end
297
+
298
+ context "when two positional and all keyword arguments are passed together" do
299
+ it "immediately calls the proc with all positional and keyword arguments" do
300
+ proc_to_curry.curry
301
+ .call(pos1, pos2, key1: key1)
302
+
303
+ expect(spy).not_to have_received(:proc_was_called)
304
+ end
305
+ end
306
+ end
307
+ end
308
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keyword_curry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Best
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.14'
41
+ description: Augments Ruby currying to handle required keyword arguments. Proc style
42
+ objects can be curried until all its required keywords have been receivedProc like
43
+ objects can be curried until all their required keywords have been received
44
+ email:
45
+ - bestie@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - ".rspec"
52
+ - ".ruby-version"
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - keyword_curry.gemspec
58
+ - lib/keyword_curry.rb
59
+ - lib/keyword_curry/keyword_argument_currying.rb
60
+ - lib/keyword_curry/version.rb
61
+ - spec/lib/keyword_curry/hash_merge_currying_spec.rb
62
+ - spec/lib/keyword_curry/keyword_argument_currying_spec.rb
63
+ - spec/spec_helper.rb
64
+ homepage: ''
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '='
75
+ - !ruby/object:Gem::Version
76
+ version: 2.1.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.1.9
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Augments Ruby currying to handle required keyword arguments. Proc style objects
88
+ can be curried until all its required keywords have been received
89
+ test_files:
90
+ - spec/lib/keyword_curry/hash_merge_currying_spec.rb
91
+ - spec/lib/keyword_curry/keyword_argument_currying_spec.rb
92
+ - spec/spec_helper.rb