invokr 0.0.5 → 0.0.6

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: 808ea736022fc954541d33ff6329fa5f08d97cba
4
- data.tar.gz: 51e16c3ffd417e064e5012ae2a706d85641bf69c
3
+ metadata.gz: 9f813b2a7451887b13d734bf67a3dacb9c5046a8
4
+ data.tar.gz: 95c15ca02dcbfc7ba1a938d4926533c9800b2e42
5
5
  SHA512:
6
- metadata.gz: 401bd351f434a91efa23ec097abed40ef186b2cbeb5b80dd294cfef9fe5d78a6283c0ddc864225199256a5d860c2bdc82c03445ec794147c4a2f9576571ab459
7
- data.tar.gz: 62d71c2fd5e803f80dcfb5abfc6238b7ac28b44c5d5e5538f0fd331dacdfee84bf0608bad17c5f8ca80e9631a0115a7e6b8e7eb065dadcf7f7e6d8016031c75c
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
 
@@ -31,7 +31,11 @@ module Invokr
31
31
 
32
32
  def build_invocation
33
33
  @block_arg = @implicit_block if @implicit_block
34
- Invocation.new method.name, @positional_args, @keyword_args, @block_arg
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!
@@ -1,3 +1,3 @@
1
1
  module Invokr
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
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.5
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-01 00:00:00.000000000 Z
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.3.0
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.