corindon 0.5.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/corindon.gemspec +2 -0
  3. data/lib/corindon/dependency_injection/container.rb +87 -36
  4. data/lib/corindon/dependency_injection/definition.rb +13 -32
  5. data/lib/corindon/dependency_injection/dsl.rb +19 -11
  6. data/lib/corindon/dependency_injection/injectable.rb +22 -8
  7. data/lib/corindon/dependency_injection/injector.rb +2 -0
  8. data/lib/corindon/dependency_injection/parameter_bag.rb +47 -0
  9. data/lib/corindon/dependency_injection/ruby_compat.rb +27 -0
  10. data/lib/corindon/dependency_injection/testing/mock_utils.rb +40 -0
  11. data/lib/corindon/dependency_injection/token/service_call_token.rb +1 -21
  12. data/lib/corindon/dependency_injection/token/service_factory_token.rb +24 -0
  13. data/lib/corindon/{ext/guards.rb → guards/ext.rb} +4 -4
  14. data/lib/corindon/result/errors/bad_return_type_error.rb +19 -0
  15. data/lib/corindon/{ext/guards/error.rb → result/errors/result_error.rb} +3 -3
  16. data/lib/corindon/result/ext.rb +25 -0
  17. data/lib/corindon/result/failure.rb +27 -0
  18. data/lib/corindon/result/result.rb +32 -0
  19. data/lib/corindon/result/success.rb +39 -0
  20. data/lib/corindon/{ext/something.rb → something/ext.rb} +2 -2
  21. data/lib/corindon/version.rb +1 -1
  22. metadata +42 -11
  23. data/lib/corindon/console/application.rb +0 -15
  24. data/lib/corindon/console/base_command.rb +0 -13
  25. data/lib/corindon/console/input.rb +0 -8
  26. data/lib/corindon/console/output.rb +0 -8
  27. data/lib/corindon/dependency_injection/id/id_generator.rb +0 -12
  28. 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: 37d5ac638b45a7b98ffa07c588b299147ae04ad8172eb9ac3d84f6b207242df7
4
- data.tar.gz: 867092edd58ca1209d5bdfd3fd27d97f7663e63813c6266937e55b4eaa26bc8f
3
+ metadata.gz: 1dd263e6d3149f438f50e22fe7575fec7fcfc6d5f8755c77cd83877e5e89c0de
4
+ data.tar.gz: a33cc4b33c681d3027238dac77f1400e7a611b7502f0504aa5e8b6db2d28ac0a
5
5
  SHA512:
6
- metadata.gz: a18f25be1501b07dab913c482f1b84308d392355182f02bc724bfc7dece28227c9aac87c7ecf6fb2bca2934019188968f9b177832303c9e7f8716254409dc0a8
7
- data.tar.gz: ca432ab9e27df7b318052cc755d7b5f65b0b3e1bf42c6a1fae7043d27400b3108e148274ea0c862f64a5db6b3121cac3db449f9fb8cb8335aabeb4a536972151
6
+ metadata.gz: 13d607a74528b870f3d064a94b6913a4a76ea69ce5d4463ce6f0c41dd65c97c115d9aaeb07c05dc6f874910619dc8354c2670084cf71747e2330b34be46be145
7
+ data.tar.gz: 1c63ab07e90abbec9c23b04c7c6a097e39dbf23b2faf4f9cecaabb27006f0ee52aec72ee03c477157480931d9cfab95979357890bcc273de62f08bffbb02eaec
data/corindon.gemspec CHANGED
@@ -20,5 +20,7 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_development_dependency 'bundler'
22
22
  s.add_development_dependency 'minitest', '~> 5.14'
23
+ s.add_development_dependency 'minitest-reporters', '~> 1.4'
23
24
  s.add_development_dependency 'rake', '~> 13.0'
25
+ s.add_development_dependency 'rubocop', '1.3.1'
24
26
  end
@@ -3,42 +3,69 @@
3
3
  module Corindon
4
4
  module DependencyInjection
5
5
  class Container
6
- using Ext::Something
6
+ using Something::Ext
7
7
 
8
- attr_reader :id_generator
9
8
  attr_reader :injector
10
9
 
11
- # @param [Id::IdGenerator] id_generator
12
- def initialize(id_generator: Id::UuidGenerator.new)
13
- @id_generator = id_generator
10
+ # @param [Array<Definition>] definitions
11
+ # @param [ParameterBag] parameters
12
+ def initialize(definitions: [], parameters: ParameterBag.new, service_built_listeners: [])
14
13
  @services = {}
15
14
  @definitions = {}
16
- @parameters = {}
15
+ @parameters = parameters
17
16
  @tags = Hash.new { |hash, key| hash[key] = [] }
18
17
 
19
18
  @injector = Injector.new(container: self)
19
+
20
+ definitions.each { |d| register_definition(d) }
21
+
22
+ @service_built_listeners = service_built_listeners
23
+ end
24
+
25
+ # @return [Container]
26
+ def dup
27
+ Container.new(
28
+ definitions: definitions.values,
29
+ parameters: ParameterBag.new(parameters.bag.dup),
30
+ service_built_listeners: service_built_listeners
31
+ )
32
+ end
33
+
34
+ # @param [Definition, Injectable, Class]
35
+ # @return [Definition]
36
+ def as_definition(def_or_injectable)
37
+ if def_or_injectable.is_a?(Definition)
38
+ def_or_injectable
39
+ elsif def_or_injectable.is_a?(Injectable)
40
+ def_or_injectable.definition
41
+ elsif def_or_injectable.is_a?(Class)
42
+ Definition.new(def_or_injectable)
43
+ else
44
+ raise StandardError.new("Don't know how to build #{def_or_injectable}")
45
+ end
20
46
  end
21
47
 
22
- # @param [Class] klass
48
+ # @param [Class, Injectable, Definition] def_or_injectable
23
49
  # @return [String]
24
- def add_definition(klass, id: nil, anonymous: false, context: {}, &block)
25
- definition = if injectable?(klass)
26
- klass.definition
27
- elsif block.sth?
28
- Dsl.new(klass).exec(context: context, &block)
29
- else
30
- Definition.new(klass)
31
- end
32
-
33
- # Generate an id if set to anonymous when registering
34
- # If a definition is set to be anonymous but an id is provided zhen registering, use this id instead<
35
- if anonymous || (definition.anonymous? && id.nil?)
36
- id = id_generator.generate
50
+ def add_definition(def_or_injectable, context: {}, &block)
51
+ definition = as_definition(def_or_injectable)
52
+
53
+ if block.sth?
54
+ definition = Dsl.from_definition(definition).exec(context: context, &block)
37
55
  end
38
56
 
39
- id = id || definition.id || to_id(klass)
40
- definitions[id] = definition
41
- definition.tags.each { |tag| tags[tag] << id }
57
+ id = definition.id || to_id(def_or_injectable)
58
+
59
+ register_definition(
60
+ Definition.new(
61
+ definition.object_source,
62
+ id: id,
63
+ args: definition.args,
64
+ kwargs: definition.kwargs,
65
+ calls: definition.calls,
66
+ tags: definition.tags
67
+ )
68
+ )
42
69
 
43
70
  id
44
71
  end
@@ -79,44 +106,68 @@ module Corindon
79
106
  end
80
107
  end
81
108
 
82
- # @param [Class, #to_s] key
109
+ # @param [Class, String, Token::ParameterToken] key
83
110
  # @param [Object] value
84
- def set_parameter(name, value)
85
- parameters[to_id(name)] = value
111
+ def set_parameter(key, value)
112
+ parameters.set(key, value)
86
113
  end
87
114
 
88
- # @param [Class, #to_s] key
115
+ # @param [Class, String, Token::ParameterToken] key
89
116
  # @return [Boolean]
90
117
  def parameter?(key)
91
- parameters.key?(to_id(key))
118
+ parameters.has?(key)
92
119
  end
93
120
 
94
- # @param [Class, #to_s] key
121
+ # @param [Class, String, Token::ParameterToken] key
95
122
  # @return [Object]
96
123
  def parameter(key)
97
- parameters.fetch(to_id(key))
124
+ parameters.get(key)
125
+ end
126
+
127
+ # @param [Proc{Object, Container}] listener
128
+ def on_service_built(listener)
129
+ service_built_listeners << listener
98
130
  end
99
131
 
100
132
  private
101
133
 
134
+ # @return [Hash{String=>Definition}]
102
135
  attr_reader :definitions
136
+ # @return [ParameterBag]
103
137
  attr_reader :parameters
104
138
  attr_reader :services
105
139
  attr_reader :tags
140
+ attr_reader :service_built_listeners
141
+
142
+ def register_definition(definition)
143
+ definitions[definition.id] = definition
144
+ definition.tags.each { |tag| tags[tag] << definition.id }
145
+ end
106
146
 
107
147
  def build_service(id)
108
- definitions.fetch(id).build(injector).tap do |service|
109
- services[id] = service
148
+ service = injector.resolve(definitions.fetch(id)).tap do |svc|
149
+ services[id] = svc
110
150
  end
151
+
152
+ service_built_listeners.each do |listener|
153
+ listener.call(service, self)
154
+ end
155
+
156
+ service
111
157
  end
112
158
 
113
- def injectable?(klass)
114
- klass.is_a?(Class) && klass.ancestors.include?(Injectable)
159
+ def injectable?(object)
160
+ object.is_a?(Injectable)
115
161
  end
116
162
 
117
- # @param [Class, #to_s] key
163
+ # @param [Injectable, Identifiable, Class, Definition, #to_s] key
164
+ # @return [String]
118
165
  def to_id(key)
119
- if key.is_a?(Class)
166
+ if key.is_a?(Definition)
167
+ key.id
168
+ elsif injectable?(key)
169
+ to_id(key.definition)
170
+ elsif key.is_a?(Class)
120
171
  key.name
121
172
  else
122
173
  key.to_s
@@ -5,59 +5,40 @@ require 'semantic'
5
5
  module Corindon
6
6
  module DependencyInjection
7
7
  class Definition
8
- attr_reader :klass
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(klass, args: [], kwargs: {}, calls: [], tags: [], id: nil, anonymous: false)
16
- @klass = klass
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
- # # @return [Object]
26
+ # @return [Object]
27
27
  def build(injector)
28
- object = do_call(klass, :new, injector.resolve(args), injector.resolve(kwargs))
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,15 +3,29 @@
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, anonymous: false)
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
 
17
31
  # @param [Hash] context
@@ -23,7 +37,7 @@ module Corindon
23
37
 
24
38
  instance_exec(context, &block)
25
39
 
26
- Definition.new(@klass, args: @args, kwargs: @kwargs, calls: @calls, tags: @tags, id: @id, anonymous: @anonymous)
40
+ Definition.new(@klass, args: @args, kwargs: @kwargs, calls: @calls, tags: @tags, id: @id)
27
41
  end
28
42
 
29
43
  def args(*arguments, **kv_arguments)
@@ -38,15 +52,9 @@ module Corindon
38
52
 
39
53
  # @param [String] id
40
54
  def id(id)
41
- @anonymous = false
42
55
  @id = id
43
56
  end
44
57
 
45
- def anonymous!
46
- @anonymous = true
47
- @id = nil
48
- end
49
-
50
58
  # @param [Class, #to_s] key
51
59
  # @return [Token::ParameterToken]
52
60
  def param(key)
@@ -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
- include Injectable
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(self, args: args, kwargs: kwargs)
31
+ Definition.new(source, args: args, kwargs: kwargs, id: name)
17
32
  else
18
- Dsl.new(self).exec(&block)
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
@@ -19,6 +19,8 @@ module Corindon
19
19
  value.transform_values(&method(:resolve))
20
20
  elsif value.is_a?(Token::InjectionToken)
21
21
  value.resolve(injector: self)
22
+ elsif value.is_a?(Definition)
23
+ value.build(self)
22
24
  else
23
25
  container.get(value)
24
26
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Corindon
4
+ module DependencyInjection
5
+ class ParameterBag
6
+ # @return [Hash{String => Object}]
7
+ attr_reader :bag
8
+
9
+ # @param [Hash{String => Object}] bag
10
+ def initialize(bag = {})
11
+ @bag = bag
12
+ end
13
+
14
+ # @param [Class, String, Token::ParameterToken] key
15
+ # @return [Boolean]
16
+ def has?(key)
17
+ @bag.key?(to_key(key))
18
+ end
19
+
20
+ # @param [Class, String, Token::ParameterToken] key
21
+ # @return [Object]
22
+ def get(key)
23
+ @bag.fetch(to_key(key))
24
+ end
25
+
26
+ # @param [Class, String, Token::ParameterToken] key
27
+ # @param [Object] value
28
+ def set(key, value)
29
+ @bag[to_key(key)] = value
30
+ end
31
+
32
+ private
33
+
34
+ def to_key(keylike)
35
+ if keylike.is_a?(Class)
36
+ keylike.name
37
+ elsif keylike.is_a?(Token::ParameterToken)
38
+ keylike.key
39
+ elsif keylike.is_a?(String)
40
+ keylike
41
+ else
42
+ raise StandardError.new("#{keylike} is not a valid key (objects of class #{keylike.class.name} are not handled.")
43
+ end
44
+ end
45
+ end
46
+ end
47
+ 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,40 @@
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
+ singleton_class.prepend MockUtils unless is_a?(MockUtils)
14
+
15
+ add_definition(definition) if !has?(definition)
16
+
17
+ id = to_id(definition)
18
+ mocks[definition] = services[id] = value
19
+ end
20
+ end
21
+
22
+ using self
23
+
24
+ def mocks
25
+ @mocks ||= {}
26
+ end
27
+
28
+ def dup
29
+ new_instance = super
30
+
31
+ mocks.each do |definition, value|
32
+ new_instance.mock_definition(definition, value)
33
+ end
34
+
35
+ new_instance
36
+ end
37
+ end
38
+ end
39
+ end
40
+ 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 Ext
5
- module Guards
4
+ module Guards
5
+ module Ext
6
6
  refine Object do
7
7
  def unimplemented!(message = nil)
8
- raise Error.new(message || "This method is not implemented.")
8
+ raise NotImplementedError.new(message || 'This method is not implemented.')
9
9
  end
10
10
 
11
11
  def unreachable!(message = nil)
12
- raise Error.new(message || "Reached unreachable code.")
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
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Corindon
4
- module Ext
5
- module Guards
6
- class Error < StandardError
4
+ module Result
5
+ module Errors
6
+ class ResultError < StandardError
7
7
  end
8
8
  end
9
9
  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
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Corindon
4
- module Ext
5
- module Something
4
+ module Something
5
+ module Ext
6
6
  refine Object do
7
7
  def sth?
8
8
  !nil?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Corindon
4
- VERSION = "0.5.0"
4
+ VERSION = '0.8.0'
5
5
  end
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.5.0
4
+ version: 0.8.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-07 00:00:00.000000000 Z
11
+ date: 2021-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: semantic
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '5.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.4'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rake
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,20 @@ dependencies:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: '13.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 1.3.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 1.3.1
83
111
  description:
84
112
  email: remi@piotaix.fr
85
113
  executables: []
@@ -88,25 +116,28 @@ extra_rdoc_files: []
88
116
  files:
89
117
  - corindon.gemspec
90
118
  - 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
119
  - lib/corindon/dependency_injection/container.rb
96
120
  - lib/corindon/dependency_injection/definition.rb
97
121
  - 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
122
  - lib/corindon/dependency_injection/injectable.rb
101
123
  - lib/corindon/dependency_injection/injector.rb
124
+ - lib/corindon/dependency_injection/parameter_bag.rb
125
+ - lib/corindon/dependency_injection/ruby_compat.rb
126
+ - lib/corindon/dependency_injection/testing/mock_utils.rb
102
127
  - lib/corindon/dependency_injection/token/injection_token.rb
103
128
  - lib/corindon/dependency_injection/token/parameter_token.rb
104
129
  - lib/corindon/dependency_injection/token/service_call_token.rb
130
+ - lib/corindon/dependency_injection/token/service_factory_token.rb
105
131
  - lib/corindon/dependency_injection/token/tagged_token.rb
106
132
  - lib/corindon/dependency_injection/token/value_token.rb
107
- - lib/corindon/ext/guards.rb
108
- - lib/corindon/ext/guards/error.rb
109
- - lib/corindon/ext/something.rb
133
+ - lib/corindon/guards/ext.rb
134
+ - lib/corindon/result/errors/bad_return_type_error.rb
135
+ - lib/corindon/result/errors/result_error.rb
136
+ - lib/corindon/result/ext.rb
137
+ - lib/corindon/result/failure.rb
138
+ - lib/corindon/result/result.rb
139
+ - lib/corindon/result/success.rb
140
+ - lib/corindon/something/ext.rb
110
141
  - lib/corindon/version.rb
111
142
  homepage: https://gitlab.com/piotaixr/corindon
112
143
  licenses:
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Corindon
4
- module Console
5
- class Application
6
- attr_reader :commands
7
-
8
- def initialize
9
- @commands = {}
10
- end
11
-
12
- def run(argv); end
13
- end
14
- end
15
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Corindon
4
- module Console
5
- class BaseCommand
6
- configure do |c|
7
- c.name 'blabla'
8
- end
9
-
10
- def run(input, output); end
11
- end
12
- end
13
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Corindon
4
- module Console
5
- class Input
6
- end
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Corindon
4
- module Console
5
- class Output
6
- end
7
- end
8
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Corindon
4
- module DependencyInjection
5
- module Id
6
- class IdGenerator
7
- # @return [String]
8
- def generate; end
9
- end
10
- end
11
- end
12
- end
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Corindon
4
- module DependencyInjection
5
- module Id
6
- class UuidGenerator < IdGenerator
7
- # @return [String]
8
- def generate
9
- SecureRandom.uuid
10
- end
11
- end
12
- end
13
- end
14
- end