smart_ioc 0.5.0 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/Gemfile.lock +1 -1
- data/lib/smart_ioc/bean_definition.rb +5 -3
- data/lib/smart_ioc/bean_definitions_storage.rb +23 -42
- data/lib/smart_ioc/bean_factory.rb +14 -10
- data/lib/smart_ioc/container.rb +8 -4
- data/lib/smart_ioc/iocify.rb +3 -8
- data/lib/smart_ioc/version.rb +1 -1
- data/lib/smart_ioc.rb +1 -0
- data/spec/smart_ioc/object_spec.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7dac45d6133624620331b4bbc980e7e19574881e51b4c98b2558d7b5114c584
|
4
|
+
data.tar.gz: a7753416faca728d5f72e2d8d215b3f77ef7de7eeb54f58601bf97d3b2d92f5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca530b375da6e1467d9d6ac60e7c84e3be955a8c1922bd4b4a2a24d53057ba3cb707ffe87a7bbaf0e897749b4825cdf138312df8fc24441a921c41cf60f8824d
|
7
|
+
data.tar.gz: ca838c6018069eead30c652c05f74a2c0f345429b87845e70c9f9d5cfb1d98021cb96f93417605ac2171ac99ee6ecf31dd12341f7fa85becdb201dcf5940e367
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.
|
1
|
+
3.2.2
|
data/Gemfile.lock
CHANGED
@@ -67,12 +67,14 @@ class SmartIoC::BeanDefinition
|
|
67
67
|
|
68
68
|
def preload
|
69
69
|
@dependencies.each do |dep|
|
70
|
-
SmartIoC
|
71
|
-
dep.ref
|
70
|
+
bd = SmartIoC.get_bean_definition(
|
71
|
+
dep.ref,
|
72
72
|
package: dep.package,
|
73
73
|
parent_bean_definition: self,
|
74
74
|
parent_bean_name: name,
|
75
|
-
)
|
75
|
+
)
|
76
|
+
|
77
|
+
bd.preload
|
76
78
|
end
|
77
79
|
end
|
78
80
|
end
|
@@ -2,11 +2,11 @@ class SmartIoC::BeanDefinitionsStorage
|
|
2
2
|
include SmartIoC::Errors
|
3
3
|
|
4
4
|
def initialize
|
5
|
-
@collection = []
|
5
|
+
@collection = Hash.new { |h, k| h[k] = [] }
|
6
6
|
end
|
7
7
|
|
8
8
|
def clear_dependencies
|
9
|
-
@collection.each do |bd|
|
9
|
+
@collection.values.flatten.each do |bd|
|
10
10
|
bd.dependencies.each do |dependency|
|
11
11
|
dependency.bean_definition = nil
|
12
12
|
end
|
@@ -15,12 +15,12 @@ class SmartIoC::BeanDefinitionsStorage
|
|
15
15
|
|
16
16
|
# @param bean_definition [BeanDefinition]
|
17
17
|
def push(bean_definition)
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
bd_scope = @collection[bean_definition.name]
|
19
|
+
|
20
|
+
existing_bd = bd_scope.detect { |bd| bd == bean_definition }
|
21
21
|
|
22
22
|
if existing_bd
|
23
|
-
|
23
|
+
bd_scope.reject! { |bd| bd == bean_definition }
|
24
24
|
|
25
25
|
message = <<~EOF
|
26
26
|
\nReplacing bean definition...
|
@@ -34,22 +34,15 @@ class SmartIoC::BeanDefinitionsStorage
|
|
34
34
|
puts message
|
35
35
|
end
|
36
36
|
|
37
|
-
|
37
|
+
bd_scope.push(bean_definition)
|
38
38
|
end
|
39
39
|
|
40
|
-
def
|
41
|
-
|
42
|
-
bean = @collection.detect {|bd| bd.klass.to_s == klass_str}
|
40
|
+
def delete(bean_definition)
|
41
|
+
bd_scope = @collection[bean_definition.name]
|
43
42
|
|
44
|
-
|
45
|
-
@collection.delete(bean)
|
46
|
-
end
|
47
|
-
end
|
43
|
+
bd_scope.delete_if { |bd| bd.klass.to_s == bean_definition.klass.to_s }
|
48
44
|
|
49
|
-
|
50
|
-
# @return bean definition [BeanDefinition] or nil
|
51
|
-
def find_by_class(klass)
|
52
|
-
@collection.detect {|bd| bd.klass == klass}
|
45
|
+
nil
|
53
46
|
end
|
54
47
|
|
55
48
|
# Returns bean definition for specific class
|
@@ -58,27 +51,23 @@ class SmartIoC::BeanDefinitionsStorage
|
|
58
51
|
# @param context [Symbol]
|
59
52
|
# @return bean definition [BeanDefinition] or nil
|
60
53
|
def find_bean(bean_name, package, context)
|
61
|
-
@collection.detect
|
54
|
+
@collection[bean_name].detect do |bd|
|
55
|
+
bd.name == bean_name && bd.package == package && bd.context == context
|
56
|
+
end
|
62
57
|
end
|
63
58
|
|
64
59
|
def filter_by(bean_name, package = nil, context = nil)
|
65
|
-
|
66
|
-
bd.name == bean_name
|
67
|
-
end
|
60
|
+
bd_scope = @collection[bean_name]
|
68
61
|
|
69
62
|
if package
|
70
|
-
|
71
|
-
bd.package == package
|
72
|
-
end
|
63
|
+
bd_scope = bd_scope.select { |bd| bd.package == package }
|
73
64
|
end
|
74
65
|
|
75
66
|
if context
|
76
|
-
|
77
|
-
bd.context == context
|
78
|
-
end
|
67
|
+
bd_scope = bean_definitions.select { |bd| bd.context == context }
|
79
68
|
end
|
80
69
|
|
81
|
-
|
70
|
+
bd_scope
|
82
71
|
end
|
83
72
|
|
84
73
|
# @bean_name [Symbol] bean name
|
@@ -112,26 +101,18 @@ class SmartIoC::BeanDefinitionsStorage
|
|
112
101
|
# @package [Symbol, nil] package name
|
113
102
|
# @context [Symbol, nil] context
|
114
103
|
def filter_by_with_drop_to_default_context(bean_name, package = nil, context = nil)
|
115
|
-
|
116
|
-
bd.name == bean_name
|
117
|
-
end
|
104
|
+
bd_scope = @collection[bean_name]
|
118
105
|
|
119
106
|
if package
|
120
|
-
|
121
|
-
bd.package == package
|
122
|
-
end
|
107
|
+
bd_scope = bd_scope.select { |bd| bd.package == package }
|
123
108
|
end
|
124
109
|
|
125
110
|
if context
|
126
|
-
context_bean_definitions =
|
127
|
-
bd.context == context
|
128
|
-
end
|
111
|
+
context_bean_definitions = bd_scope.select { |bd| bd.context == context }
|
129
112
|
|
130
|
-
if
|
131
|
-
bean_definitions = context_bean_definitions
|
132
|
-
end
|
113
|
+
bd_scope = context_bean_definitions if context_bean_definitions.any?
|
133
114
|
end
|
134
115
|
|
135
|
-
|
116
|
+
bd_scope
|
136
117
|
end
|
137
118
|
end
|
@@ -32,16 +32,7 @@ class SmartIoC::BeanFactory
|
|
32
32
|
# @raise [ArgumentError] if bean is not found
|
33
33
|
# @raise [ArgumentError] if ambiguous bean definition was found
|
34
34
|
def get_bean(bean_name, package: nil, parent_bean_definition: nil, context: nil, parent_bean_name: nil)
|
35
|
-
|
36
|
-
check_arg(package, :package, Symbol) if package
|
37
|
-
check_arg(parent_bean_definition, :parent_bean_definition, SmartIoC::BeanDefinition) if parent_bean_definition
|
38
|
-
check_arg(context, :context, Symbol) if context
|
39
|
-
|
40
|
-
@bean_file_loader.require_bean(bean_name)
|
41
|
-
|
42
|
-
parent_package_name = parent_bean_definition ? parent_bean_definition.package : nil
|
43
|
-
context = autodetect_context(bean_name, package, parent_package_name, context, parent_bean_name)
|
44
|
-
bean_definition = @bean_definitions_storage.find(bean_name, package, context, parent_package_name)
|
35
|
+
bean_definition = get_bean_definition(bean_name, package:, parent_bean_definition:, context:, parent_bean_name:)
|
45
36
|
scope = get_scope(bean_definition)
|
46
37
|
bean = scope.get_bean(bean_definition.klass)
|
47
38
|
|
@@ -56,6 +47,19 @@ class SmartIoC::BeanFactory
|
|
56
47
|
raise e
|
57
48
|
end
|
58
49
|
|
50
|
+
def get_bean_definition(bean_name, package: nil, context: nil, parent_bean_definition: nil, parent_bean_name: nil)
|
51
|
+
check_arg(bean_name, :bean_name, Symbol)
|
52
|
+
check_arg(package, :package, Symbol) if package
|
53
|
+
check_arg(parent_bean_definition, :parent_bean_definition, SmartIoC::BeanDefinition) if parent_bean_definition
|
54
|
+
check_arg(context, :context, Symbol) if context
|
55
|
+
|
56
|
+
@bean_file_loader.require_bean(bean_name)
|
57
|
+
|
58
|
+
parent_package_name = parent_bean_definition ? parent_bean_definition.package : nil
|
59
|
+
context = autodetect_context(bean_name, package, parent_package_name, context, parent_bean_name)
|
60
|
+
bean_definition = @bean_definitions_storage.find(bean_name, package, context, parent_package_name)
|
61
|
+
end
|
62
|
+
|
59
63
|
private
|
60
64
|
|
61
65
|
def init_bean(bean_definition)
|
data/lib/smart_ioc/container.rb
CHANGED
@@ -19,10 +19,10 @@ module SmartIoC
|
|
19
19
|
raise ArgumentError, "SmartIoC::Container should not be allocated. Use SmartIoC::Container.get_instance instead"
|
20
20
|
end
|
21
21
|
|
22
|
-
# @param klass [
|
22
|
+
# @param klass [BeanDefinition] bean class name
|
23
23
|
# @return nil
|
24
|
-
def unregister_bean(
|
25
|
-
bean_definitions_storage.
|
24
|
+
def unregister_bean(bean_definition)
|
25
|
+
bean_definitions_storage.delete(bean_definition)
|
26
26
|
clear_scopes
|
27
27
|
nil
|
28
28
|
end
|
@@ -90,10 +90,14 @@ module SmartIoC
|
|
90
90
|
# @param package [Symbol]
|
91
91
|
# @param context [Symbol]
|
92
92
|
# return [BeanDefinition]
|
93
|
-
def
|
93
|
+
def find_bean_definition(bean_name, package, context)
|
94
94
|
bean_definitions_storage.find_bean(bean_name, package, context)
|
95
95
|
end
|
96
96
|
|
97
|
+
def get_bean_definition(...)
|
98
|
+
bean_factory.get_bean_definition(...)
|
99
|
+
end
|
100
|
+
|
97
101
|
# Sets new load proc
|
98
102
|
# for those who use active support dependency loader
|
99
103
|
# one can use
|
data/lib/smart_ioc/iocify.rb
CHANGED
@@ -68,15 +68,14 @@ module SmartIoC::Iocify
|
|
68
68
|
file_path ||= caller[0].split(':').first
|
69
69
|
package ||= SmartIoC::BeanLocations.get_bean_package(file_path)
|
70
70
|
context ||= SmartIoC::Container::DEFAULT_CONTEXT
|
71
|
-
bean_definition = SmartIoC.
|
71
|
+
bean_definition = SmartIoC.find_bean_definition(bean_name, package, context)
|
72
72
|
|
73
73
|
if bean_definition
|
74
74
|
if bean_definition.path == file_path
|
75
75
|
# seems that file with bean definition was reloaded
|
76
76
|
# lets clear all scopes so we do not have
|
77
|
-
|
78
|
-
|
79
|
-
container.force_clear_scopes
|
77
|
+
SmartIoC::Container.get_instance.unregister_bean(bean_definition)
|
78
|
+
SmartIoC::Container.get_instance.force_clear_scopes
|
80
79
|
else
|
81
80
|
raise ArgumentError, "bean with for class #{self.to_s} was already defined in #{bean_definition.path}"
|
82
81
|
end
|
@@ -168,9 +167,5 @@ module SmartIoC::Iocify
|
|
168
167
|
|
169
168
|
nil
|
170
169
|
end
|
171
|
-
|
172
|
-
def _smart_ioc_preload_
|
173
|
-
@bean_definition&.preload; nil
|
174
|
-
end
|
175
170
|
end
|
176
171
|
end
|
data/lib/smart_ioc/version.rb
CHANGED
data/lib/smart_ioc.rb
CHANGED
@@ -32,7 +32,7 @@ describe Object do
|
|
32
32
|
factory_method: :my_method, context: :test
|
33
33
|
end
|
34
34
|
|
35
|
-
@bean_definition = SmartIoC.
|
35
|
+
@bean_definition = SmartIoC.find_bean_definition(:my_bean, :my_package, :test)
|
36
36
|
end
|
37
37
|
|
38
38
|
it { expect(@bean_definition.name).to eq(:my_bean) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_ioc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruslan Gatiyatov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-10-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
- !ruby/object:Gem::Version
|
136
136
|
version: '0'
|
137
137
|
requirements: []
|
138
|
-
rubygems_version: 3.5.
|
138
|
+
rubygems_version: 3.5.10
|
139
139
|
signing_key:
|
140
140
|
specification_version: 4
|
141
141
|
summary: Inversion of Control Container
|