smart_ioc 0.3.0 → 0.3.1
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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/smart_ioc/args.rb +1 -1
- data/lib/smart_ioc/bean_definitions_storage.rb +12 -1
- data/lib/smart_ioc/bean_factory.rb +11 -5
- data/lib/smart_ioc/container.rb +8 -6
- data/lib/smart_ioc/errors.rb +13 -5
- data/lib/smart_ioc/iocify.rb +11 -1
- data/lib/smart_ioc/version.rb +1 -1
- data/spec/smart_ioc/example/admins/repository/admins_repository.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8afa36b74a66cb6082823684679f4fd9d5f974f0
|
4
|
+
data.tar.gz: 715809ca8d2dcbb7020acad09ba3a6f39e4883f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4badf8cf4a9da4a48909e7d827f1881d50c003415f50cb1d763976f4e2d8ece131c4b2b4f4860dd3338d0cf1d5bf8d41d2e5bce343f3612b0b9f4c46ba1d30f0
|
7
|
+
data.tar.gz: cb8e5693a785030019776cbabf90f9d5abeeed5cb0726efb96292cb20f078ed5d9cd51bbe7edf1fd9af707890387db0328eef7efc05add950e3a279cd8f22f52
|
data/Gemfile.lock
CHANGED
data/lib/smart_ioc/args.rb
CHANGED
@@ -73,10 +73,21 @@ Existing bean details:
|
|
73
73
|
# @bean_name [Symbol] bean name
|
74
74
|
# @package [Symbol, nil] package name
|
75
75
|
# @context [Symbol, nil] context
|
76
|
+
# @package [Symbol, nil] parent_package name of parent package
|
76
77
|
# @raises AmbiguousBeanDefinition if multiple bean definitions are found
|
77
|
-
def find(bean_name, package = nil, context = nil)
|
78
|
+
def find(bean_name, package = nil, context = nil, parent_package = nil)
|
78
79
|
bds = filter_by_with_drop_to_default_context(bean_name, package, context)
|
79
80
|
|
81
|
+
if bds.size > 1 && parent_package
|
82
|
+
bean_definition = bds.detect do |bd|
|
83
|
+
bd.package == parent_package
|
84
|
+
end
|
85
|
+
|
86
|
+
if bean_definition
|
87
|
+
bds = [bean_definition]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
80
91
|
if bds.size > 1
|
81
92
|
raise AmbiguousBeanDefinition.new(bean_name, bds)
|
82
93
|
elsif bds.size == 0
|
@@ -26,19 +26,22 @@ class SmartIoC::BeanFactory
|
|
26
26
|
# Get bean from the container by it's name, package, context
|
27
27
|
# @param bean_name [Symbol] bean name
|
28
28
|
# @param package [Symbol] package name
|
29
|
+
# @param parent_bean_definition [SmartIoC::BeanDefinition] parent bean definition
|
29
30
|
# @param context [Symbol] context
|
30
31
|
# @return bean instance
|
31
32
|
# @raise [ArgumentError] if bean is not found
|
32
33
|
# @raise [ArgumentError] if ambiguous bean definition was found
|
33
|
-
def get_bean(bean_name, package: nil, context: nil)
|
34
|
+
def get_bean(bean_name, package: nil, parent_bean_definition: nil, context: nil)
|
34
35
|
check_arg(bean_name, :bean_name, Symbol)
|
35
36
|
check_arg(package, :package, Symbol) if package
|
37
|
+
check_arg(parent_bean_definition, :parent_bean_definition, SmartIoC::BeanDefinition) if parent_bean_definition
|
36
38
|
check_arg(context, :context, Symbol) if context
|
37
39
|
|
38
40
|
@bean_file_loader.require_bean(bean_name)
|
39
41
|
|
40
|
-
|
41
|
-
|
42
|
+
parent_package_name = parent_bean_definition ? parent_bean_definition.package : nil
|
43
|
+
context = autodetect_context(bean_name, package, parent_package_name, context)
|
44
|
+
bean_definition = @bean_definitions_storage.find(bean_name, package, context, parent_package_name)
|
42
45
|
scope = get_scope(bean_definition)
|
43
46
|
bean = scope.get_bean(bean_definition.klass)
|
44
47
|
|
@@ -48,6 +51,9 @@ class SmartIoC::BeanFactory
|
|
48
51
|
|
49
52
|
scope.save_bean(bean_definition.klass, bean)
|
50
53
|
bean
|
54
|
+
rescue SmartIoC::Errors::AmbiguousBeanDefinition => e
|
55
|
+
e.parent_bean_definition = parent_bean_definition
|
56
|
+
raise e
|
51
57
|
end
|
52
58
|
|
53
59
|
private
|
@@ -70,13 +76,13 @@ class SmartIoC::BeanFactory
|
|
70
76
|
bean
|
71
77
|
end
|
72
78
|
|
73
|
-
def autodetect_context(bean_name, package, context)
|
79
|
+
def autodetect_context(bean_name, package, parent_bean_package, context)
|
74
80
|
return context if context
|
75
81
|
|
76
82
|
if package
|
77
83
|
@extra_package_contexts.get_context(package)
|
78
84
|
else
|
79
|
-
bean_definition = autodetect_bean_definition(bean_name, package,
|
85
|
+
bean_definition = autodetect_bean_definition(bean_name, package, parent_bean_package)
|
80
86
|
bean_definition.context
|
81
87
|
end
|
82
88
|
end
|
data/lib/smart_ioc/container.rb
CHANGED
@@ -13,10 +13,6 @@ module SmartIoC
|
|
13
13
|
def clear
|
14
14
|
@container = nil
|
15
15
|
end
|
16
|
-
|
17
|
-
def get_bean(bean_name, package = nil, context = nil)
|
18
|
-
get_instance.get_bean(bean_name, package: package, context: context)
|
19
|
-
end
|
20
16
|
end
|
21
17
|
|
22
18
|
def initialize
|
@@ -118,10 +114,16 @@ module SmartIoC
|
|
118
114
|
|
119
115
|
# @param bean_name [Symbol] bean name
|
120
116
|
# @param optional package [Symbol] package name
|
117
|
+
# @param optional parent_bean_definition [SmartIoc::BeanDefinition] bean definition of parent bean
|
121
118
|
# @param optional context [Symbol] package context
|
122
119
|
# @return bean instance from container
|
123
|
-
def get_bean(bean_name, package: nil, context: nil)
|
124
|
-
bean_factory.get_bean(
|
120
|
+
def get_bean(bean_name, package: nil, context: nil, parent_bean_definition: nil)
|
121
|
+
bean_factory.get_bean(
|
122
|
+
bean_name,
|
123
|
+
package: package,
|
124
|
+
parent_bean_definition: parent_bean_definition,
|
125
|
+
context: context,
|
126
|
+
)
|
125
127
|
end
|
126
128
|
|
127
129
|
def clear_scopes
|
data/lib/smart_ioc/errors.rb
CHANGED
@@ -6,12 +6,20 @@ module SmartIoC::Errors
|
|
6
6
|
end
|
7
7
|
|
8
8
|
class AmbiguousBeanDefinition < StandardError
|
9
|
+
attr_accessor :parent_bean_definition
|
10
|
+
|
9
11
|
def initialize(bean_name, bean_definitions)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
@bean_name = bean_name
|
13
|
+
@bean_definitions = bean_definitions
|
14
|
+
end
|
15
|
+
|
16
|
+
def message
|
17
|
+
<<~EOS
|
18
|
+
Unable to inject bean :#{@bean_name}#{@parent_bean_definition ? " into :#{@parent_bean_definition.name} (package: #{@parent_bean_definition.package})" : ""}.
|
19
|
+
Several bean definitions with name :#{@bean_name} were found:
|
20
|
+
|
21
|
+
#{@bean_definitions.map(&:inspect).join("\n\n")}
|
22
|
+
EOS
|
15
23
|
end
|
16
24
|
end
|
17
25
|
end
|
data/lib/smart_ioc/iocify.rb
CHANGED
@@ -95,7 +95,17 @@ module SmartIoC::Iocify
|
|
95
95
|
bean_method = Proc.new do
|
96
96
|
bean = instance_variable_get(:"@#{bean_name}")
|
97
97
|
return bean if bean
|
98
|
-
|
98
|
+
|
99
|
+
klass = self.is_a?(Class) ? self : self.class
|
100
|
+
bean_definition = SmartIoC::Container.get_instance.get_bean_definition_by_class(klass)
|
101
|
+
|
102
|
+
bean = SmartIoC::Container.get_instance.get_bean(
|
103
|
+
ref || bean_name,
|
104
|
+
package: from,
|
105
|
+
parent_bean_definition: bean_definition
|
106
|
+
)
|
107
|
+
|
108
|
+
instance_variable_set(:"@#{bean_name}", bean)
|
99
109
|
end
|
100
110
|
|
101
111
|
if bean_definition.is_instance?
|
data/lib/smart_ioc/version.rb
CHANGED
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.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruslan Gatiyatov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|