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 +7 -7
- data/README.md +13 -5
- data/lib/invokr.rb +11 -6
- data/lib/invokr/builder.rb +3 -2
- data/lib/invokr/errors.rb +8 -1
- data/lib/invokr/version.rb +1 -1
- data/test/dependency_injection_test.rb +1 -1
- data/test/invokr_test.rb +9 -1
- metadata +55 -71
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
5
|
-
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 )
|
data/lib/invokr.rb
CHANGED
@@ -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
|
-
|
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?
|
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
|
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
|
data/lib/invokr/builder.rb
CHANGED
@@ -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
|
data/lib/invokr/errors.rb
CHANGED
@@ -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|
|
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
|
data/lib/invokr/version.rb
CHANGED
data/test/invokr_test.rb
CHANGED
@@ -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.
|
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
|
-
|
12
|
-
|
13
|
-
|
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:
|
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:
|
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:
|
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:
|
65
|
-
|
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
|
-
|
76
|
-
|
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
|
-
|
92
|
+
|
93
|
+
require_paths:
|
107
94
|
- lib
|
108
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
-
requirements:
|
110
|
-
-
|
111
|
-
|
112
|
-
|
113
|
-
|
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.
|
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
|