invokr 0.0.6 → 0.1.0
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 +1 -1
- data/lib/invokr.rb +19 -13
- data/lib/invokr/dependency_injection.rb +66 -12
- data/lib/invokr/version.rb +1 -1
- data/test/dependency_injection_example_test.rb +21 -22
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d465d0fe3a99374d9964c837dd835e2686ea18e4
|
4
|
+
data.tar.gz: 6f524e10b9b8dcd60755ac40ea7264835370cf5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aeb433d0fcc41411665277604feab0b684f82a0f3cbdfd86cab9a9822d93aa2af2b27b5f5856fc95f8f6578d2291860d74a295e54c7338bd4852a1f7061b5534
|
7
|
+
data.tar.gz: 68128087cca50df10d2186934160052d6a809c93f1f8d078c6025cb57a2825ac7b52ef824dab49e4475b7235647715796cf0f446787eac1b45f147f38cc2827b
|
data/README.md
CHANGED
@@ -70,7 +70,7 @@ Before ruby 2.x introduced keyword arguments, it was common to end your method s
|
|
70
70
|
|
71
71
|
## Dependency injection
|
72
72
|
|
73
|
-
One of the use cases for Invokr is building abstract factories. In this case, you want to inspect the method signature of `Object#initialize`, but actually pass `.new` to the class to have it allocate memory and invoke the initializer for you.
|
73
|
+
One of the use cases for Invokr is building abstract factories. In this case, you want to inspect the method signature of `Object#initialize`, but actually pass `.new` to the class to have it allocate memory and invoke the initializer for you. Check out `test/dependency_injection_example_test.rb` for how it is used. You can use a hash to serve as the registry of objects, or build your own custom resolver.
|
74
74
|
|
75
75
|
## Contributing
|
76
76
|
|
data/lib/invokr.rb
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
require 'delegate'
|
2
2
|
|
3
|
+
require_relative 'invokr/builder'
|
4
|
+
require_relative 'invokr/dependency_injection'
|
5
|
+
require_relative 'invokr/errors'
|
6
|
+
require_relative 'invokr/invocation'
|
7
|
+
require_relative 'invokr/method'
|
8
|
+
require_relative 'invokr/version'
|
9
|
+
|
3
10
|
module Invokr
|
4
11
|
extend self
|
5
12
|
|
@@ -11,6 +18,17 @@ module Invokr
|
|
11
18
|
end
|
12
19
|
end
|
13
20
|
|
21
|
+
def inject obj, args = {}
|
22
|
+
using = require_arguments! args, :using
|
23
|
+
DependencyInjection.inject obj, using
|
24
|
+
end
|
25
|
+
|
26
|
+
def query_method method
|
27
|
+
Method.new method
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
14
32
|
def invoke_method args = {}
|
15
33
|
method_name, obj, hsh_args = require_arguments! args, :method, :on, :with
|
16
34
|
method = obj.method method_name
|
@@ -18,19 +36,13 @@ module Invokr
|
|
18
36
|
invocation.invoke! obj
|
19
37
|
end
|
20
38
|
|
21
|
-
def invoke_proc _proc, args
|
39
|
+
def invoke_proc _proc, args = {}
|
22
40
|
hsh_args = require_arguments! args, :with
|
23
41
|
invocation = Builder.build _proc, hsh_args, args[:block]
|
24
42
|
obj = SimpleDelegator.new _proc
|
25
43
|
invocation.invoke! obj
|
26
44
|
end
|
27
45
|
|
28
|
-
def query_method method
|
29
|
-
Method.new method
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
46
|
def require_arguments! hsh, *args
|
35
47
|
found_args, missing_args = args.partition do |arg|
|
36
48
|
hsh.has_key? arg
|
@@ -40,9 +52,3 @@ module Invokr
|
|
40
52
|
args.size == 1 ? list.first : list
|
41
53
|
end
|
42
54
|
end
|
43
|
-
|
44
|
-
require_relative 'invokr/builder'
|
45
|
-
require_relative 'invokr/errors'
|
46
|
-
require_relative 'invokr/invocation'
|
47
|
-
require_relative 'invokr/method'
|
48
|
-
require_relative 'invokr/version'
|
@@ -1,28 +1,82 @@
|
|
1
1
|
module Invokr
|
2
2
|
module DependencyInjection
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def inject obj, using
|
6
|
+
meth = case obj
|
7
|
+
when Proc then :inject_proc
|
8
|
+
when Class then :inject_klass
|
9
|
+
else raise ArgumentError, "can't inject #{obj.inspect}"
|
10
|
+
end
|
11
|
+
resolver = build_resolver using
|
12
|
+
send meth, obj, resolver
|
8
13
|
end
|
9
14
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
15
|
+
private
|
16
|
+
|
17
|
+
def build_resolver using
|
18
|
+
if using.is_a? Hash
|
19
|
+
HashResolver.new using
|
20
|
+
else
|
21
|
+
using
|
14
22
|
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def inject_klass klass, resolver
|
26
|
+
injector = KlassInjector.new resolver, klass
|
27
|
+
injector.inject
|
28
|
+
end
|
29
|
+
|
30
|
+
def inject_proc proc, resolver
|
31
|
+
injector = ProcInjector.new resolver, proc
|
32
|
+
injector.inject
|
33
|
+
end
|
15
34
|
|
35
|
+
Injector = Struct.new :resolver, :obj do
|
16
36
|
def keys
|
17
|
-
|
37
|
+
method.parameters.map { |_, identifier| identifier }
|
18
38
|
end
|
19
39
|
|
20
40
|
def fetch arg, &default
|
21
41
|
resolver.resolve arg, &default
|
22
42
|
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class KlassInjector < Injector
|
46
|
+
def inject
|
47
|
+
_method = Invokr.query_method method
|
48
|
+
_method.invoke :method => :new, :with => self
|
49
|
+
end
|
50
|
+
|
51
|
+
def method
|
52
|
+
obj.instance_method :initialize
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class ProcInjector < Injector
|
57
|
+
def inject
|
58
|
+
Invokr.invoke :proc => obj, :with => self
|
59
|
+
end
|
60
|
+
|
61
|
+
def method
|
62
|
+
obj
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class HashResolver
|
67
|
+
def initialize hsh
|
68
|
+
@hsh = hsh
|
69
|
+
end
|
70
|
+
|
71
|
+
def inject klass
|
72
|
+
DependencyInjection.inject(
|
73
|
+
:klass => klass,
|
74
|
+
:using => self,
|
75
|
+
)
|
76
|
+
end
|
23
77
|
|
24
|
-
def
|
25
|
-
|
78
|
+
def resolve val
|
79
|
+
@hsh.fetch val
|
26
80
|
end
|
27
81
|
end
|
28
82
|
end
|
data/lib/invokr/version.rb
CHANGED
@@ -1,33 +1,32 @@
|
|
1
|
-
require '
|
1
|
+
require 'ostruct'
|
2
2
|
|
3
3
|
class DependencyInjectionExampleTest < Minitest::Test
|
4
|
-
def
|
5
|
-
|
6
|
-
|
7
|
-
:
|
8
|
-
|
4
|
+
def test_dependency_injection
|
5
|
+
obj = Invokr.inject(
|
6
|
+
TestObject,
|
7
|
+
:using => {
|
8
|
+
:album => 'farmhouse',
|
9
|
+
:guitarist => 'trey',
|
10
|
+
:drummer => 'fishman',
|
11
|
+
},
|
9
12
|
)
|
10
|
-
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
+
assert_equal 'farmhouse', obj.album
|
15
|
+
assert_equal 'trey', obj.guitarist
|
14
16
|
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
@hsh = hsh
|
19
|
-
end
|
18
|
+
def test_injecting_a_proc
|
19
|
+
my_proc = -> foo do OpenStruct.new foo: foo end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
:
|
25
|
-
|
26
|
-
|
21
|
+
obj = Invokr.inject(
|
22
|
+
my_proc,
|
23
|
+
:using => {
|
24
|
+
:foo => 'bar',
|
25
|
+
:ping => 'pong',
|
26
|
+
}
|
27
|
+
)
|
27
28
|
|
28
|
-
|
29
|
-
@hsh.fetch val
|
30
|
-
end
|
29
|
+
assert_equal 'bar', obj.foo
|
31
30
|
end
|
32
31
|
|
33
32
|
class TestObject
|
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.1.0
|
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-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|