invokr 0.9.5 → 0.9.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 54217bbbc58bdaa01e35ac2136a36f5692e8c9b3
4
- data.tar.gz: 482641abbb16a1cb72e5cd4029f59ba19bf26531
5
- SHA512:
6
- metadata.gz: e966c7c7ddb73d96ea9bc7c2cf1d52d854b77c3c34d9f75001cc0c8b6807704ae9c09465a942f84b3919e707c82baa18a46beed6ca965acf9b8cf57671200596
7
- data.tar.gz: d4bc38723344baf8a3ead9501865e0f1f78c06a8f740705934698eec425ee0d812b19ab314dac51f7ef09f4e51ce3e14514939fbeccda45ddac72520abdaa3e5
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c973b1faf440edfe2f271633ac8e036aa3c5a088
4
+ data.tar.gz: 1340286f0aac16980c4fb7946e7c94d5a81ff885
5
+ SHA512:
6
+ metadata.gz: 277e09fa4dce4c7a8b465b2c9fa02bff91dc4a79f2ea7ea87660fcbce02f251208106053354aa47d93872e40378b39e16717535094983843cf9a31902ad65a39
7
+ data.tar.gz: 238f90dc85020e397ea467f167aa0284a809eafabc075a92346e953565b7394d502eb06aa91b15a7379e4c8b5796916f0a507d6993b55a337d911f0f6f9cb37b
data/README.md CHANGED
@@ -31,6 +31,14 @@ my_proc = ->|a,b| { a + b }
31
31
  Invokr.invoke proc: my_proc, with: { a: 2, b, 4 }
32
32
  ```
33
33
 
34
+ ## Using vs. With
35
+
36
+ If you supply arguments that the method doesn't know how to handle, an `Invokr::ExtraArgumentsError` is raised. This is because, in general, you can't supply extra arguments to a plain old ruby method. However, from time to time we want to be able to pass in extra arguments. You can use the `using` keyword in order to simulate the behavior of the splat (`*`) operator:
37
+
38
+ ```ruby
39
+ Invokr.invoke method: :add_transaction, on: bank_account, with: { amount: 12.34, account_id: 24, extra_arg: 'hey, there' }
40
+ ```
41
+
34
42
  ## Querying
35
43
 
36
44
  Want to investigate the arguments of a method?
@@ -59,11 +67,6 @@ end
59
67
 
60
68
  Without knowing how to parse the source code for `#my_method`, Invokr couldn't know what the default values are. And even if I brought in e.g. [ruby_parser](https://github.com/seattlerb/ruby_parser), I'd have to support lazy evaluation, for when you supply a method or constant as the default. This complexity is completely unneccessary when using keyword arguments, so I suggest using that approach for multiple defaults for now.
61
69
 
62
- ## Todo
63
-
64
- * Cleanup
65
- * Use the `Invokr::Method` object within the `Invokr::Builder`.
66
-
67
70
  ## Pre-keyword argument hash defaults
68
71
 
69
72
  Before ruby 2.x introduced keyword arguments, it was common to end your method signature with a default hash, e.g. `def my_method args = {}`. Invoker supports this by building a Hash out of all the unused arguments you passed in, and passing *that* into the optional argument.
@@ -85,6 +88,11 @@ Invokr.inject MyKlass, using: { foo: 'FOO', bar: 'BAR', baz: 'BAZ' }
85
88
 
86
89
  Even though `MyKlass` doesn't depend on `baz`, because everything it *did* need was present in the `using` Hash, Invokr was able to instantiate an instance of `MyKlass`
87
90
 
91
+ ## Todo
92
+
93
+ * Cleanup
94
+ * Use the `Invokr::Method` object within the `Invokr::Builder`.
95
+
88
96
  ## Contributing
89
97
 
90
98
  1. Fork it ( https://github.com/[my-github-username]/invokr/fork )
@@ -30,25 +30,30 @@ module Invokr
30
30
  private
31
31
 
32
32
  def invoke_method args = {}
33
- method_name, obj, hsh_args = require_arguments! args, :method, :on, :with
33
+ method_name, obj, hsh_args = require_arguments! args, :method, :on, [:with, :using]
34
+ allow_unused = args.has_key? :using
34
35
  method = obj.method method_name
35
- invocation = Builder.build method, hsh_args, args[:block]
36
+ invocation = Builder.build method, hsh_args, args[:block], allow_unused
36
37
  invocation.invoke! obj
37
38
  end
38
39
 
39
40
  def invoke_proc _proc, args = {}
40
- hsh_args = require_arguments! args, :with
41
- invocation = Builder.build _proc, hsh_args, args[:block]
41
+ hsh_args = require_arguments! args, [:with, :using]
42
+ allow_unused = args.has_key? :using
43
+ invocation = Builder.build _proc, hsh_args, args[:block], allow_unused
42
44
  obj = SimpleDelegator.new _proc
43
45
  invocation.invoke! obj
44
46
  end
45
47
 
46
48
  def require_arguments! hsh, *args
47
49
  found_args, missing_args = args.partition do |arg|
48
- hsh.has_key? arg
50
+ Array(arg).any? &hsh.method(:has_key?)
49
51
  end
50
52
  raise InputError.new missing_args unless missing_args.empty?
51
- list = found_args.map { |arg| hsh.fetch arg }
53
+ list = found_args.map do |arg|
54
+ pair = hsh.detect do |k,_| Array(arg).include? k end
55
+ pair.fetch 1
56
+ end
52
57
  args.size == 1 ? list.first : list
53
58
  end
54
59
  end
@@ -7,12 +7,13 @@ module Invokr
7
7
 
8
8
  attr :argument_names, :injector, :method, :missing_args, :unused_args
9
9
 
10
- def initialize method, injector, implicit_block
10
+ def initialize method, injector, implicit_block, allow_unused = false
11
11
  @argument_names = method.parameters.map &:last
12
12
  @injector = injector
13
13
  @method = method
14
14
  @opt_arg_name = nil
15
15
 
16
+ @allow_unused = allow_unused
16
17
  @block_arg = nil
17
18
  @implicit_block = implicit_block
18
19
  @keyword_args = {}
@@ -24,7 +25,7 @@ module Invokr
24
25
 
25
26
  def build
26
27
  handle_args!
27
- check_for_unused_args!
28
+ check_for_unused_args! unless @allow_unused
28
29
  check_for_missing_args!
29
30
  build_invocation
30
31
  end
@@ -4,7 +4,7 @@ module Invokr
4
4
 
5
5
  def initialize missing_args
6
6
  @missing_args = missing_args
7
- missing_args.map! do |arg| "`#{arg}'" end
7
+ missing_args.map! do |arg| display arg end
8
8
  end
9
9
 
10
10
  def message
@@ -22,6 +22,13 @@ module Invokr
22
22
  last_arg = missing_args.pop
23
23
  "#{missing_args.join ', '} and #{last_arg}"
24
24
  end
25
+
26
+ def display arg
27
+ list = Array(arg).map do |arg| "`#{arg}'" end
28
+ str = list * " or "
29
+ str.insert 0, "either " if list.size > 1
30
+ str
31
+ end
25
32
  end
26
33
 
27
34
  class BadArgumentsError < ArgumentError
@@ -1,3 +1,3 @@
1
1
  module Invokr
2
- VERSION = "0.9.5"
2
+ VERSION = "0.9.6"
3
3
  end
@@ -58,7 +58,7 @@ class DependencyInjectionExampleTest < Minitest::Test
58
58
  [[:req, :foo],[:req, :bar]]
59
59
  end
60
60
 
61
- def call(foo, bar)
61
+ def call foo, bar
62
62
  OpenStruct.new foo: foo, bar: bar
63
63
  end
64
64
  end
@@ -7,13 +7,21 @@ class InvokrTest < Minitest::Test
7
7
  assert_equal 5, result
8
8
  end
9
9
 
10
+ def test_using_keyword_overrides_extra_arguments_error
11
+ my_proc = ->a,b{a**b}
12
+
13
+ result = Invokr.invoke proc: my_proc, using: { a: 2, b: 3, c: nil }
14
+
15
+ assert_equal 8, result
16
+ end
17
+
10
18
  def test_incorrectly_invoking
11
19
  error = assert_raises Invokr::InputError do
12
20
  Invokr.invoke
13
21
  end
14
22
 
15
23
  assert_equal(
16
- "cannot invoke; missing required arguments: `method', `on' and `with'",
24
+ "cannot invoke; missing required arguments: `method', `on' and either `with' or `using'",
17
25
  error.message,
18
26
  )
19
27
  end
metadata CHANGED
@@ -1,79 +1,64 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: invokr
3
- version: !ruby/object:Gem::Version
4
- version: 0.9.5
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.6
5
5
  platform: ruby
6
- authors:
6
+ authors:
7
7
  - ntl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-22 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
11
+
12
+ date: 2014-12-03 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: "1.6"
14
20
  name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.6'
20
- type: :development
21
21
  prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.6'
27
- - !ruby/object:Gem::Dependency
28
- name: minitest
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
22
+ version_requirements: *id001
34
23
  type: :development
24
+ - !ruby/object:Gem::Dependency
25
+ requirement: &id002 !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - &id003
28
+ - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: "0"
31
+ name: minitest
35
32
  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: minitest-red_green
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
33
+ version_requirements: *id002
48
34
  type: :development
35
+ - !ruby/object:Gem::Dependency
36
+ requirement: &id004 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - *id003
39
+ name: minitest-red_green
49
40
  prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
41
+ version_requirements: *id004
62
42
  type: :development
43
+ - !ruby/object:Gem::Dependency
44
+ requirement: &id005 !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - *id003
47
+ name: rake
63
48
  prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
49
+ version_requirements: *id005
50
+ type: :development
69
51
  description: Invoke methods with a consistent Hash interface. Useful for metaprogramming.
70
- email:
52
+ email:
71
53
  - nathanladd+github@gmail.com
72
54
  executables: []
55
+
73
56
  extensions: []
57
+
74
58
  extra_rdoc_files: []
75
- files:
76
- - ".gitignore"
59
+
60
+ files:
61
+ - .gitignore
77
62
  - Gemfile
78
63
  - LICENSE.txt
79
64
  - README.md
@@ -98,30 +83,29 @@ files:
98
83
  - test/rest_args_test.rb
99
84
  - test/test_helper.rb
100
85
  homepage: https://github.com/ntl/invokr
101
- licenses:
86
+ licenses:
102
87
  - MIT
103
88
  metadata: {}
89
+
104
90
  post_install_message:
105
91
  rdoc_options: []
106
- require_paths:
92
+
93
+ require_paths:
107
94
  - lib
108
- required_ruby_version: !ruby/object:Gem::Requirement
109
- requirements:
110
- - - ">="
111
- - !ruby/object:Gem::Version
112
- version: '0'
113
- required_rubygems_version: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: '0'
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - *id003
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - *id003
118
101
  requirements: []
102
+
119
103
  rubyforge_project:
120
- rubygems_version: 2.4.1
104
+ rubygems_version: 2.4.2
121
105
  signing_key:
122
106
  specification_version: 4
123
107
  summary: Invoke methods with a consistent Hash interface.
124
- test_files:
108
+ test_files:
125
109
  - test/block_args_test.rb
126
110
  - test/dependency_injection_test.rb
127
111
  - test/invokr_test.rb