corindon 0.2.0 → 0.7.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/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 +124 -20
- data/lib/corindon/dependency_injection/definition.rb +17 -26
- data/lib/corindon/dependency_injection/dsl.rb +62 -7
- data/lib/corindon/dependency_injection/injectable.rb +22 -8
- data/lib/corindon/dependency_injection/injector.rb +14 -15
- data/lib/corindon/dependency_injection/ruby_compat.rb +27 -0
- data/lib/corindon/dependency_injection/token/injection_token.rb +12 -0
- data/lib/corindon/dependency_injection/token/parameter_token.rb +22 -0
- data/lib/corindon/dependency_injection/token/service_call_token.rb +40 -0
- data/lib/corindon/dependency_injection/token/service_factory_token.rb +24 -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/guards/ext.rb +17 -0
- data/lib/corindon/result/errors/bad_return_type_error.rb +19 -0
- data/lib/corindon/result/errors/result_error.rb +10 -0
- data/lib/corindon/result/ext.rb +25 -0
- data/lib/corindon/result/failure.rb +27 -0
- data/lib/corindon/result/result.rb +32 -0
- data/lib/corindon/result/success.rb +39 -0
- data/lib/corindon/{ext/something.rb → something/ext.rb} +2 -2
- data/lib/corindon/version.rb +1 -1
- metadata +21 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e23965fecffcb108574ea3e42ae2889965914a65ebc80f71fbdb3f40e3b67e34
|
4
|
+
data.tar.gz: 96efc5116431669ef5ae2d88fa1945ba38d90fb14de3d35e7182154039efbde1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b87c39aaa85e776fe0289cf9f97eafc80ce093d44775fb5753d864aa5e1ad1537c93b4f99dadce284ae4071f41ee15a03f873ee6230a3c2a246df5acb1fcd0c8
|
7
|
+
data.tar.gz: 13ff2f71fc510903f7430e4e37e528cc122a627d8e5228684c922c41b076aaf84a45f6620bcb9755c42a9e66676d117e2b83ef56218980abbe92550f4434f257
|
@@ -3,34 +3,92 @@
|
|
3
3
|
module Corindon
|
4
4
|
module DependencyInjection
|
5
5
|
class Container
|
6
|
-
using Ext
|
6
|
+
using Something::Ext
|
7
7
|
|
8
|
-
attr_reader :definitions
|
9
8
|
attr_reader :injector
|
10
|
-
attr_reader :services
|
11
9
|
|
12
|
-
|
10
|
+
# @param [Array<Definition>] definitions
|
11
|
+
def initialize(definitions: [], parameters: {}, service_built_listeners: [])
|
13
12
|
@services = {}
|
14
13
|
@definitions = {}
|
14
|
+
@parameters = parameters
|
15
|
+
@tags = Hash.new { |hash, key| hash[key] = [] }
|
15
16
|
|
16
17
|
@injector = Injector.new(container: self)
|
18
|
+
|
19
|
+
definitions.each { |d| register_definition(d) }
|
20
|
+
|
21
|
+
@service_built_listeners = service_built_listeners
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Container]
|
25
|
+
def dup
|
26
|
+
Container.new(
|
27
|
+
definitions: definitions.values,
|
28
|
+
parameters: parameters.dup,
|
29
|
+
service_built_listeners: service_built_listeners
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
# @param [Definition, Injectable, Class]
|
34
|
+
# @return [Definition]
|
35
|
+
def as_definition(def_or_injectable)
|
36
|
+
if def_or_injectable.is_a?(Definition)
|
37
|
+
def_or_injectable
|
38
|
+
elsif def_or_injectable.is_a?(Injectable)
|
39
|
+
def_or_injectable.definition
|
40
|
+
elsif def_or_injectable.is_a?(Class)
|
41
|
+
Definition.new(def_or_injectable)
|
42
|
+
else
|
43
|
+
raise StandardError.new("Don't know how to build #{def_or_injectable}")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# @param [Class, Injectable, Definition] def_or_injectable
|
48
|
+
# @return [String]
|
49
|
+
def add_definition(def_or_injectable, context: {}, &block)
|
50
|
+
definition = as_definition(def_or_injectable)
|
51
|
+
|
52
|
+
if block.sth?
|
53
|
+
definition = Dsl.from_definition(definition).exec(context: context, &block)
|
54
|
+
end
|
55
|
+
|
56
|
+
id = definition.id || to_id(def_or_injectable)
|
57
|
+
|
58
|
+
register_definition(
|
59
|
+
Definition.new(
|
60
|
+
definition.object_source,
|
61
|
+
id: id,
|
62
|
+
args: definition.args,
|
63
|
+
kwargs: definition.kwargs,
|
64
|
+
calls: definition.calls,
|
65
|
+
tags: definition.tags
|
66
|
+
)
|
67
|
+
)
|
68
|
+
|
69
|
+
id
|
17
70
|
end
|
18
71
|
|
19
|
-
# @param [
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
72
|
+
# @param [String] tag
|
73
|
+
# @return [Array<String>]
|
74
|
+
def tagged(tag)
|
75
|
+
if tags.key?(tag)
|
76
|
+
|
77
|
+
tags.fetch(tag)
|
78
|
+
else
|
79
|
+
[]
|
80
|
+
end
|
28
81
|
end
|
29
82
|
|
30
83
|
# @param [Class, #to_s] key
|
31
84
|
# @return [Boolean]
|
32
85
|
def has?(key)
|
33
|
-
|
86
|
+
definitions.key?(to_id(key))
|
87
|
+
end
|
88
|
+
|
89
|
+
# Clears all the cache of services
|
90
|
+
def clear
|
91
|
+
@services = {}
|
34
92
|
end
|
35
93
|
|
36
94
|
# @param [Class, #to_s] key
|
@@ -47,21 +105,67 @@ module Corindon
|
|
47
105
|
end
|
48
106
|
end
|
49
107
|
|
108
|
+
# @param [Class, #to_s] key
|
109
|
+
# @param [Object] value
|
110
|
+
def set_parameter(name, value)
|
111
|
+
parameters[to_id(name)] = value
|
112
|
+
end
|
113
|
+
|
114
|
+
# @param [Class, #to_s] key
|
115
|
+
# @return [Boolean]
|
116
|
+
def parameter?(key)
|
117
|
+
parameters.key?(to_id(key))
|
118
|
+
end
|
119
|
+
|
120
|
+
# @param [Class, #to_s] key
|
121
|
+
# @return [Object]
|
122
|
+
def parameter(key)
|
123
|
+
parameters.fetch(to_id(key))
|
124
|
+
end
|
125
|
+
|
126
|
+
# @param [Proc{Object, Container}] listener
|
127
|
+
def on_service_built(listener)
|
128
|
+
service_built_listeners << listener
|
129
|
+
end
|
130
|
+
|
50
131
|
private
|
51
132
|
|
133
|
+
# @return [Hash{String=>Definition}]
|
134
|
+
attr_reader :definitions
|
135
|
+
attr_reader :parameters
|
136
|
+
attr_reader :services
|
137
|
+
attr_reader :tags
|
138
|
+
attr_reader :service_built_listeners
|
139
|
+
|
140
|
+
def register_definition(definition)
|
141
|
+
definitions[definition.id] = definition
|
142
|
+
definition.tags.each { |tag| tags[tag] << definition.id }
|
143
|
+
end
|
144
|
+
|
52
145
|
def build_service(id)
|
53
|
-
|
54
|
-
|
146
|
+
service = injector.resolve(definitions.fetch(id)).tap do |svc|
|
147
|
+
services[id] = svc
|
55
148
|
end
|
149
|
+
|
150
|
+
service_built_listeners.each do |listener|
|
151
|
+
listener.call(service, self)
|
152
|
+
end
|
153
|
+
|
154
|
+
service
|
56
155
|
end
|
57
156
|
|
58
|
-
def injectable?(
|
59
|
-
|
157
|
+
def injectable?(object)
|
158
|
+
object.is_a?(Injectable)
|
60
159
|
end
|
61
160
|
|
62
|
-
# @param [Class, #to_s] key
|
161
|
+
# @param [Injectable, Identifiable, Class, Definition, #to_s] key
|
162
|
+
# @return [String]
|
63
163
|
def to_id(key)
|
64
|
-
if key.is_a?(
|
164
|
+
if key.is_a?(Definition)
|
165
|
+
key.id
|
166
|
+
elsif injectable?(key)
|
167
|
+
to_id(key.definition)
|
168
|
+
elsif key.is_a?(Class)
|
65
169
|
key.name
|
66
170
|
else
|
67
171
|
key.to_s
|
@@ -5,49 +5,40 @@ require 'semantic'
|
|
5
5
|
module Corindon
|
6
6
|
module DependencyInjection
|
7
7
|
class Definition
|
8
|
-
attr_reader :
|
8
|
+
attr_reader :object_source
|
9
9
|
attr_reader :args
|
10
10
|
attr_reader :kwargs
|
11
11
|
attr_reader :calls
|
12
|
+
attr_reader :tags
|
13
|
+
# @return [String]
|
14
|
+
attr_reader :id
|
12
15
|
|
13
|
-
def initialize(
|
14
|
-
@
|
16
|
+
def initialize(object_source, args: [], kwargs: {}, calls: [], tags: [], id: nil)
|
17
|
+
@object_source = object_source
|
15
18
|
@args = args
|
16
19
|
@kwargs = kwargs
|
17
20
|
@calls = calls
|
21
|
+
@tags = tags
|
22
|
+
@id = id
|
18
23
|
end
|
19
24
|
|
20
25
|
# @param [Injector] injector
|
21
|
-
#
|
26
|
+
# @return [Object]
|
22
27
|
def build(injector)
|
23
|
-
|
28
|
+
source = if object_source.is_a?(Class)
|
29
|
+
[object_source, :new]
|
30
|
+
else
|
31
|
+
injector.resolve(object_source)
|
32
|
+
end
|
33
|
+
|
34
|
+
object = RubyCompat.do_call(*source, injector.resolve(args), injector.resolve(kwargs))
|
24
35
|
|
25
36
|
calls.each do |(call, call_args, call_kwargs)|
|
26
|
-
do_call(object, call,
|
37
|
+
RubyCompat.do_call(object, call, injector.resolve(call_args), injector.resolve(call_kwargs))
|
27
38
|
end
|
28
39
|
|
29
40
|
object
|
30
41
|
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
if ::Semantic::Version.new(RUBY_VERSION).satisfies?(">= 2.7.0")
|
35
|
-
def do_call(obj, method, args, kwargs)
|
36
|
-
obj.send(method, *args, **kwargs)
|
37
|
-
end
|
38
|
-
else
|
39
|
-
def do_call(obj, method, args, kwargs)
|
40
|
-
if args.empty? && kwargs.empty?
|
41
|
-
obj.send(method)
|
42
|
-
elsif args.empty?
|
43
|
-
obj.send(method, **kwargs)
|
44
|
-
elsif kwargs.empty?
|
45
|
-
obj.send(method, *args)
|
46
|
-
else
|
47
|
-
obj.send(method, *args, **kwargs)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
42
|
end
|
52
43
|
end
|
53
44
|
end
|
@@ -3,19 +3,41 @@
|
|
3
3
|
module Corindon
|
4
4
|
module DependencyInjection
|
5
5
|
class Dsl
|
6
|
+
class << self
|
7
|
+
# @param [Definition] definition
|
8
|
+
# @return [Dsl]
|
9
|
+
def from_definition(definition)
|
10
|
+
new(
|
11
|
+
definition.object_source,
|
12
|
+
id: definition.id,
|
13
|
+
args: definition.args,
|
14
|
+
kwargs: definition.kwargs,
|
15
|
+
calls: definition.calls,
|
16
|
+
tags: definition.tags
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
6
21
|
# @param [Class] klass
|
7
|
-
def initialize(klass)
|
22
|
+
def initialize(klass, args: [], kwargs: {}, id: nil, calls: [], tags: [])
|
8
23
|
@klass = klass
|
9
|
-
@args =
|
10
|
-
@kwargs =
|
11
|
-
@calls =
|
24
|
+
@args = args
|
25
|
+
@kwargs = kwargs
|
26
|
+
@calls = calls
|
27
|
+
@tags = tags
|
28
|
+
@id = id
|
12
29
|
end
|
13
30
|
|
31
|
+
# @param [Hash] context
|
14
32
|
# @return [Definition]
|
15
|
-
def exec(&block)
|
16
|
-
|
33
|
+
def exec(context: {}, &block)
|
34
|
+
if context.is_a?(Hash)
|
35
|
+
context = OpenStruct.new(context)
|
36
|
+
end
|
17
37
|
|
18
|
-
|
38
|
+
instance_exec(context, &block)
|
39
|
+
|
40
|
+
Definition.new(@klass, args: @args, kwargs: @kwargs, calls: @calls, tags: @tags, id: @id)
|
19
41
|
end
|
20
42
|
|
21
43
|
def args(*arguments, **kv_arguments)
|
@@ -27,6 +49,39 @@ module Corindon
|
|
27
49
|
def call(name, *arguments, **kv_arguments)
|
28
50
|
@calls << [name, arguments, kv_arguments]
|
29
51
|
end
|
52
|
+
|
53
|
+
# @param [String] id
|
54
|
+
def id(id)
|
55
|
+
@anonymous = false
|
56
|
+
@id = id
|
57
|
+
end
|
58
|
+
|
59
|
+
# @param [Class, #to_s] key
|
60
|
+
# @return [Token::ParameterToken]
|
61
|
+
def param(key)
|
62
|
+
Token::ParameterToken.new(key: key)
|
63
|
+
end
|
64
|
+
|
65
|
+
# @param [String] tag
|
66
|
+
def tag(tag)
|
67
|
+
@tags << tag
|
68
|
+
end
|
69
|
+
|
70
|
+
# @return [Token::ServiceCallToken]
|
71
|
+
def on(id)
|
72
|
+
Token::ServiceCallToken.new(service: id)
|
73
|
+
end
|
74
|
+
|
75
|
+
# @return [Token::ValueToken]
|
76
|
+
def value(val)
|
77
|
+
Token::ValueToken.new(value: val)
|
78
|
+
end
|
79
|
+
|
80
|
+
# @param [String] tag
|
81
|
+
# @return [Token::TaggedToken]
|
82
|
+
def tagged(tag)
|
83
|
+
Token::TaggedToken.new(tag)
|
84
|
+
end
|
30
85
|
end
|
31
86
|
end
|
32
87
|
end
|
@@ -3,22 +3,36 @@
|
|
3
3
|
module Corindon
|
4
4
|
module DependencyInjection
|
5
5
|
module Injectable
|
6
|
-
def definition
|
7
|
-
Definition.new(self)
|
8
|
-
end
|
9
|
-
|
10
6
|
refine Class do
|
7
|
+
def factory(service, method)
|
8
|
+
Token::ServiceFactoryToken.new(service, method)
|
9
|
+
end
|
10
|
+
|
11
|
+
def make_parameter(name)
|
12
|
+
Token::ParameterToken.new(key: "#{self.name.downcase.gsub(/::/, '.')}.#{name}")
|
13
|
+
end
|
14
|
+
|
15
|
+
def make_definition(name, source, *args, **kwargs, &block)
|
16
|
+
do_make_definition("#{self.name.downcase.gsub(/::/, '.')}.#{name}", source, args: args, kwargs: kwargs, &block)
|
17
|
+
end
|
18
|
+
|
11
19
|
def injectable(*args, **kwargs, &block)
|
12
|
-
|
20
|
+
extend Injectable
|
13
21
|
|
14
22
|
define_singleton_method :definition do
|
23
|
+
do_make_definition(name, self, args: args, kwargs: kwargs, &block)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def do_make_definition(name, source, args:, kwargs:, &block)
|
15
30
|
if block.nil?
|
16
|
-
Definition.new(
|
31
|
+
Definition.new(source, args: args, kwargs: kwargs, id: name)
|
17
32
|
else
|
18
|
-
Dsl.new(
|
33
|
+
Dsl.new(source, args: args, kwargs: kwargs, id: name).exec(&block)
|
19
34
|
end
|
20
35
|
end
|
21
|
-
end
|
22
36
|
end
|
23
37
|
end
|
24
38
|
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,20 @@ module Corindon
|
|
10
11
|
@container = container
|
11
12
|
end
|
12
13
|
|
13
|
-
# @
|
14
|
-
def resolve(
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
+
elsif value.is_a?(Definition)
|
23
|
+
value.build(self)
|
24
|
+
else
|
25
|
+
container.get(value)
|
28
26
|
end
|
27
|
+
end
|
29
28
|
end
|
30
29
|
end
|
31
30
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Corindon
|
4
|
+
module DependencyInjection
|
5
|
+
class RubyCompat
|
6
|
+
class << self
|
7
|
+
if ::Semantic::Version.new(RUBY_VERSION).satisfies?(">= 2.7.0")
|
8
|
+
def do_call(obj, method, args, kwargs)
|
9
|
+
obj.send(method, *args, **kwargs)
|
10
|
+
end
|
11
|
+
else
|
12
|
+
def do_call(obj, method, args, kwargs)
|
13
|
+
if args.empty? && kwargs.empty?
|
14
|
+
obj.send(method)
|
15
|
+
elsif args.empty?
|
16
|
+
obj.send(method, **kwargs)
|
17
|
+
elsif kwargs.empty?
|
18
|
+
obj.send(method, *args)
|
19
|
+
else
|
20
|
+
obj.send(method, *args, **kwargs)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Corindon
|
4
|
+
module DependencyInjection
|
5
|
+
module Token
|
6
|
+
class ParameterToken < InjectionToken
|
7
|
+
attr_reader :key
|
8
|
+
|
9
|
+
def initialize(key:)
|
10
|
+
super()
|
11
|
+
|
12
|
+
@key = key
|
13
|
+
end
|
14
|
+
|
15
|
+
# @param [Injector] injector
|
16
|
+
def resolve(injector:)
|
17
|
+
injector.container.parameter(key)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,40 @@
|
|
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
|
+
RubyCompat.do_call(
|
31
|
+
injector.resolve(service),
|
32
|
+
method,
|
33
|
+
injector.resolve(args),
|
34
|
+
injector.resolve(kwargs)
|
35
|
+
)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Corindon
|
4
|
+
module DependencyInjection
|
5
|
+
module Token
|
6
|
+
class ServiceFactoryToken < InjectionToken
|
7
|
+
attr_reader :service
|
8
|
+
attr_reader :method
|
9
|
+
|
10
|
+
def initialize(service, method)
|
11
|
+
super()
|
12
|
+
|
13
|
+
@service = service
|
14
|
+
@method = method
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param [Injector] injector
|
18
|
+
def resolve(injector:)
|
19
|
+
[injector.resolve(service), method]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
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 Guards
|
5
|
+
module Ext
|
6
|
+
refine Object do
|
7
|
+
def unimplemented!(message = nil)
|
8
|
+
raise NotImplementedError.new(message || "This method is not implemented.")
|
9
|
+
end
|
10
|
+
|
11
|
+
def unreachable!(message = nil)
|
12
|
+
raise StandardError.new(message || "Reached unreachable code.")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Corindon
|
4
|
+
module Result
|
5
|
+
module Errors
|
6
|
+
class BadReturnTypeError < ResultError
|
7
|
+
# @return [Object]
|
8
|
+
attr_reader :value
|
9
|
+
|
10
|
+
# @param [Object] value
|
11
|
+
def initialize(value)
|
12
|
+
super("Expected a Result, got #{value}")
|
13
|
+
|
14
|
+
@value = value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Corindon
|
4
|
+
module Result
|
5
|
+
module Ext
|
6
|
+
refine Object do
|
7
|
+
def rescue_failure(&block)
|
8
|
+
block.call
|
9
|
+
rescue StandardError => error
|
10
|
+
Corindon::Result::Failure.new(error)
|
11
|
+
end
|
12
|
+
|
13
|
+
# rubocop:disable Naming/MethodName
|
14
|
+
def Failure(error)
|
15
|
+
Corindon::Result::Failure.new(error)
|
16
|
+
end
|
17
|
+
|
18
|
+
def Success(value)
|
19
|
+
Corindon::Result::Success.new(value)
|
20
|
+
end
|
21
|
+
# rubocop:enable Naming/MethodName
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Corindon
|
4
|
+
module Result
|
5
|
+
class Failure < Result
|
6
|
+
# @return [Exception]
|
7
|
+
attr_reader :error
|
8
|
+
|
9
|
+
# @param [Exception] error
|
10
|
+
def initialize(error)
|
11
|
+
super()
|
12
|
+
|
13
|
+
@error = error
|
14
|
+
end
|
15
|
+
|
16
|
+
# @raise [Exception]
|
17
|
+
def unwrap!
|
18
|
+
raise error
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [Boolean]
|
22
|
+
def failure?
|
23
|
+
true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
using Corindon::Guards::Ext
|
4
|
+
|
5
|
+
module Corindon
|
6
|
+
module Result
|
7
|
+
class Result
|
8
|
+
# @return [Boolean]
|
9
|
+
def success?
|
10
|
+
false
|
11
|
+
end
|
12
|
+
|
13
|
+
# @return [Boolean]
|
14
|
+
def failure?
|
15
|
+
false
|
16
|
+
end
|
17
|
+
|
18
|
+
# @raise [Exception] if called on a Failure
|
19
|
+
# @return [Object]
|
20
|
+
def unwrap!
|
21
|
+
unimplemented!
|
22
|
+
end
|
23
|
+
|
24
|
+
# @yieldparam [Object] value
|
25
|
+
# @yieldreturn [Result]
|
26
|
+
# @return [Result]
|
27
|
+
def and_then(&_block)
|
28
|
+
self
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Corindon
|
4
|
+
module Result
|
5
|
+
class Success < Result
|
6
|
+
# @return [Object]
|
7
|
+
attr_reader :value
|
8
|
+
|
9
|
+
# @param [Object] value
|
10
|
+
def initialize(value)
|
11
|
+
super()
|
12
|
+
|
13
|
+
@value = value
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [Boolean]
|
17
|
+
def success?
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [Object]
|
22
|
+
def unwrap!
|
23
|
+
value
|
24
|
+
end
|
25
|
+
|
26
|
+
def and_then(&block)
|
27
|
+
retval = block.call(value)
|
28
|
+
|
29
|
+
if retval.is_a?(Result)
|
30
|
+
retval
|
31
|
+
else
|
32
|
+
Failure.new(Errors::BadReturnTypeError.new(retval))
|
33
|
+
end
|
34
|
+
rescue StandardError => error
|
35
|
+
Failure.new(error)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
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.7.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-
|
11
|
+
date: 2020-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: semantic
|
@@ -88,12 +88,30 @@ 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
|
94
98
|
- lib/corindon/dependency_injection/injectable.rb
|
95
99
|
- lib/corindon/dependency_injection/injector.rb
|
96
|
-
- lib/corindon/
|
100
|
+
- lib/corindon/dependency_injection/ruby_compat.rb
|
101
|
+
- lib/corindon/dependency_injection/token/injection_token.rb
|
102
|
+
- lib/corindon/dependency_injection/token/parameter_token.rb
|
103
|
+
- lib/corindon/dependency_injection/token/service_call_token.rb
|
104
|
+
- lib/corindon/dependency_injection/token/service_factory_token.rb
|
105
|
+
- lib/corindon/dependency_injection/token/tagged_token.rb
|
106
|
+
- lib/corindon/dependency_injection/token/value_token.rb
|
107
|
+
- lib/corindon/guards/ext.rb
|
108
|
+
- lib/corindon/result/errors/bad_return_type_error.rb
|
109
|
+
- lib/corindon/result/errors/result_error.rb
|
110
|
+
- lib/corindon/result/ext.rb
|
111
|
+
- lib/corindon/result/failure.rb
|
112
|
+
- lib/corindon/result/result.rb
|
113
|
+
- lib/corindon/result/success.rb
|
114
|
+
- lib/corindon/something/ext.rb
|
97
115
|
- lib/corindon/version.rb
|
98
116
|
homepage: https://gitlab.com/piotaixr/corindon
|
99
117
|
licenses:
|