invokr 0.0.5 → 0.0.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 +4 -4
- data/README.md +7 -0
- data/lib/invokr.rb +19 -1
- data/lib/invokr/builder.rb +5 -1
- data/lib/invokr/version.rb +1 -1
- data/test/invokr_test.rb +8 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f813b2a7451887b13d734bf67a3dacb9c5046a8
|
4
|
+
data.tar.gz: 95c15ca02dcbfc7ba1a938d4926533c9800b2e42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ba3d9a231c5c359ea78b10932fea381cd1e15c765704a57cc5bd40905472581674df5c40782dc3cb268b72031a01344fd98707b997cb07584a029831b0fe648
|
7
|
+
data.tar.gz: acd9c17ba32c4e05031dddf856a07ce3aec1d45ac00d0feae5a251bc3af7e774eb5c726ad0de4873a2c92af36453429fa6d8ba16a54df2ea45374ba24ae88b58
|
data/README.md
CHANGED
@@ -24,6 +24,13 @@ Invokr.invoke method: :add_transaction, on: bank_account, with: params
|
|
24
24
|
|
25
25
|
Behind the scenes, Invokr figured out how to translate that `Hash` into a method signature compatible with `BankAccount#add_transaction`.
|
26
26
|
|
27
|
+
You can also pass in procs to invokr:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
my_proc = ->|a,b| { a + b }
|
31
|
+
Invokr.invoke proc: my_proc, with: { a: 2, b, 4 }
|
32
|
+
```
|
33
|
+
|
27
34
|
## Querying
|
28
35
|
|
29
36
|
Want to investigate the arguments of a method?
|
data/lib/invokr.rb
CHANGED
@@ -1,13 +1,30 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
1
3
|
module Invokr
|
2
4
|
extend self
|
3
5
|
|
4
6
|
def invoke args = {}
|
7
|
+
if _proc = args.delete(:proc)
|
8
|
+
invoke_proc _proc, args
|
9
|
+
else
|
10
|
+
invoke_method args
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def invoke_method args = {}
|
5
15
|
method_name, obj, hsh_args = require_arguments! args, :method, :on, :with
|
6
16
|
method = obj.method method_name
|
7
17
|
invocation = Builder.build method, hsh_args, args[:block]
|
8
18
|
invocation.invoke! obj
|
9
19
|
end
|
10
20
|
|
21
|
+
def invoke_proc _proc, args
|
22
|
+
hsh_args = require_arguments! args, :with
|
23
|
+
invocation = Builder.build _proc, hsh_args, args[:block]
|
24
|
+
obj = SimpleDelegator.new _proc
|
25
|
+
invocation.invoke! obj
|
26
|
+
end
|
27
|
+
|
11
28
|
def query_method method
|
12
29
|
Method.new method
|
13
30
|
end
|
@@ -19,7 +36,8 @@ module Invokr
|
|
19
36
|
hsh.has_key? arg
|
20
37
|
end
|
21
38
|
raise InputError.new missing_args unless missing_args.empty?
|
22
|
-
found_args.map { |arg| hsh.fetch arg }
|
39
|
+
list = found_args.map { |arg| hsh.fetch arg }
|
40
|
+
args.size == 1 ? list.first : list
|
23
41
|
end
|
24
42
|
end
|
25
43
|
|
data/lib/invokr/builder.rb
CHANGED
@@ -31,7 +31,11 @@ module Invokr
|
|
31
31
|
|
32
32
|
def build_invocation
|
33
33
|
@block_arg = @implicit_block if @implicit_block
|
34
|
-
|
34
|
+
if method.is_a? Proc
|
35
|
+
Invocation.new :call, @positional_args, @keyword_args, @block_arg
|
36
|
+
else
|
37
|
+
Invocation.new method.name, @positional_args, @keyword_args, @block_arg
|
38
|
+
end
|
35
39
|
end
|
36
40
|
|
37
41
|
def handle_args!
|
data/lib/invokr/version.rb
CHANGED
data/test/invokr_test.rb
CHANGED
@@ -1,4 +1,12 @@
|
|
1
1
|
class InvokrTest < Minitest::Test
|
2
|
+
def test_supplying_proc
|
3
|
+
my_proc = ->a,b{a+b}
|
4
|
+
|
5
|
+
result = Invokr.invoke proc: my_proc, with: { a: 1, b: 4 }
|
6
|
+
|
7
|
+
assert_equal 5, result
|
8
|
+
end
|
9
|
+
|
2
10
|
def test_incorrectly_invoking
|
3
11
|
error = assert_raises Invokr::InputError do
|
4
12
|
Invokr.invoke
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: invokr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ntl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -117,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
117
|
version: '0'
|
118
118
|
requirements: []
|
119
119
|
rubyforge_project:
|
120
|
-
rubygems_version: 2.
|
120
|
+
rubygems_version: 2.4.1
|
121
121
|
signing_key:
|
122
122
|
specification_version: 4
|
123
123
|
summary: Invoke methods with a consistent Hash interface.
|