corindon 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/corindon/console/application.rb +15 -0
- data/lib/corindon/console/base_command.rb +13 -0
- data/lib/corindon/console/input.rb +8 -0
- data/lib/corindon/console/output.rb +8 -0
- data/lib/corindon/dependency_injection/container.rb +38 -10
- data/lib/corindon/dependency_injection/definition.rb +13 -3
- data/lib/corindon/dependency_injection/dsl.rb +39 -4
- data/lib/corindon/dependency_injection/id/id_generator.rb +12 -0
- data/lib/corindon/dependency_injection/id/uuid_generator.rb +14 -0
- data/lib/corindon/dependency_injection/injectable.rb +1 -1
- data/lib/corindon/dependency_injection/injector.rb +12 -15
- data/lib/corindon/dependency_injection/token/injection_token.rb +12 -0
- data/lib/corindon/dependency_injection/token/service_call_token.rb +60 -0
- data/lib/corindon/dependency_injection/token/tagged_token.rb +24 -0
- data/lib/corindon/dependency_injection/token/value_token.rb +22 -0
- data/lib/corindon/ext/guards.rb +17 -0
- data/lib/corindon/ext/guards/error.rb +10 -0
- data/lib/corindon/version.rb +1 -1
- metadata +14 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e142a541f92e6b2af8869b8fadc2ecc82da2823b4c5ed9a9a24b7959cceecd7c
|
4
|
+
data.tar.gz: 8d9623f9a1c9b8162b551efd142fb71259e0bb689eb7e3f9ef0b2788682521c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e45eca1ef91da4d221138313a9c15bca970585adb5f22a47efe9503aaa2b9630691182819199387b8f690d48859a7e28e20725abf3c135e5f3e30c9a42951a4f
|
7
|
+
data.tar.gz: 88772fb162c2b8c95678f9965aee48dbc15cc548224e39b928de8e5422683005e07ff64148f43e790ed6dc96a05bf69a3c25a3f2a1dba9e01606f8be81f42107
|
@@ -6,25 +6,53 @@ module Corindon
|
|
6
6
|
using Ext::Something
|
7
7
|
|
8
8
|
attr_reader :definitions
|
9
|
+
attr_reader :id_generator
|
9
10
|
attr_reader :injector
|
10
11
|
attr_reader :services
|
11
12
|
|
12
|
-
|
13
|
+
# @param [Id::IdGenerator] id_generator
|
14
|
+
def initialize(id_generator: Id::UuidGenerator.new)
|
15
|
+
@id_generator = id_generator
|
13
16
|
@services = {}
|
14
17
|
@definitions = {}
|
18
|
+
@tags = Hash.new { |hash, key| hash[key] = [] }
|
15
19
|
|
16
20
|
@injector = Injector.new(container: self)
|
17
21
|
end
|
18
22
|
|
19
23
|
# @param [Class] klass
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
# @return [String]
|
25
|
+
def add_definition(klass, id: nil, anonymous: false, &block)
|
26
|
+
definition = if injectable?(klass)
|
27
|
+
klass.definition
|
28
|
+
elsif block.sth?
|
29
|
+
Dsl.new(klass).exec(&block)
|
30
|
+
else
|
31
|
+
Definition.new(klass)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Generate an id if set to anonymous when registering
|
35
|
+
# If a definition is set to be anonymous but an id is provided zhen registering, use this id instead<
|
36
|
+
if anonymous || (definition.anonymous? && id.nil?)
|
37
|
+
id = id_generator.generate
|
38
|
+
end
|
39
|
+
|
40
|
+
id = id || definition.id || to_id(klass)
|
41
|
+
@definitions[id] = definition
|
42
|
+
definition.tags.each { |tag| @tags[tag] << id }
|
43
|
+
|
44
|
+
id
|
45
|
+
end
|
46
|
+
|
47
|
+
# @param [String] tag
|
48
|
+
# @return [Array<String>]
|
49
|
+
def tagged(tag)
|
50
|
+
if @tags.key?(tag)
|
51
|
+
|
52
|
+
@tags.fetch(tag)
|
53
|
+
else
|
54
|
+
[]
|
55
|
+
end
|
28
56
|
end
|
29
57
|
|
30
58
|
# @param [Class, #to_s] key
|
@@ -56,7 +84,7 @@ module Corindon
|
|
56
84
|
end
|
57
85
|
|
58
86
|
def injectable?(klass)
|
59
|
-
klass.ancestors.include?(Injectable)
|
87
|
+
klass.is_a?(Class) && klass.ancestors.include?(Injectable)
|
60
88
|
end
|
61
89
|
|
62
90
|
# @param [Class, #to_s] key
|
@@ -9,26 +9,36 @@ module Corindon
|
|
9
9
|
attr_reader :args
|
10
10
|
attr_reader :kwargs
|
11
11
|
attr_reader :calls
|
12
|
+
attr_reader :tags
|
13
|
+
attr_reader :id
|
12
14
|
|
13
|
-
def initialize(klass, args
|
15
|
+
def initialize(klass, args: [], kwargs: {}, calls: [], tags: [], id: nil, anonymous: false)
|
14
16
|
@klass = klass
|
15
17
|
@args = args
|
16
18
|
@kwargs = kwargs
|
17
19
|
@calls = calls
|
20
|
+
@tags = tags
|
21
|
+
@id = id
|
22
|
+
@anonymous = anonymous
|
18
23
|
end
|
19
24
|
|
20
25
|
# @param [Injector] injector
|
21
26
|
# # @return [Object]
|
22
27
|
def build(injector)
|
23
|
-
object = do_call(klass, :new,
|
28
|
+
object = do_call(klass, :new, injector.resolve(args), injector.resolve(kwargs))
|
24
29
|
|
25
30
|
calls.each do |(call, call_args, call_kwargs)|
|
26
|
-
do_call(object, call,
|
31
|
+
do_call(object, call, injector.resolve(call_args), injector.resolve(call_kwargs))
|
27
32
|
end
|
28
33
|
|
29
34
|
object
|
30
35
|
end
|
31
36
|
|
37
|
+
# @return [Boolean]
|
38
|
+
def anonymous?
|
39
|
+
@anonymous
|
40
|
+
end
|
41
|
+
|
32
42
|
private
|
33
43
|
|
34
44
|
if ::Semantic::Version.new(RUBY_VERSION).satisfies?(">= 2.7.0")
|
@@ -4,18 +4,21 @@ module Corindon
|
|
4
4
|
module DependencyInjection
|
5
5
|
class Dsl
|
6
6
|
# @param [Class] klass
|
7
|
-
def initialize(klass)
|
7
|
+
def initialize(klass, args: [], kwargs: {}, id: nil, anonymous: false)
|
8
8
|
@klass = klass
|
9
|
-
@args =
|
10
|
-
@kwargs =
|
9
|
+
@args = args
|
10
|
+
@kwargs = kwargs
|
11
11
|
@calls = []
|
12
|
+
@tags = []
|
13
|
+
@id = id
|
14
|
+
@anonymous = anonymous
|
12
15
|
end
|
13
16
|
|
14
17
|
# @return [Definition]
|
15
18
|
def exec(&block)
|
16
19
|
instance_exec(&block)
|
17
20
|
|
18
|
-
Definition.new(@klass, @args, @kwargs, @calls)
|
21
|
+
Definition.new(@klass, args: @args, kwargs: @kwargs, calls: @calls, tags: @tags, id: @id, anonymous: @anonymous)
|
19
22
|
end
|
20
23
|
|
21
24
|
def args(*arguments, **kv_arguments)
|
@@ -27,6 +30,38 @@ module Corindon
|
|
27
30
|
def call(name, *arguments, **kv_arguments)
|
28
31
|
@calls << [name, arguments, kv_arguments]
|
29
32
|
end
|
33
|
+
|
34
|
+
# @param [String] id
|
35
|
+
def id(id)
|
36
|
+
@anonymous = false
|
37
|
+
@id = id
|
38
|
+
end
|
39
|
+
|
40
|
+
def anonymous!
|
41
|
+
@anonymous = true
|
42
|
+
@id = nil
|
43
|
+
end
|
44
|
+
|
45
|
+
# @param [String] tag
|
46
|
+
def tag(tag)
|
47
|
+
@tags << tag
|
48
|
+
end
|
49
|
+
|
50
|
+
# @return [Token::ServiceCallToken]
|
51
|
+
def on(id)
|
52
|
+
Token::ServiceCallToken.new(service: id)
|
53
|
+
end
|
54
|
+
|
55
|
+
# @return [Token::ValueToken]
|
56
|
+
def value(val)
|
57
|
+
Token::ValueToken.new(value: val)
|
58
|
+
end
|
59
|
+
|
60
|
+
# @param [String] tag
|
61
|
+
# @return [Token::TaggedToken]
|
62
|
+
def tagged(tag)
|
63
|
+
Token::TaggedToken.new(tag)
|
64
|
+
end
|
30
65
|
end
|
31
66
|
end
|
32
67
|
end
|
@@ -3,6 +3,7 @@
|
|
3
3
|
module Corindon
|
4
4
|
module DependencyInjection
|
5
5
|
class Injector
|
6
|
+
# @return [Container]
|
6
7
|
attr_reader :container
|
7
8
|
|
8
9
|
# @param [Container] container
|
@@ -10,22 +11,18 @@ module Corindon
|
|
10
11
|
@container = container
|
11
12
|
end
|
12
13
|
|
13
|
-
# @
|
14
|
-
def resolve(
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
elsif value.is_a?(Hash)
|
24
|
-
value.transform_values(&method(:resolve_value))
|
25
|
-
else
|
26
|
-
container.get(value)
|
27
|
-
end
|
14
|
+
# @param [Object] value
|
15
|
+
def resolve(value)
|
16
|
+
if value.is_a?(Array)
|
17
|
+
value.map(&method(:resolve))
|
18
|
+
elsif value.is_a?(Hash)
|
19
|
+
value.transform_values(&method(:resolve))
|
20
|
+
elsif value.is_a?(Token::InjectionToken)
|
21
|
+
value.resolve(injector: self)
|
22
|
+
else
|
23
|
+
container.get(value)
|
28
24
|
end
|
25
|
+
end
|
29
26
|
end
|
30
27
|
end
|
31
28
|
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Corindon
|
4
|
+
module DependencyInjection
|
5
|
+
module Token
|
6
|
+
class ServiceCallToken < InjectionToken
|
7
|
+
attr_reader :service
|
8
|
+
attr_reader :method
|
9
|
+
attr_reader :args
|
10
|
+
attr_reader :kwargs
|
11
|
+
|
12
|
+
def initialize(service:)
|
13
|
+
super()
|
14
|
+
|
15
|
+
@service = service
|
16
|
+
@args = []
|
17
|
+
@kwargs = {}
|
18
|
+
end
|
19
|
+
|
20
|
+
def call(method, *args, **kwargs)
|
21
|
+
@method = method
|
22
|
+
@args = args
|
23
|
+
@kwargs = kwargs
|
24
|
+
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param [Injector] injector
|
29
|
+
def resolve(injector:)
|
30
|
+
do_call(
|
31
|
+
injector.resolve(service),
|
32
|
+
method,
|
33
|
+
injector.resolve(args),
|
34
|
+
injector.resolve(kwargs)
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
if ::Semantic::Version.new(RUBY_VERSION).satisfies?(">= 2.7.0")
|
41
|
+
def do_call(obj, method, args, kwargs)
|
42
|
+
obj.send(method, *args, **kwargs)
|
43
|
+
end
|
44
|
+
else
|
45
|
+
def do_call(obj, method, args, kwargs)
|
46
|
+
if args.empty? && kwargs.empty?
|
47
|
+
obj.send(method)
|
48
|
+
elsif args.empty?
|
49
|
+
obj.send(method, **kwargs)
|
50
|
+
elsif kwargs.empty?
|
51
|
+
obj.send(method, *args)
|
52
|
+
else
|
53
|
+
obj.send(method, *args, **kwargs)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Corindon
|
4
|
+
module DependencyInjection
|
5
|
+
module Token
|
6
|
+
class TaggedToken < InjectionToken
|
7
|
+
# @return [String] tag
|
8
|
+
attr_reader :tag
|
9
|
+
|
10
|
+
# @param [String] tag
|
11
|
+
def initialize(tag)
|
12
|
+
super()
|
13
|
+
|
14
|
+
@tag = tag
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param [Injector] injector
|
18
|
+
def resolve(injector:)
|
19
|
+
injector.container.tagged(tag).map { |id| injector.resolve(id) }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Corindon
|
4
|
+
module DependencyInjection
|
5
|
+
module Token
|
6
|
+
class ValueToken < InjectionToken
|
7
|
+
attr_reader :value
|
8
|
+
|
9
|
+
# @param [Object] value
|
10
|
+
def initialize(value:)
|
11
|
+
super()
|
12
|
+
|
13
|
+
@value = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def resolve(*)
|
17
|
+
@value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Corindon
|
4
|
+
module Ext
|
5
|
+
module Guards
|
6
|
+
refine Object do
|
7
|
+
def unimplemented!(message = nil)
|
8
|
+
raise Error.new(message || "This method is not implemented.")
|
9
|
+
end
|
10
|
+
|
11
|
+
def unreachable!(message = nil)
|
12
|
+
raise Error.new(message || "Reached unreachable code.")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/corindon/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: corindon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rémi Piotaix
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-09-
|
11
|
+
date: 2020-09-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: semantic
|
@@ -88,11 +88,23 @@ extra_rdoc_files: []
|
|
88
88
|
files:
|
89
89
|
- corindon.gemspec
|
90
90
|
- lib/corindon.rb
|
91
|
+
- lib/corindon/console/application.rb
|
92
|
+
- lib/corindon/console/base_command.rb
|
93
|
+
- lib/corindon/console/input.rb
|
94
|
+
- lib/corindon/console/output.rb
|
91
95
|
- lib/corindon/dependency_injection/container.rb
|
92
96
|
- lib/corindon/dependency_injection/definition.rb
|
93
97
|
- lib/corindon/dependency_injection/dsl.rb
|
98
|
+
- lib/corindon/dependency_injection/id/id_generator.rb
|
99
|
+
- lib/corindon/dependency_injection/id/uuid_generator.rb
|
94
100
|
- lib/corindon/dependency_injection/injectable.rb
|
95
101
|
- lib/corindon/dependency_injection/injector.rb
|
102
|
+
- lib/corindon/dependency_injection/token/injection_token.rb
|
103
|
+
- lib/corindon/dependency_injection/token/service_call_token.rb
|
104
|
+
- lib/corindon/dependency_injection/token/tagged_token.rb
|
105
|
+
- lib/corindon/dependency_injection/token/value_token.rb
|
106
|
+
- lib/corindon/ext/guards.rb
|
107
|
+
- lib/corindon/ext/guards/error.rb
|
96
108
|
- lib/corindon/ext/something.rb
|
97
109
|
- lib/corindon/version.rb
|
98
110
|
homepage: https://gitlab.com/piotaixr/corindon
|