invokr 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/invokr/builder.rb +5 -0
- data/lib/invokr/dependency_injection.rb +2 -3
- data/lib/invokr/errors.rb +6 -0
- data/lib/invokr/method.rb +39 -27
- data/lib/invokr/version.rb +1 -1
- data/test/keyword_args_test.rb +15 -0
- data/test/query_test.rb +15 -3
- data/test/rest_args_test.rb +16 -0
- data/test/test_helper.rb +8 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 808ea736022fc954541d33ff6329fa5f08d97cba
|
4
|
+
data.tar.gz: 51e16c3ffd417e064e5012ae2a706d85641bf69c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 401bd351f434a91efa23ec097abed40ef186b2cbeb5b80dd294cfef9fe5d78a6283c0ddc864225199256a5d860c2bdc82c03445ec794147c4a2f9576571ab459
|
7
|
+
data.tar.gz: 62d71c2fd5e803f80dcfb5abfc6238b7ac28b44c5d5e5538f0fd331dacdfee84bf0608bad17c5f8ca80e9631a0115a7e6b8e7eb065dadcf7f7e6d8016031c75c
|
data/lib/invokr/builder.rb
CHANGED
@@ -64,6 +64,11 @@ module Invokr
|
|
64
64
|
@keyword_args[identifier] = injector[identifier]
|
65
65
|
end
|
66
66
|
|
67
|
+
def handle_rest_arg identifier
|
68
|
+
raise UnsupportedArgumentsError.new(self, [identifier])
|
69
|
+
end
|
70
|
+
alias_method :handle_keyrest_arg, :handle_rest_arg
|
71
|
+
|
67
72
|
def handle_block_arg identifier
|
68
73
|
if injector.has_key? identifier and @implicit_block
|
69
74
|
unused_args << identifier and return
|
@@ -9,9 +9,8 @@ module Invokr
|
|
9
9
|
|
10
10
|
Injector = Struct.new :resolver, :klass do
|
11
11
|
def inject
|
12
|
-
|
13
|
-
|
14
|
-
invocation.invoke! klass
|
12
|
+
method = Invokr.query_method klass.instance_method :initialize
|
13
|
+
method.invoke method: :new, with: self
|
15
14
|
end
|
16
15
|
|
17
16
|
def keys
|
data/lib/invokr/errors.rb
CHANGED
@@ -49,6 +49,12 @@ module Invokr
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
+
class UnsupportedArgumentsError < BadArgumentsError
|
53
|
+
def message
|
54
|
+
%(unsupported splat argument(s) #{formatted_args} when invoking method `#{builder.method.name}' on #{builder.method.owner.inspect})
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
52
58
|
class OptionalPositionalArgumentError < StandardError
|
53
59
|
attr :message
|
54
60
|
|
data/lib/invokr/method.rb
CHANGED
@@ -1,38 +1,50 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module Invokr
|
2
|
+
Method = Struct.new :method do
|
3
|
+
def invoke args = {}
|
4
|
+
receiver, method_name, hsh_args = extract_args! args
|
5
|
+
unless receiver == method.owner or receiver.kind_of? method.owner
|
6
|
+
raise TypeError, "no implicit conversion of #{receiver.class} into #{method.owner.name}"
|
7
|
+
end
|
8
|
+
invocation = Builder.build method, hsh_args, args[:block]
|
9
|
+
invocation.method = method_name unless method_name == method.name
|
10
|
+
invocation.invoke! receiver
|
5
11
|
end
|
6
|
-
Invokr.invoke method: method.name, on: receiver, with: hsh_args
|
7
|
-
end
|
8
12
|
|
9
|
-
|
10
|
-
|
11
|
-
|
13
|
+
def trim_args hsh_args
|
14
|
+
hsh_args.select { |key, _| dependencies.include? key }
|
15
|
+
end
|
12
16
|
|
13
|
-
|
14
|
-
|
15
|
-
|
17
|
+
def dependencies
|
18
|
+
map_identifiers parameters
|
19
|
+
end
|
16
20
|
|
17
|
-
|
18
|
-
|
19
|
-
|
21
|
+
def optional_dependencies
|
22
|
+
map_identifiers select_parameters_by_type [:opt, :key]
|
23
|
+
end
|
20
24
|
|
21
|
-
|
22
|
-
|
23
|
-
|
25
|
+
def required_dependencies
|
26
|
+
map_identifiers select_parameters_by_type [:req, :keyreq]
|
27
|
+
end
|
24
28
|
|
25
|
-
|
26
|
-
|
27
|
-
|
29
|
+
def parameters
|
30
|
+
method.parameters
|
31
|
+
end
|
28
32
|
|
29
|
-
|
33
|
+
private
|
30
34
|
|
31
|
-
|
32
|
-
|
33
|
-
|
35
|
+
def extract_args! args
|
36
|
+
receiver = args.fetch :receiver do method.owner end
|
37
|
+
method_name = args.fetch :method do method.name end
|
38
|
+
with = args.fetch :with
|
39
|
+
[receiver, method_name, with]
|
40
|
+
end
|
41
|
+
|
42
|
+
def select_parameters_by_type types
|
43
|
+
parameters.select do |type, _| types.include? type end
|
44
|
+
end
|
34
45
|
|
35
|
-
|
36
|
-
|
46
|
+
def map_identifiers parameters
|
47
|
+
parameters.map do |_, identifier| identifier end
|
48
|
+
end
|
37
49
|
end
|
38
50
|
end
|
data/lib/invokr/version.rb
CHANGED
data/test/keyword_args_test.rb
CHANGED
@@ -29,6 +29,21 @@ module KeywordArgsTest
|
|
29
29
|
|
30
30
|
assert_equal [:album], method.optional_dependencies
|
31
31
|
end
|
32
|
+
|
33
|
+
def test_passing_in_splat_raises_error
|
34
|
+
error = assert_raises Invokr::UnsupportedArgumentsError do
|
35
|
+
Invokr.invoke(
|
36
|
+
method: :keyword_splat_argument,
|
37
|
+
on: TestMethodBank,
|
38
|
+
with: { guitarist: 'trey' },
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
assert_equal(
|
43
|
+
"unsupported splat argument(s) `rest' when invoking method `keyword_splat_argument' on #<TestMethodBank:0xdeadbeef>",
|
44
|
+
error.message,
|
45
|
+
)
|
46
|
+
end
|
32
47
|
end
|
33
48
|
|
34
49
|
class RequiredKeywordArgsTest < Minitest::Test
|
data/test/query_test.rb
CHANGED
@@ -17,23 +17,31 @@ class QueryTest < Minitest::Test
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_invoking_singleton_from_query_object
|
20
|
-
assert_equal ['junta', 'trey'], @method.invoke(album: 'junta')
|
20
|
+
assert_equal ['junta', 'trey'], @method.invoke(with: { album: 'junta' })
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_invoking_instance_from_query_object
|
24
24
|
test_klass = define_test_klass
|
25
25
|
method = Invokr.query_method test_klass.instance_method :upcase
|
26
26
|
|
27
|
-
val = method.invoke test_klass.new, dep: 'phIsh'
|
27
|
+
val = method.invoke receiver: test_klass.new, with: { dep: 'phIsh' }
|
28
28
|
assert_equal "PHISH", val
|
29
29
|
end
|
30
30
|
|
31
|
+
def test_invoking_alternate_method
|
32
|
+
test_klass = define_test_klass
|
33
|
+
method = Invokr.query_method test_klass.instance_method :upcase
|
34
|
+
|
35
|
+
val = method.invoke receiver: test_klass.new, method: :downcase, with: { dep: 'PHiSH' }
|
36
|
+
assert_equal "phish", val
|
37
|
+
end
|
38
|
+
|
31
39
|
def test_cannot_invoke_instance_not_type_other_than_method_owner
|
32
40
|
test_klass = define_test_klass
|
33
41
|
method = Invokr.query_method test_klass.instance_method :upcase
|
34
42
|
|
35
43
|
error = assert_raises TypeError do
|
36
|
-
method.invoke Array.new, dep: 'phIsh'
|
44
|
+
method.invoke receiver: Array.new, with: { dep: 'phIsh' }
|
37
45
|
end
|
38
46
|
|
39
47
|
assert_equal 'no implicit conversion of Array into TestKlass', error.message
|
@@ -55,6 +63,10 @@ class QueryTest < Minitest::Test
|
|
55
63
|
def upcase dep
|
56
64
|
dep.upcase
|
57
65
|
end
|
66
|
+
|
67
|
+
def downcase dep
|
68
|
+
dep.downcase
|
69
|
+
end
|
58
70
|
end
|
59
71
|
end
|
60
72
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class RestArgsTest < Minitest::Test
|
2
|
+
def test_passing_in_splat
|
3
|
+
error = assert_raises Invokr::UnsupportedArgumentsError do
|
4
|
+
Invokr.invoke(
|
5
|
+
method: :splat_argument,
|
6
|
+
on: TestMethodBank,
|
7
|
+
with: { guitarist: 'trey' },
|
8
|
+
)
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_equal(
|
12
|
+
"unsupported splat argument(s) `rest' when invoking method `splat_argument' on #<TestMethodBank:0xdeadbeef>",
|
13
|
+
error.message,
|
14
|
+
)
|
15
|
+
end
|
16
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -33,6 +33,10 @@ module TestMethodBank
|
|
33
33
|
[album, guitarist]
|
34
34
|
end
|
35
35
|
|
36
|
+
def splat_argument album = 'junta', *rest
|
37
|
+
[album, *rest]
|
38
|
+
end
|
39
|
+
|
36
40
|
def just_yields
|
37
41
|
yield
|
38
42
|
end
|
@@ -46,6 +50,10 @@ module TestMethodBank
|
|
46
50
|
def optional_keyword_argument album: 'pitcher_of_nectar'
|
47
51
|
album
|
48
52
|
end
|
53
|
+
|
54
|
+
def keyword_splat_argument album: 'pitcher_of_nectar', **rest
|
55
|
+
[album, **rest]
|
56
|
+
end
|
49
57
|
RB
|
50
58
|
end
|
51
59
|
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ntl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- test/optional_args_test.rb
|
96
96
|
- test/query_test.rb
|
97
97
|
- test/required_args_test.rb
|
98
|
+
- test/rest_args_test.rb
|
98
99
|
- test/test_helper.rb
|
99
100
|
homepage: https://github.com/ntl/invokr
|
100
101
|
licenses:
|
@@ -128,4 +129,5 @@ test_files:
|
|
128
129
|
- test/optional_args_test.rb
|
129
130
|
- test/query_test.rb
|
130
131
|
- test/required_args_test.rb
|
132
|
+
- test/rest_args_test.rb
|
131
133
|
- test/test_helper.rb
|