corindon 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d803035372216a30b75af02572aac4a1978e1f0d687c6425d26074913abfb1f
4
- data.tar.gz: '007096e0f7cfa51fc6b85d1b4a4c63ef920b13a6f6f8bd638313b9c7ddd4743b'
3
+ metadata.gz: e142a541f92e6b2af8869b8fadc2ecc82da2823b4c5ed9a9a24b7959cceecd7c
4
+ data.tar.gz: 8d9623f9a1c9b8162b551efd142fb71259e0bb689eb7e3f9ef0b2788682521c6
5
5
  SHA512:
6
- metadata.gz: 5a9fdc84c38dd60b28bedc5469305c6cbaaf52a3291f140494b3aab2f010a717afa54e797681aeb140f99700fef50688ead340301acf55e8322701f0048edae1
7
- data.tar.gz: b616cdbbefa1d2f84104082bb28deea30af5bdced64600a0d28ccd59f595928ed270de394f38a5c8040159973298213836315c74b2543d6828b292af0b1bc806
6
+ metadata.gz: e45eca1ef91da4d221138313a9c15bca970585adb5f22a47efe9503aaa2b9630691182819199387b8f690d48859a7e28e20725abf3c135e5f3e30c9a42951a4f
7
+ data.tar.gz: 88772fb162c2b8c95678f9965aee48dbc15cc548224e39b928de8e5422683005e07ff64148f43e790ed6dc96a05bf69a3c25a3f2a1dba9e01606f8be81f42107
@@ -0,0 +1,15 @@
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
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Corindon
4
+ module Console
5
+ class Input
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Corindon
4
+ module Console
5
+ class Output
6
+ end
7
+ end
8
+ end
@@ -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
- def initialize
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
- def add_definition(klass, &block)
21
- @definitions[to_id(klass)] = if injectable?(klass)
22
- klass.definition
23
- elsif block.sth?
24
- Dsl.new(klass).exec(&block)
25
- else
26
- Definition.new(klass)
27
- end
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 = [], kwargs = {}, calls = [])
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, *injector.resolve(args, kwargs))
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, *injector.resolve(call_args, call_kwargs))
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
@@ -0,0 +1,12 @@
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
@@ -0,0 +1,14 @@
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
@@ -13,7 +13,7 @@ module Corindon
13
13
 
14
14
  define_singleton_method :definition do
15
15
  if block.nil?
16
- Definition.new(self, args, kwargs, [])
16
+ Definition.new(self, args: args, kwargs: kwargs)
17
17
  else
18
18
  Dsl.new(self).exec(&block)
19
19
  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
- # @return [Array{Array, Hash}]
14
- def resolve(args, kwargs)
15
- [resolve_value(args), resolve_value(kwargs)]
16
- end
17
-
18
- private
19
-
20
- def resolve_value(value)
21
- if value.is_a?(Array)
22
- value.map(&method(:resolve_value))
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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Corindon
4
+ module DependencyInjection
5
+ module Token
6
+ class InjectionToken
7
+ # @param [Injector] injector
8
+ def resolve(injector:) end
9
+ end
10
+ end
11
+ end
12
+ 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
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Corindon
4
+ module Ext
5
+ module Guards
6
+ class Error < StandardError
7
+ end
8
+ end
9
+ end
10
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Corindon
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.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.2.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-06 00:00:00.000000000 Z
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