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,142 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # @api private
4
- # @since 0.1.0
5
- class SmartCore::Injection::Locator::ContainerProxy
6
- # @param registered_containers [SmartCore::Injection::Injector::ContainerSet]
7
- # @param explicitly_passed_container [NilClass, SmartCore::Container]
8
- # @return [void]
9
- #
10
- # @api private
11
- # @since 0.1.0
12
- def initialize(registered_containers, explicitly_passed_container)
13
- @registered_containers = registered_containers
14
- @explicitly_passed_container = explicitly_passed_container
15
- @observers = Hash.new { |h, k| h[k] = [] }
16
- registered_containers.listen_addings { |container| listen_changes(container) }
17
- end
18
-
19
- # @param dependency_path [String]
20
- # @return [Any]
21
- #
22
- # @raise [SmartCore::Injection::NoRegisteredContainersError]
23
- # @raise [SmartCore::Container::ResolvingError]
24
- #
25
- # @api private
26
- # @since 0.1.0
27
- def resolve_dependency(dependency_path)
28
- resolving_error = nil
29
-
30
- each_container do |container|
31
- begin # rubocop:disable Style/RedundantBegin
32
- return container.resolve(dependency_path)
33
- rescue SmartCore::Container::ResolvingError => error
34
- resolving_error = error
35
- end
36
- end
37
-
38
- unless resolving_error
39
- raise(SmartCore::Injection::NoRegisteredContainersError, <<~ERROR_MESSAGE)
40
- You haven't registered any containers for import.
41
- ERROR_MESSAGE
42
- else
43
- raise(resolving_error)
44
- end
45
- end
46
-
47
- # @param import_path [String]
48
- # @param observer [Block]
49
- # @return [void]
50
- #
51
- # @api private
52
- # @since 0.1.0
53
- def observe(import_path, &observer)
54
- register_observer(import_path, observer)
55
-
56
- each_container do |container|
57
- container.observe(import_path) { |path, cntr| process_changement(cntr, path) }
58
- end
59
- end
60
-
61
- private
62
-
63
- # @return [Hash<String,Proc>]
64
- #
65
- # @api private
66
- # @since 0.1.0
67
- attr_reader :observers
68
-
69
- # @return [SmartCore::Injection::Injector::ContainerSet]
70
- #
71
- # @api private
72
- # @since 0.1.0
73
- attr_reader :registered_containers
74
-
75
- # @return [NilClass, SmartCore::Container]
76
- #
77
- # @api private
78
- # @since 0.1.0
79
- attr_reader :explicitly_passed_container
80
-
81
- # @param import_path [NilClass, String]
82
- # @param container [SmartCore::Container]
83
- # @return [void]
84
- #
85
- # @api private
86
- # @since 0.1.0
87
- def process_changement(container, import_path = nil)
88
- observed_pathes = import_path ? [import_path] : observed_import_pathes
89
-
90
- observed_pathes.each do |observed_path|
91
- suitable_container = each_container.find { |cntr| cntr.key?(observed_path) }
92
- break unless suitable_container
93
- notify_observers(observed_path) if suitable_container == container
94
- end
95
- end
96
-
97
- # @param import_path [String]
98
- # @param observer [SmartCore::Container]
99
- # @return [void]
100
- #
101
- # @api private
102
- # @since 0.1.0
103
- def register_observer(import_path, observer)
104
- observers[import_path] << observer
105
- end
106
-
107
- # @return [Array<String>]
108
- #
109
- # @api private
110
- # @since 0.1.0
111
- def observed_import_pathes
112
- observers.keys
113
- end
114
-
115
- # @param import_pathes [String]
116
- # @return [void]
117
- #
118
- # @api private
119
- # @since 0.1.0
120
- def notify_observers(*import_pathes)
121
- import_pathes.each { |import_path| observers.fetch(import_path).each(&:call) }
122
- end
123
-
124
- # @param block [Block]
125
- # @yield [container]
126
- # @yieldparam container [SmartCore::Container]
127
- # @return [Enumerable]
128
- #
129
- # @api private
130
- # @since 0.1.0
131
- def each_container(&block)
132
- enumerator = Enumerator.new do |yielder|
133
- if explicitly_passed_container
134
- yielder.yield(explicitly_passed_container)
135
- else
136
- registered_containers.reverse_each(&yielder)
137
- end
138
- end
139
-
140
- block_given? ? enumerator.each(&block) : enumerator.each
141
- end
142
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # @api private
4
- # @since 0.1.0
5
- class SmartCore::Injection::Locator::Dependency
6
- # @return [void]
7
- #
8
- # @api private
9
- # @since 0.1.0
10
- def initialize
11
- @binded = false
12
- @value = nil
13
- @barrier = SmartCore::Engine::Lock.new
14
- end
15
-
16
- # @param block [Block]
17
- # @return [Any]
18
- #
19
- # @api private
20
- # @since 0.1.0
21
- def rebind(&block)
22
- with_barrier do
23
- @binded = false
24
- bind(&block)
25
- end
26
- end
27
-
28
- # @param block [Block]
29
- # @return [Any]
30
- #
31
- # @api public
32
- # @since 0.7.0
33
- def bind(&block)
34
- with_barrier do
35
- if @binded
36
- @value
37
- else
38
- @binded = true
39
- @value = yield
40
- end
41
- end
42
- end
43
-
44
- private
45
-
46
- # @param block [Block]
47
- # @return [Any]
48
- #
49
- # @api private
50
- # @since 0.1.0
51
- def with_barrier(&block)
52
- @barrier.synchronize(&block)
53
- end
54
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # @api private
4
- # @since 0.1.0
5
- module SmartCore::Injection::Locator::Factory
6
- class << self
7
- # @param injection_settings [SmartCore::Injection::Injector::InjectionSettings]
8
- # @return [SmartCore::Injection::Locator]
9
- #
10
- # @api private
11
- # @since 0.1.0
12
- def create(injection_settings, import_key, import_path)
13
- container_proxy = create_container_proxy(injection_settings)
14
- create_locator(import_path, container_proxy).tap do |locator|
15
- control_injection_memoization(injection_settings, container_proxy, locator, import_path)
16
- end
17
- end
18
-
19
- private
20
-
21
- # @return [SmartCore::Injection::Locator::ContainerProxy]
22
- #
23
- # @api private
24
- # @since 0.1.0
25
- def create_container_proxy(injection_settings)
26
- SmartCore::Injection::Locator::ContainerProxy.new(
27
- injection_settings.container_set,
28
- injection_settings.from
29
- )
30
- end
31
-
32
- # @param import_path [String]
33
- # @param container_proxy [SmartCore::Injection::Locator::ContainerProxy]
34
- # @return [SmartCore::Injection::Locator]
35
- #
36
- # @api private
37
- # @since 0.1.0
38
- def create_locator(import_path, container_proxy)
39
- SmartCore::Injection::Locator.new(import_path, container_proxy)
40
- end
41
-
42
- # @param injection_settings [SmartCore::Injection::Injector::InjectionSettings]
43
- # @param locator [SmartCore::Injection::Locator]
44
- # @param import_path [String]
45
- # @return [void]
46
- #
47
- # @api private
48
- # @since 0.1.0
49
- def control_injection_memoization(injection_settings, container_proxy, locator, import_path)
50
- container_proxy.observe(import_path) do
51
- locator.rebind!
52
- end unless injection_settings.memoize
53
- end
54
- end
55
- end
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # @api private
4
- # @since 0.1.0
5
- class SmartCore::Injection::Locator
6
- require_relative 'locator/container_proxy'
7
- require_relative 'locator/dependency'
8
- require_relative 'locator/factory'
9
-
10
- # @return [String]
11
- #
12
- # @api private
13
- # @since 0.1.0
14
- attr_reader :import_path
15
-
16
- # @param import_path [String]
17
- # @param container_proxy [SmartCore::Injection::Locator::ContainerProxy]
18
- # @return [void]
19
- #
20
- # @api private
21
- # @since 0.1.0
22
- def initialize(import_path, container_proxy)
23
- @import_path = import_path
24
- @container_proxy = container_proxy
25
- @dependency = SmartCore::Injection::Locator::Dependency.new
26
- end
27
-
28
- # @return [Any]
29
- #
30
- # @api private
31
- # @since 0.1.0
32
- def resolve_dependency
33
- dependency.bind { container_proxy.resolve_dependency(import_path) }
34
- end
35
- alias_method :bind!, :resolve_dependency
36
-
37
- # @return [Any]
38
- #
39
- # @api private
40
- # @since 0.1.0
41
- def rebind_dependency
42
- dependency.rebind { container_proxy.resolve_dependency(import_path) }
43
- end
44
- alias_method :rebind!, :rebind_dependency
45
-
46
- private
47
-
48
- # @return [SmartCore::Injection::Locator::Dependency]
49
- #
50
- # @api private
51
- # @since 0.1.0
52
- attr_reader :dependency
53
-
54
- # @return [SmartCore::Injection::Locator::ContainerProxy]
55
- #
56
- # @api private
57
- # @since 0.1.0
58
- attr_reader :container_proxy
59
- end