smart_injection 0.0.0.alpha3 → 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,113 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # @api private
4
- # @since 0.1.0
5
- module SmartCore::Injection::Injector::InjectionSettings::IncompatabilityControl
6
- class << self
7
- # @param injectable [Class, Module]
8
- # @param imports [Hash<String|Symbol,String>]
9
- # @param memoize [Boolean]
10
- # @param access [Symbol]
11
- # @param bind [Symbol]
12
- # @param from [NilClass, SmartCore::Container]
13
- # @return [void]
14
- #
15
- # @api private
16
- # @since 0.1.0
17
- def prevent_incompatabilities!(injectable, imports, memoize, access, bind, from)
18
- prevent_injectable_incompatabilities!(injectable)
19
- prevent_imports_incompatabilites!(imports)
20
- prevent_memoize_incompatabilites(memoize)
21
- prevent_access_incompatabilites(access)
22
- prevent_bind_incompatabilites(bind)
23
- prevent_from_incompatabilites(from)
24
- end
25
-
26
- private
27
-
28
- # @param injectable [Class, Module]
29
- # @return [void]
30
- #
31
- # @api private
32
- # @since 0.1.0
33
- def prevent_injectable_incompatabilities!(injectable)
34
- unless injectable.is_a?(Class) || injectable.is_a?(Module)
35
- raise(SmartCore::Injection::ArgumentError, <<~ERROR_MESSAGE)
36
- ERROR_MESSAGE
37
- end
38
- end
39
-
40
- # @param imports [Hash<String|Symbol,String>]
41
- # @return [void]
42
- #
43
- # @api private
44
- # @since 0.1.0
45
- def prevent_imports_incompatabilites!(imports)
46
- unless imports.is_a?(::Hash)
47
- raise(SmartCore::Injection::ArgumentError, <<~ERROR_MESSAGE)
48
- Incorrect import list (should be a type of Hash)
49
- ERROR_MESSAGE
50
- end
51
-
52
- unless imports.keys.all? { |key| key.is_a?(String) || key.is_a?(Symbol) }
53
- raise(SmartCore::Injection::ArgumentError, <<~ERROR_MESSAGE)
54
- Some method aliases are incorret (they should be a type of String or Symbol)
55
- ERROR_MESSAGE
56
- end
57
-
58
- unless imports.values.all? { |value| value.is_a?(String) }
59
- raise(SmartCore::Injection::ArgumentError, <<~ERROR_MESSAGE)
60
- Some injection pathes are incorrect (they should be a type of String)
61
- ERROR_MESSAGE
62
- end
63
- end
64
-
65
- # @param memoize [Boolean]
66
- # @return [void]
67
- #
68
- # @api private
69
- # @since 0.1.0
70
- def prevent_memoize_incompatabilites(memoize)
71
- unless memoize.is_a?(::TrueClass) || memoize.is_a?(::FalseClass)
72
- raise(SmartCore::Injection::ArgumentError, <<~ERROR_MESSAGE)
73
- ERROR_MESSAGE
74
- end
75
- end
76
-
77
- # @param access [Symbol]
78
- # @return [void]
79
- #
80
- # @api private
81
- # @since 0.1.0
82
- def prevent_access_incompatabilites(access)
83
- unless SmartCore::Injection::Injector::InjectionSettings::ACCESS_MARKS.include?(access)
84
- raise(SmartCore::Injection::ArgumentError, <<~ERROR_MESSAGE)
85
- ERROR_MESSAGE
86
- end
87
- end
88
-
89
- # @param bind [Symbol]
90
- # @return [void]
91
- #
92
- # @api private
93
- # @since 0.1.0
94
- def prevent_bind_incompatabilites(bind)
95
- unless SmartCore::Injection::Injector::InjectionSettings::BINDING_STRATEGIES.include?(bind)
96
- raise(SmartCore::Injection::ArgumentError, <<~ERROR_MESSAGE)
97
- ERROR_MESSAGE
98
- end
99
- end
100
-
101
- # @param from [NilClass, SmartCore::Container]
102
- # @return [void]
103
- #
104
- # @api private
105
- # @since 0.1.0
106
- def prevent_from_incompatabilites(from)
107
- unless from.is_a?(NilClass) || from.is_a?(SmartCore::Container)
108
- raise(SmartCore::Injection::ArgumentError, <<~ERROR_MESSAGE)
109
- ERROR_MESSAGE
110
- end
111
- end
112
- end
113
- end
@@ -1,173 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # @api private
4
- # @since 0.1.0
5
- class SmartCore::Injection::Injector::InjectionSettings
6
- require_relative 'injection_settings/incompatability_control'
7
-
8
- # @return [Hash<String|Symbol,String>]
9
- #
10
- # @api private
11
- # @since 0.1.0
12
- DEFAULT_IMPORTS = {}.freeze
13
-
14
- # @return [Symbol]
15
- #
16
- # @api private
17
- # @since 0.1.0
18
- DEFAULT_ACCESS = :public
19
-
20
- # @return [Array<Symbol>]
21
- #
22
- # @api private
23
- # @since 0.1.0
24
- ACCESS_MARKS = %i[public protected private].freeze
25
-
26
- # @return [Symbol]
27
- #
28
- # @api private
29
- # @since 0.1.0
30
- DEFAULT_BINDING_STRATEGY = :dynamic
31
-
32
- # @return [Array<Symbol>]
33
- #
34
- # @api private
35
- # @since 0.1.0
36
- BINDING_STRATEGIES = %i[static dynamic].freeze
37
-
38
- # @return [Boolean]
39
- #
40
- # @api private
41
- # @since 0.1.0
42
- DEFAULT_MEMOIZE = false
43
-
44
- # @return [NilClass]
45
- #
46
- # @api private
47
- # @since 0.1.0
48
- EMPTY_CONTAINER_DESTINATION = nil
49
-
50
- # @return [Hash<String|Symbol,String>]
51
- #
52
- # @api private
53
- # @since 0.1.0
54
- attr_reader :imports
55
-
56
- # @return [Symbol]
57
- #
58
- # @api private
59
- # @since 0.1.0
60
- attr_reader :access
61
-
62
- # @return [Symbol]
63
- #
64
- # @api private
65
- # @since 0.1.0
66
- attr_reader :bind
67
-
68
- # @return [NilClass, <SmartCore::Container>]
69
- #
70
- # @api private
71
- # @since 0.1.0
72
- attr_reader :from
73
-
74
- # @return [Boolean]
75
- #
76
- # @api private
77
- # @since 0.1.0
78
- attr_reader :memoize
79
-
80
- # @return [SmartCore::Injection::Injector::ContainerSet]
81
- #
82
- # @api private
83
- # @since 0.1.0
84
- attr_reader :container_set
85
-
86
- # @return [Class, Module]
87
- #
88
- # @api private
89
- # @since 0.1.0
90
- attr_reader :injectable
91
-
92
- # @param injectable [Class, Module]
93
- # @param container_set [SmartCore::Injection::Injector::ContainerSet]
94
- # @param import [Hash<String|Symbol,String>]
95
- # @option memoize [Boolean]
96
- # @option access [Symbol]
97
- # @option bind [Symbol]
98
- # @option from [NilClass, SmartCore::Container]
99
- # @return [void]
100
- #
101
- # @api private
102
- # @since 0.1.0
103
- def initialize(
104
- injectable,
105
- container_set,
106
- imports,
107
- memoize: DEFAULT_MEMOIZE,
108
- access: DEFAULT_ACCESS,
109
- bind: DEFAULT_BINDING_STRATEGY,
110
- from: EMPTY_CONTAINER_DESTINATION
111
- )
112
- IncompatabilityControl.prevent_incompatabilities!(
113
- injectable,
114
- imports,
115
- memoize,
116
- access,
117
- bind,
118
- from
119
- )
120
-
121
- @injectable = injectable
122
- @container_set = container_set
123
- @imports = imports
124
- @memoize = memoize
125
- @access = access
126
- @bind = bind
127
- @from = from
128
- end
129
-
130
- # @return [Class, Module]
131
- #
132
- # @api private
133
- # @since 0.1.0
134
- def instance_level_injectable
135
- injectable
136
- end
137
-
138
- # @return [Class]
139
- #
140
- # @api private
141
- # @since 0.1.0
142
- def class_level_injectable
143
- class << injectable; self; end
144
- end
145
-
146
- # @return [Boolean]
147
- #
148
- # @api private
149
- # @since 0.1.0
150
- def bind_dynamic?
151
- bind == :dynamic
152
- end
153
-
154
- # @return [Boolean]
155
- #
156
- # @api private
157
- # @since 0.1.0
158
- def bind_static?
159
- bind == :static
160
- end
161
-
162
- # @param block [Block]
163
- # @yield [import_key, import_path]
164
- # @yieldparam import_key [String, Symbol]
165
- # @yieldparam import_path [String]
166
- # @return [Enumerable]
167
- #
168
- # @api private
169
- # @since 0.1.0
170
- def each_import(&block)
171
- block_given? ? imports.each_pair(&block) : imports.each_pair
172
- end
173
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # @api private
4
- # @since 0.1.0
5
- module SmartCore::Injection::Injector::Modulizer
6
- class << self
7
- # @param containers [Array<SmartCore::Container>]
8
- # @return [Module]
9
- #
10
- # @api private
11
- # @since 0.1.0
12
- def with_containers(containers)
13
- prevent_inconsistency!(containers)
14
- build_container_injectable_module(containers)
15
- end
16
-
17
- # @param base_klass [Class, Module]
18
- # @return [void]
19
- #
20
- # @api private
21
- # @since 0.1.0
22
- def inject_to(base_klass)
23
- base_klass.include(::SmartCore::Injection::DSL)
24
- end
25
-
26
- private
27
-
28
- # @param containers [Array<SmartCore::Container>]
29
- # @return [void]
30
- #
31
- # @api private
32
- # @since 0.1.0
33
- def prevent_inconsistency!(containers)
34
- unless containers.is_a?(Array) && containers.all? { |cont| cont.is_a?(SmartCore::Container) }
35
- raise(SmartCore::Injection::ArgumentError, <<~ERROR_MESSAGE)
36
- Injectable containers should be a type of SmartCore::Container
37
- ERROR_MESSAGE
38
- end
39
- end
40
-
41
- # @param containers [Array<SmartCore::Container>]
42
- # @return [Module]
43
- #
44
- # @api private
45
- # @since 0.1.0
46
- def build_container_injectable_module(containers)
47
- Module.new do
48
- define_singleton_method :included do |base_klass|
49
- base_klass.include(::SmartCore::Injection::DSL)
50
- base_klass.register_container(*containers)
51
- end
52
- end
53
- end
54
- end
55
- end
@@ -1,92 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # @api private
4
- # @since 0.1.0
5
- module SmartCore::Injection::Injector::Strategies::MethodInjection
6
- class << self
7
- # @param injection_settings [SmartCore::Injection::Injector::InjectionSettings]
8
- # @return [void]
9
- #
10
- # @api private
11
- # @since 0.1.0
12
- def inject_instance_method(injection_settings)
13
- inject_dependency(injection_settings, injection_settings.instance_level_injectable)
14
- end
15
-
16
- # @param injection_settings [SmartCore::Injection::Injector::InjectionSettings]
17
- # @return [void]
18
- #
19
- # @api private
20
- # @since 0.1.0
21
- def inject_class_method(injection_settings)
22
- inject_dependency(injection_settings, injection_settings.class_level_injectable)
23
- end
24
-
25
- private
26
-
27
- # @param injection_settings [SmartCore::Injection::Injector::InjectionSettings]
28
- # @param injectable [Class, Module]
29
- # @return [void]
30
- #
31
- # @api private
32
- # @since 0.1.0
33
- def inject_dependency(injection_settings, injectable)
34
- injection_settings.each_import do |import_key, import_path|
35
- locator = build_locator(injection_settings, import_key, import_path)
36
- process_injection_bindings(injection_settings, locator)
37
- injection = build_injection(injection_settings, import_key, locator)
38
- inject_injection(injection_settings, injection, injectable)
39
- end
40
- end
41
-
42
- # @param injection_settings [SmartCore::Injection::Injector::InjectionSettings]
43
- # @param import_key [String, Symbol]
44
- # @param import_path [String]
45
- # @return [SmartCore::Injection::Locator]
46
- #
47
- # @api private
48
- # @since 0.1.0
49
- def build_locator(injection_settings, import_key, import_path)
50
- SmartCore::Injection::Locator::Factory.create(injection_settings, import_key, import_path)
51
- end
52
-
53
- # @param locator [SmartCore::Injection::Locator]
54
- # @return [void]
55
- #
56
- # @api private
57
- # @since 0.1.0
58
- def process_injection_bindings(injection_settings, locator)
59
- locator.bind! if injection_settings.bind_static?
60
- end
61
-
62
- # @param injection_settings [SmartCore::Injection::Injector::InjectionSettings]
63
- # @param import_key [String, Symbol]
64
- # @param locator [SmartCore::Injection::Locator]
65
- # @return [Module]
66
- #
67
- # @api private
68
- # @since 0.1.0
69
- def build_injection(injection_settings, import_key, locator)
70
- Module.new do
71
- define_method(import_key) { locator.resolve_dependency }
72
-
73
- case injection_settings.access
74
- when :public then public(import_key)
75
- when :private then private(import_key)
76
- when :protected then protected(import_key)
77
- end
78
- end
79
- end
80
-
81
- # @param injection_settings [SmartCore::Injection::Injector::InjectionSettings]
82
- # @param injection [Module]
83
- # @param injectable [Class, Module]
84
- # @return [void]
85
- #
86
- # @api private
87
- # @since 0.1.0
88
- def inject_injection(injection_settings, injection, injectable)
89
- injectable.include(injection)
90
- end
91
- end
92
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # @api private
4
- # @since 0.1.0
5
- module SmartCore::Injection::Injector::Strategies
6
- require_relative 'strategies/method_injection'
7
- end
@@ -1,165 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # @api private
4
- # @since 0.1.0
5
- class SmartCore::Injection::Injector
6
- require_relative 'injector/container_set'
7
- require_relative 'injector/injection_settings'
8
- require_relative 'injector/modulizer'
9
- require_relative 'injector/strategies'
10
-
11
- # @param injectable [Class, Module]
12
- # @return [void]
13
- #
14
- # @api private
15
- # @since 0.1.0
16
- def initialize(injectable)
17
- @injectable = injectable
18
- @linked_containers = SmartCore::Injection::Injector::ContainerSet.new
19
- @access_lock = SmartCore::Engine::Lock.new
20
- end
21
-
22
- # @param imports [Hash<String|Symbol,String>]
23
- # @param memoize [Boolean]
24
- # @param access [Symbol]
25
- # @param bind [Symbol]
26
- # @param from [NilClass, SmartCore::Container]
27
- # @return [void]
28
- #
29
- # @api private
30
- # @since 0.1.0
31
- def inject(imports, memoize, access, bind, from)
32
- thread_safe { inject_instance_method(imports, memoize, access, bind, from) }
33
- end
34
-
35
- # @param imports [Hash<String|Symbol,String>]
36
- # @param memoize [Boolean]
37
- # @param access [Symbol]
38
- # @param bind [Symbol]
39
- # @param from [NilClass, SmartCore::Container]
40
- # @return [void]
41
- #
42
- # @api private
43
- # @since 0.1.0
44
- def inject_static(imports, memoize, access, bind, from)
45
- thread_safe { inject_class_method(imports, memoize, access, bind, from) }
46
- end
47
-
48
- # @param containers [Array<SmartCore::Container>]
49
- # @return [void]
50
- #
51
- # @api private
52
- # @since 0.1.0
53
- def register_container(*containers)
54
- thread_safe { link_container(containers) }
55
- end
56
-
57
- # @return [Array<SmartCore::Container>]
58
- #
59
- # @api private
60
- # @since 0.1.0
61
- def associated_containers
62
- thread_safe { linked_containers.list }
63
- end
64
-
65
- # @param another_injectable [Class, Module]
66
- # @return [SmartCore::Injection::Injector]
67
- #
68
- # @api private
69
- # @since 0.1.0
70
- def duplicate_for(another_injectable)
71
- thread_safe { create_duplicate(another_injectable) }
72
- end
73
-
74
- private
75
-
76
- # @return [Class, Module]
77
- #
78
- # @api private
79
- # @since 0.1.0
80
- attr_reader :injectable
81
-
82
- # @return [Array<SmartCore::Container>]
83
- #
84
- # @api private
85
- # @since 0.1.0
86
- attr_reader :linked_containers
87
-
88
- # @param another_injectable [Class, Module]
89
- # @return [SmartCore::Injection::Injector]
90
- #
91
- # @api private
92
- # @since 0.1.0
93
- def create_duplicate(another_injectable)
94
- self.class.new(another_injectable).tap do |duplicate|
95
- linked_containers.each do |container|
96
- duplicate.register_container(container)
97
- end
98
- end
99
- end
100
-
101
- # @param imports [Hash<String|Symbol,String>]
102
- # @param memoize [Boolean]
103
- # @param access [Symbol]
104
- # @param bind [Symbol]
105
- # @param from [NilClass, SmartCore::Container]
106
- # @return [void]
107
- #
108
- # @api private
109
- # @since 0.1.0
110
- def inject_instance_method(imports, memoize, access, bind, from)
111
- SmartCore::Injection::Injector::Strategies::MethodInjection.inject_instance_method(
112
- SmartCore::Injection::Injector::InjectionSettings.new(
113
- injectable,
114
- linked_containers,
115
- imports,
116
- memoize: memoize,
117
- access: access,
118
- bind: bind,
119
- from: from
120
- )
121
- )
122
- end
123
-
124
- # @param imports [Hash<String|Symbol,String>]
125
- # @param memoize [Boolean]
126
- # @param access [Symbol]
127
- # @param bind [Symbol]
128
- # @param from [NilClass, SmartCore::Container]
129
- # @return [void]
130
- #
131
- # @api private
132
- # @since 0.1.0
133
- def inject_class_method(imports, memoize, access, bind, from)
134
- SmartCore::Injection::Injector::Strategies::MethodInjection.inject_class_method(
135
- SmartCore::Injection::Injector::InjectionSettings.new(
136
- injectable,
137
- linked_containers,
138
- imports,
139
- memoize: memoize,
140
- access: access,
141
- bind: bind,
142
- from: from
143
- )
144
- )
145
- end
146
-
147
- # @param containers [Array<SmartCore::Container>]
148
- # @return [void]
149
- #
150
- # @api private
151
- # @since 0.1.0
152
- def link_container(containers)
153
- containers.each do |container|
154
- linked_containers.add(container)
155
- end
156
- end
157
-
158
- # @return [Any]
159
- #
160
- # @api private
161
- # @since 0.1.0
162
- def thread_safe(&block)
163
- @access_lock.synchronize(&block)
164
- end
165
- end