corindon 0.3.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/corindon.gemspec +1 -0
- data/lib/corindon/dependency_injection/container.rb +109 -33
- data/lib/corindon/dependency_injection/definition.rb +13 -32
- data/lib/corindon/dependency_injection/dsl.rb +30 -11
- data/lib/corindon/dependency_injection/injectable.rb +22 -8
- data/lib/corindon/dependency_injection/injector.rb +2 -0
- data/lib/corindon/dependency_injection/ruby_compat.rb +27 -0
- data/lib/corindon/dependency_injection/testing/mock_utils.rb +21 -0
- data/lib/corindon/dependency_injection/token/parameter_token.rb +22 -0
- data/lib/corindon/dependency_injection/token/service_call_token.rb +1 -21
- data/lib/corindon/dependency_injection/token/service_factory_token.rb +24 -0
- data/lib/corindon/{ext/guards.rb → guards/ext.rb} +4 -4
- data/lib/corindon/result/errors/bad_return_type_error.rb +19 -0
- data/lib/corindon/{ext/guards/error.rb → result/errors/result_error.rb} +3 -3
- 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 +28 -11
- data/lib/corindon/console/application.rb +0 -15
- data/lib/corindon/console/base_command.rb +0 -13
- data/lib/corindon/console/input.rb +0 -8
- data/lib/corindon/console/output.rb +0 -8
- data/lib/corindon/dependency_injection/id/id_generator.rb +0 -12
- data/lib/corindon/dependency_injection/id/uuid_generator.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cca83eddfe861552adada0fd1f570d4fe8cb4ae9621be1b8ba183f4ce75d2e0a
|
4
|
+
data.tar.gz: a4f6192afef7522bc45be5a3b448b1366a1e18cc6318b8dd95e7e88021b12070
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64b64ba2e60584ccf94737cae4770f972a45ef72ba3421f7131f1727cff2da7b6b2001537f6d054338a3a3edb9941a0655dff0d00eeaf2c85c0bd64e0ebd7da0
|
7
|
+
data.tar.gz: fc7a43a1ff7490622cb87c49805957b6a8b1e0ce488e64bc9cc23f2edb75f135f0d50bc376b128f88a3b01965d3cdc8a84c7b87a8918991eeb6aca9505db1fc4
|
data/corindon.gemspec
CHANGED
@@ -3,43 +3,68 @@
|
|
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
|
-
attr_reader :id_generator
|
10
8
|
attr_reader :injector
|
11
|
-
attr_reader :services
|
12
9
|
|
13
|
-
# @param [
|
14
|
-
def initialize(
|
15
|
-
@id_generator = id_generator
|
10
|
+
# @param [Array<Definition>] definitions
|
11
|
+
def initialize(definitions: [], parameters: {}, service_built_listeners: [])
|
16
12
|
@services = {}
|
17
13
|
@definitions = {}
|
14
|
+
@parameters = parameters
|
18
15
|
@tags = Hash.new { |hash, key| hash[key] = [] }
|
19
16
|
|
20
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
|
21
45
|
end
|
22
46
|
|
23
|
-
# @param [Class]
|
47
|
+
# @param [Class, Injectable, Definition] def_or_injectable
|
24
48
|
# @return [String]
|
25
|
-
def add_definition(
|
26
|
-
definition =
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
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)
|
38
54
|
end
|
39
55
|
|
40
|
-
id =
|
41
|
-
|
42
|
-
|
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
|
+
)
|
43
68
|
|
44
69
|
id
|
45
70
|
end
|
@@ -47,9 +72,9 @@ module Corindon
|
|
47
72
|
# @param [String] tag
|
48
73
|
# @return [Array<String>]
|
49
74
|
def tagged(tag)
|
50
|
-
if
|
75
|
+
if tags.key?(tag)
|
51
76
|
|
52
|
-
|
77
|
+
tags.fetch(tag)
|
53
78
|
else
|
54
79
|
[]
|
55
80
|
end
|
@@ -58,7 +83,12 @@ module Corindon
|
|
58
83
|
# @param [Class, #to_s] key
|
59
84
|
# @return [Boolean]
|
60
85
|
def has?(key)
|
61
|
-
|
86
|
+
definitions.key?(to_id(key))
|
87
|
+
end
|
88
|
+
|
89
|
+
# Clears all the cache of services
|
90
|
+
def clear
|
91
|
+
@services = {}
|
62
92
|
end
|
63
93
|
|
64
94
|
# @param [Class, #to_s] key
|
@@ -75,21 +105,67 @@ module Corindon
|
|
75
105
|
end
|
76
106
|
end
|
77
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
|
+
|
78
131
|
private
|
79
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
|
+
|
80
145
|
def build_service(id)
|
81
|
-
|
82
|
-
|
146
|
+
service = injector.resolve(definitions.fetch(id)).tap do |svc|
|
147
|
+
services[id] = svc
|
148
|
+
end
|
149
|
+
|
150
|
+
service_built_listeners.each do |listener|
|
151
|
+
listener.call(service, self)
|
83
152
|
end
|
153
|
+
|
154
|
+
service
|
84
155
|
end
|
85
156
|
|
86
|
-
def injectable?(
|
87
|
-
|
157
|
+
def injectable?(object)
|
158
|
+
object.is_a?(Injectable)
|
88
159
|
end
|
89
160
|
|
90
|
-
# @param [Class, #to_s] key
|
161
|
+
# @param [Injectable, Identifiable, Class, Definition, #to_s] key
|
162
|
+
# @return [String]
|
91
163
|
def to_id(key)
|
92
|
-
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)
|
93
169
|
key.name
|
94
170
|
else
|
95
171
|
key.to_s
|
@@ -5,59 +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
12
|
attr_reader :tags
|
13
|
+
# @return [String]
|
13
14
|
attr_reader :id
|
14
15
|
|
15
|
-
def initialize(
|
16
|
-
@
|
16
|
+
def initialize(object_source, args: [], kwargs: {}, calls: [], tags: [], id: nil)
|
17
|
+
@object_source = object_source
|
17
18
|
@args = args
|
18
19
|
@kwargs = kwargs
|
19
20
|
@calls = calls
|
20
21
|
@tags = tags
|
21
22
|
@id = id
|
22
|
-
@anonymous = anonymous
|
23
23
|
end
|
24
24
|
|
25
25
|
# @param [Injector] injector
|
26
|
-
#
|
26
|
+
# @return [Object]
|
27
27
|
def build(injector)
|
28
|
-
|
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))
|
29
35
|
|
30
36
|
calls.each do |(call, call_args, call_kwargs)|
|
31
|
-
do_call(object, call, injector.resolve(call_args), injector.resolve(call_kwargs))
|
37
|
+
RubyCompat.do_call(object, call, injector.resolve(call_args), injector.resolve(call_kwargs))
|
32
38
|
end
|
33
39
|
|
34
40
|
object
|
35
41
|
end
|
36
|
-
|
37
|
-
# @return [Boolean]
|
38
|
-
def anonymous?
|
39
|
-
@anonymous
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
if ::Semantic::Version.new(RUBY_VERSION).satisfies?(">= 2.7.0")
|
45
|
-
def do_call(obj, method, args, kwargs)
|
46
|
-
obj.send(method, *args, **kwargs)
|
47
|
-
end
|
48
|
-
else
|
49
|
-
def do_call(obj, method, args, kwargs)
|
50
|
-
if args.empty? && kwargs.empty?
|
51
|
-
obj.send(method)
|
52
|
-
elsif args.empty?
|
53
|
-
obj.send(method, **kwargs)
|
54
|
-
elsif kwargs.empty?
|
55
|
-
obj.send(method, *args)
|
56
|
-
else
|
57
|
-
obj.send(method, *args, **kwargs)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
42
|
end
|
62
43
|
end
|
63
44
|
end
|
@@ -3,22 +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, args: [], kwargs: {}, id: nil,
|
22
|
+
def initialize(klass, args: [], kwargs: {}, id: nil, calls: [], tags: [])
|
8
23
|
@klass = klass
|
9
24
|
@args = args
|
10
25
|
@kwargs = kwargs
|
11
|
-
@calls =
|
12
|
-
@tags =
|
26
|
+
@calls = calls
|
27
|
+
@tags = tags
|
13
28
|
@id = id
|
14
|
-
@anonymous = anonymous
|
15
29
|
end
|
16
30
|
|
31
|
+
# @param [Hash] context
|
17
32
|
# @return [Definition]
|
18
|
-
def exec(&block)
|
19
|
-
|
33
|
+
def exec(context: {}, &block)
|
34
|
+
if context.is_a?(Hash)
|
35
|
+
context = OpenStruct.new(context)
|
36
|
+
end
|
37
|
+
|
38
|
+
instance_exec(context, &block)
|
20
39
|
|
21
|
-
Definition.new(@klass, args: @args, kwargs: @kwargs, calls: @calls, tags: @tags, id: @id
|
40
|
+
Definition.new(@klass, args: @args, kwargs: @kwargs, calls: @calls, tags: @tags, id: @id)
|
22
41
|
end
|
23
42
|
|
24
43
|
def args(*arguments, **kv_arguments)
|
@@ -33,13 +52,13 @@ module Corindon
|
|
33
52
|
|
34
53
|
# @param [String] id
|
35
54
|
def id(id)
|
36
|
-
@anonymous = false
|
37
55
|
@id = id
|
38
56
|
end
|
39
57
|
|
40
|
-
|
41
|
-
|
42
|
-
|
58
|
+
# @param [Class, #to_s] key
|
59
|
+
# @return [Token::ParameterToken]
|
60
|
+
def param(key)
|
61
|
+
Token::ParameterToken.new(key: key)
|
43
62
|
end
|
44
63
|
|
45
64
|
# @param [String] tag
|
@@ -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
|
@@ -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,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Corindon
|
4
|
+
module DependencyInjection
|
5
|
+
module Testing
|
6
|
+
# Defines the `mock_definition` on Container allowing to mock services returned by the container for a given Definition
|
7
|
+
module MockUtils
|
8
|
+
refine Container do
|
9
|
+
# Allow to provide a specific value that the definition will resolve to
|
10
|
+
# @param [Definition] definition
|
11
|
+
# @param [Object] value
|
12
|
+
def mock_definition(definition, value)
|
13
|
+
add_definition(definition) if !has?(definition)
|
14
|
+
|
15
|
+
services[to_id(definition)] = value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
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
|
@@ -27,33 +27,13 @@ module Corindon
|
|
27
27
|
|
28
28
|
# @param [Injector] injector
|
29
29
|
def resolve(injector:)
|
30
|
-
do_call(
|
30
|
+
RubyCompat.do_call(
|
31
31
|
injector.resolve(service),
|
32
32
|
method,
|
33
33
|
injector.resolve(args),
|
34
34
|
injector.resolve(kwargs)
|
35
35
|
)
|
36
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
37
|
end
|
58
38
|
end
|
59
39
|
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
|
@@ -1,15 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Corindon
|
4
|
-
module
|
5
|
-
module
|
4
|
+
module Guards
|
5
|
+
module Ext
|
6
6
|
refine Object do
|
7
7
|
def unimplemented!(message = nil)
|
8
|
-
raise
|
8
|
+
raise NotImplementedError.new(message || 'This method is not implemented.')
|
9
9
|
end
|
10
10
|
|
11
11
|
def unreachable!(message = nil)
|
12
|
-
raise
|
12
|
+
raise StandardError.new(message || 'Reached unreachable code.')
|
13
13
|
end
|
14
14
|
end
|
15
15
|
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.1
|
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-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: semantic
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '13.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.3.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.3.1
|
83
97
|
description:
|
84
98
|
email: remi@piotaix.fr
|
85
99
|
executables: []
|
@@ -88,24 +102,27 @@ extra_rdoc_files: []
|
|
88
102
|
files:
|
89
103
|
- corindon.gemspec
|
90
104
|
- 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
|
95
105
|
- lib/corindon/dependency_injection/container.rb
|
96
106
|
- lib/corindon/dependency_injection/definition.rb
|
97
107
|
- lib/corindon/dependency_injection/dsl.rb
|
98
|
-
- lib/corindon/dependency_injection/id/id_generator.rb
|
99
|
-
- lib/corindon/dependency_injection/id/uuid_generator.rb
|
100
108
|
- lib/corindon/dependency_injection/injectable.rb
|
101
109
|
- lib/corindon/dependency_injection/injector.rb
|
110
|
+
- lib/corindon/dependency_injection/ruby_compat.rb
|
111
|
+
- lib/corindon/dependency_injection/testing/mock_utils.rb
|
102
112
|
- lib/corindon/dependency_injection/token/injection_token.rb
|
113
|
+
- lib/corindon/dependency_injection/token/parameter_token.rb
|
103
114
|
- lib/corindon/dependency_injection/token/service_call_token.rb
|
115
|
+
- lib/corindon/dependency_injection/token/service_factory_token.rb
|
104
116
|
- lib/corindon/dependency_injection/token/tagged_token.rb
|
105
117
|
- lib/corindon/dependency_injection/token/value_token.rb
|
106
|
-
- lib/corindon/ext
|
107
|
-
- lib/corindon/
|
108
|
-
- lib/corindon/
|
118
|
+
- lib/corindon/guards/ext.rb
|
119
|
+
- lib/corindon/result/errors/bad_return_type_error.rb
|
120
|
+
- lib/corindon/result/errors/result_error.rb
|
121
|
+
- lib/corindon/result/ext.rb
|
122
|
+
- lib/corindon/result/failure.rb
|
123
|
+
- lib/corindon/result/result.rb
|
124
|
+
- lib/corindon/result/success.rb
|
125
|
+
- lib/corindon/something/ext.rb
|
109
126
|
- lib/corindon/version.rb
|
110
127
|
homepage: https://gitlab.com/piotaixr/corindon
|
111
128
|
licenses:
|