smart_ioc 0.3.0 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile.lock +26 -27
- data/lib/smart_ioc.rb +3 -1
- data/lib/smart_ioc/args.rb +1 -1
- data/lib/smart_ioc/bean.rb +33 -0
- data/lib/smart_ioc/bean_definition.rb +1 -2
- data/lib/smart_ioc/bean_definitions_storage.rb +34 -12
- data/lib/smart_ioc/bean_factory.rb +14 -8
- data/lib/smart_ioc/container.rb +14 -11
- data/lib/smart_ioc/errors.rb +13 -5
- data/lib/smart_ioc/extra_package_contexts.rb +4 -0
- data/lib/smart_ioc/iocify.rb +66 -11
- data/lib/smart_ioc/version.rb +1 -1
- data/smart_ioc.gemspec +1 -1
- data/spec/smart_ioc/bean_definition_spec.rb +1 -2
- data/spec/smart_ioc/bean_factory_spec.rb +32 -17
- data/spec/smart_ioc/example/admins/repository/admins_dao.rb +2 -4
- data/spec/smart_ioc/example/admins/repository/admins_repository.rb +2 -8
- data/spec/smart_ioc/object_spec.rb +1 -1
- data/spec/smart_ioc/recursive_spec.rb +1 -1
- data/spec/smart_ioc/smart_ioc_spec.rb +4 -2
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a58c5f84034596665181c7a368aa35b2c9ed780e0dce715d538e4e142a921be4
|
4
|
+
data.tar.gz: 462be2bd543accc11130c90f86589247116a4d939c62aaca8e71f57db99cca5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64c18ef92109604e6eef73fec45c2dd8c9b7c84b1e5472b5f3cdf5b3a800490fa7aec50b9de5a508c1c62abf9739f571cf7393a318eec2558d2b6fd1a978211a
|
7
|
+
data.tar.gz: 1705b4a8b407707aaf00016f79c941a68250f52a1ef4e8071ad55c7f4f0c0fee03d786b3acfc40b59d8f6c2635cd7df65c7cb69e2505b07a72f7aac87b8c2ad1
|
data/Gemfile.lock
CHANGED
@@ -1,45 +1,44 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
smart_ioc (0.3.
|
4
|
+
smart_ioc (0.3.7)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
byebug (
|
10
|
-
codecov (0.
|
9
|
+
byebug (11.1.3)
|
10
|
+
codecov (0.2.5)
|
11
|
+
colorize
|
11
12
|
json
|
12
13
|
simplecov
|
13
|
-
|
14
|
-
diff-lcs (1.
|
15
|
-
docile (1.
|
16
|
-
json (2.
|
17
|
-
rake (
|
18
|
-
rspec (3.
|
19
|
-
rspec-core (~> 3.
|
20
|
-
rspec-expectations (~> 3.
|
21
|
-
rspec-mocks (~> 3.
|
22
|
-
rspec-core (3.
|
23
|
-
rspec-support (~> 3.
|
24
|
-
rspec-expectations (3.
|
14
|
+
colorize (0.8.1)
|
15
|
+
diff-lcs (1.4.4)
|
16
|
+
docile (1.3.2)
|
17
|
+
json (2.3.1)
|
18
|
+
rake (13.0.1)
|
19
|
+
rspec (3.9.0)
|
20
|
+
rspec-core (~> 3.9.0)
|
21
|
+
rspec-expectations (~> 3.9.0)
|
22
|
+
rspec-mocks (~> 3.9.0)
|
23
|
+
rspec-core (3.9.2)
|
24
|
+
rspec-support (~> 3.9.3)
|
25
|
+
rspec-expectations (3.9.2)
|
25
26
|
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
-
rspec-support (~> 3.
|
27
|
-
rspec-mocks (3.
|
27
|
+
rspec-support (~> 3.9.0)
|
28
|
+
rspec-mocks (3.9.1)
|
28
29
|
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
-
rspec-support (~> 3.
|
30
|
-
rspec-support (3.
|
31
|
-
simplecov (0.
|
32
|
-
docile (~> 1.1
|
33
|
-
|
34
|
-
|
35
|
-
simplecov-html (0.10.0)
|
36
|
-
url (0.3.2)
|
30
|
+
rspec-support (~> 3.9.0)
|
31
|
+
rspec-support (3.9.3)
|
32
|
+
simplecov (0.18.5)
|
33
|
+
docile (~> 1.1)
|
34
|
+
simplecov-html (~> 0.11)
|
35
|
+
simplecov-html (0.12.2)
|
37
36
|
|
38
37
|
PLATFORMS
|
39
38
|
ruby
|
40
39
|
|
41
40
|
DEPENDENCIES
|
42
|
-
bundler
|
41
|
+
bundler
|
43
42
|
byebug
|
44
43
|
codecov
|
45
44
|
rake
|
@@ -48,4 +47,4 @@ DEPENDENCIES
|
|
48
47
|
smart_ioc!
|
49
48
|
|
50
49
|
BUNDLED WITH
|
51
|
-
1.
|
50
|
+
2.1.2
|
data/lib/smart_ioc.rb
CHANGED
@@ -73,7 +73,7 @@ module SmartIoC
|
|
73
73
|
Container.get_instance
|
74
74
|
end
|
75
75
|
|
76
|
-
[:register_bean, :
|
76
|
+
[:register_bean, :get_bean_definition,
|
77
77
|
:set_extra_context_for_package, :get_bean, :clear_scopes,
|
78
78
|
:force_clear_scopes, :set_load_proc].each do |name|
|
79
79
|
define_method name do |*args, &block|
|
@@ -82,3 +82,5 @@ module SmartIoC
|
|
82
82
|
end
|
83
83
|
end
|
84
84
|
end
|
85
|
+
|
86
|
+
require 'smart_ioc/bean'
|
data/lib/smart_ioc/args.rb
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
def bean(bean_name, &proc)
|
2
|
+
raise ArgumentError, "name should be a Symbol" if !bean_name.is_a?(Symbol)
|
3
|
+
raise ArgumentError, "proc should be provided" if !block_given?
|
4
|
+
|
5
|
+
klass = Class.new do
|
6
|
+
include SmartIoC::Iocify
|
7
|
+
end
|
8
|
+
|
9
|
+
klass.instance_variable_set(:@anonymous_bean, true)
|
10
|
+
klass.instance_exec(&proc)
|
11
|
+
|
12
|
+
file_path = caller[0].split(':').first
|
13
|
+
package = SmartIoC::BeanLocations.get_bean_package(file_path)
|
14
|
+
|
15
|
+
klass.instance_exec do
|
16
|
+
bean(
|
17
|
+
bean_name,
|
18
|
+
file_path: file_path,
|
19
|
+
scope: instance_variable_get(:@scope) || nil,
|
20
|
+
package: instance_variable_get(:@package) || package,
|
21
|
+
instance: instance_variable_get(:@instance) || false,
|
22
|
+
factory_method: instance_variable_get(:@factory_method) || nil,
|
23
|
+
context: instance_variable_get(:@context) || nil,
|
24
|
+
after_init: instance_variable_get(:@after_init) || nil
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
(klass.instance_variable_get(:@injects) || []).each do |inject|
|
29
|
+
klass.register_inject(inject[:bean_name], ref: inject[:ref], from: inject[:from])
|
30
|
+
end
|
31
|
+
|
32
|
+
klass
|
33
|
+
end
|
@@ -47,7 +47,7 @@ class SmartIoC::BeanDefinition
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def ==(bean_definition)
|
50
|
-
bean_definition.
|
50
|
+
bean_definition.name == @name && bean_definition.package == @package && bean_definition.context == @context
|
51
51
|
end
|
52
52
|
|
53
53
|
def singleton?
|
@@ -56,7 +56,6 @@ class SmartIoC::BeanDefinition
|
|
56
56
|
|
57
57
|
def inspect
|
58
58
|
str = []
|
59
|
-
str << "class: #{@klass}"
|
60
59
|
str << "name: :#{@name}"
|
61
60
|
str << "package: :#{@package}"
|
62
61
|
str << "context: :#{@context}"
|
@@ -20,15 +20,18 @@ class SmartIoC::BeanDefinitionsStorage
|
|
20
20
|
end
|
21
21
|
|
22
22
|
if existing_bd
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
23
|
+
@collection.reject! { |bd| bd == existing_bd }
|
24
|
+
|
25
|
+
message = <<~EOF
|
26
|
+
\nReplacing bean definition...
|
27
|
+
- New bean details:
|
28
|
+
#{bean_definition.inspect}
|
29
|
+
- Existing bean details:
|
30
|
+
#{existing_bd.inspect})
|
31
|
+
|
32
|
+
EOF
|
33
|
+
|
34
|
+
puts message
|
32
35
|
end
|
33
36
|
|
34
37
|
@collection.push(bean_definition)
|
@@ -46,8 +49,16 @@ Existing bean details:
|
|
46
49
|
# @param klass [Class] bean class
|
47
50
|
# @return bean definition [BeanDefinition] or nil
|
48
51
|
def find_by_class(klass)
|
49
|
-
|
50
|
-
|
52
|
+
@collection.detect {|bd| bd.klass == klass}
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns bean definition for specific class
|
56
|
+
# @param bean_name [Symbol]
|
57
|
+
# @param package [Symbol]
|
58
|
+
# @param context [Symbol]
|
59
|
+
# @return bean definition [BeanDefinition] or nil
|
60
|
+
def find_bean(bean_name, package, context)
|
61
|
+
@collection.detect {|bd| bd.name == bean_name && bd.package == package && bd.context == context}
|
51
62
|
end
|
52
63
|
|
53
64
|
def filter_by(bean_name, package = nil, context = nil)
|
@@ -73,10 +84,21 @@ Existing bean details:
|
|
73
84
|
# @bean_name [Symbol] bean name
|
74
85
|
# @package [Symbol, nil] package name
|
75
86
|
# @context [Symbol, nil] context
|
87
|
+
# @package [Symbol, nil] parent_package name of parent package
|
76
88
|
# @raises AmbiguousBeanDefinition if multiple bean definitions are found
|
77
|
-
def find(bean_name, package = nil, context = nil)
|
89
|
+
def find(bean_name, package = nil, context = nil, parent_package = nil)
|
78
90
|
bds = filter_by_with_drop_to_default_context(bean_name, package, context)
|
79
91
|
|
92
|
+
if bds.size > 1 && parent_package
|
93
|
+
bean_definition = bds.detect do |bd|
|
94
|
+
bd.package == parent_package
|
95
|
+
end
|
96
|
+
|
97
|
+
if bean_definition
|
98
|
+
bds = [bean_definition]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
80
102
|
if bds.size > 1
|
81
103
|
raise AmbiguousBeanDefinition.new(bean_name, bds)
|
82
104
|
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, parent_bean_name: 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, parent_bean_name)
|
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,18 +76,18 @@ 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, parent_bean_name)
|
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,
|
80
|
-
bean_definition.
|
85
|
+
bean_definition = autodetect_bean_definition(bean_name, package, parent_bean_package, parent_bean_name)
|
86
|
+
@extra_package_contexts.get_context(bean_definition.package)
|
81
87
|
end
|
82
88
|
end
|
83
89
|
|
84
|
-
def autodetect_bean_definition(bean, package, parent_bean_package)
|
90
|
+
def autodetect_bean_definition(bean, package, parent_bean_package, parent_bean_name)
|
85
91
|
if package
|
86
92
|
bean_context = @extra_package_contexts.get_context(package)
|
87
93
|
bds = @bean_definitions_storage.filter_by_with_drop_to_default_context(bean, package, bean_context)
|
@@ -115,7 +121,7 @@ class SmartIoC::BeanFactory
|
|
115
121
|
|
116
122
|
if smart_bds.size > 1
|
117
123
|
raise ArgumentError.new(
|
118
|
-
%Q(Unable to autodetect bean :#{bean}.
|
124
|
+
%Q(Unable to autodetect bean :#{bean}#{parent_bean_name ? " for bean :#{parent_bean_name}" : ''}.
|
119
125
|
Several definitions were found:\n
|
120
126
|
#{smart_bds.map(&:inspect).join("\n\n")}.
|
121
127
|
Set package directly for injected dependency
|
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
|
@@ -61,8 +57,6 @@ module SmartIoC
|
|
61
57
|
raise ArgumentError, "bean scope should be one of #{allowed_scopes.inspect}"
|
62
58
|
end
|
63
59
|
|
64
|
-
package_name ||= SmartIoC::BeanLocations.get_bean_package(path)
|
65
|
-
|
66
60
|
if !package_name
|
67
61
|
raise ArgumentError, %Q(
|
68
62
|
Package name should be given for bean :#{bean_name}.
|
@@ -92,10 +86,12 @@ module SmartIoC
|
|
92
86
|
end
|
93
87
|
|
94
88
|
# Returns bean definition for specific class
|
95
|
-
# @param
|
89
|
+
# @param bean_name [Symbol]
|
90
|
+
# @param package [Symbol]
|
91
|
+
# @param context [Symbol]
|
96
92
|
# return [BeanDefinition]
|
97
|
-
def
|
98
|
-
bean_definitions_storage.
|
93
|
+
def get_bean_definition(bean_name, package, context)
|
94
|
+
bean_definitions_storage.find_bean(bean_name, package, context)
|
99
95
|
end
|
100
96
|
|
101
97
|
# Sets new load proc
|
@@ -118,10 +114,17 @@ 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, parent_bean_name: nil)
|
121
|
+
bean_factory.get_bean(
|
122
|
+
bean_name,
|
123
|
+
package: package,
|
124
|
+
parent_bean_definition: parent_bean_definition,
|
125
|
+
context: context,
|
126
|
+
parent_bean_name: parent_bean_name,
|
127
|
+
)
|
125
128
|
end
|
126
129
|
|
127
130
|
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
@@ -2,6 +2,9 @@
|
|
2
2
|
# Example of usage:
|
3
3
|
# class Bar
|
4
4
|
# bean :bar
|
5
|
+
#
|
6
|
+
# def call
|
7
|
+
# end
|
5
8
|
# end
|
6
9
|
#
|
7
10
|
# class Foo
|
@@ -12,6 +15,7 @@
|
|
12
15
|
# inject :some_bar, ref: bar, from: :repository
|
13
16
|
#
|
14
17
|
# def hello_world
|
18
|
+
# some_bar.call
|
15
19
|
# puts 'Hello world'
|
16
20
|
# end
|
17
21
|
# end
|
@@ -23,6 +27,35 @@ module SmartIoC::Iocify
|
|
23
27
|
end
|
24
28
|
|
25
29
|
module ClassMethods
|
30
|
+
def package(name)
|
31
|
+
raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol)
|
32
|
+
@package = name
|
33
|
+
end
|
34
|
+
|
35
|
+
def context(name)
|
36
|
+
raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol)
|
37
|
+
@context = name
|
38
|
+
end
|
39
|
+
|
40
|
+
def factory_method(name)
|
41
|
+
raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol)
|
42
|
+
@factory_method = name
|
43
|
+
end
|
44
|
+
|
45
|
+
def scope(name)
|
46
|
+
raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol)
|
47
|
+
@scope = name
|
48
|
+
end
|
49
|
+
|
50
|
+
def instance
|
51
|
+
@instance = true
|
52
|
+
end
|
53
|
+
|
54
|
+
def after_init(name)
|
55
|
+
raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol)
|
56
|
+
@after_init = name
|
57
|
+
end
|
58
|
+
|
26
59
|
# @param bean_name [Symbol] bean name
|
27
60
|
# @param scope [Symbol] bean scope (defaults to :singleton)
|
28
61
|
# @param package [nil or Symbol]
|
@@ -31,10 +64,11 @@ module SmartIoC::Iocify
|
|
31
64
|
# @param context [Symbol] set bean context (ex: :test)
|
32
65
|
# @param after_init [Symbol] name of bean method that will be called after bean initialization (ex: :test)
|
33
66
|
# @return nil
|
34
|
-
def bean(bean_name, scope: nil, package: nil, instance: true, factory_method: nil, context: nil, after_init: nil)
|
35
|
-
file_path
|
36
|
-
|
37
|
-
|
67
|
+
def bean(bean_name, scope: nil, package: nil, instance: true, factory_method: nil, context: nil, after_init: nil, file_path: nil)
|
68
|
+
file_path ||= caller[0].split(':').first
|
69
|
+
package ||= SmartIoC::BeanLocations.get_bean_package(file_path)
|
70
|
+
context ||= SmartIoC::Container::DEFAULT_CONTEXT
|
71
|
+
bean_definition = SmartIoC.get_bean_definition(bean_name, package, context)
|
38
72
|
|
39
73
|
if bean_definition
|
40
74
|
if bean_definition.path == file_path
|
@@ -68,9 +102,20 @@ module SmartIoC::Iocify
|
|
68
102
|
)
|
69
103
|
end
|
70
104
|
|
105
|
+
self.instance_variable_set(:@bean_definition, bean_definition)
|
106
|
+
|
71
107
|
nil
|
72
108
|
end
|
73
109
|
|
110
|
+
def inject(bean_name, ref: nil, from: nil)
|
111
|
+
if @anonymous_bean
|
112
|
+
@injects ||= []
|
113
|
+
@injects.push({bean_name: bean_name, ref: ref, from: from})
|
114
|
+
else
|
115
|
+
register_inject(bean_name, ref: ref, from: from)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
74
119
|
# @param bean_name [Symbol] injected bean name
|
75
120
|
# @param ref [Symbol] refferece bean to be sef as bean_name
|
76
121
|
# @param from [Symbol] package name
|
@@ -79,14 +124,14 @@ module SmartIoC::Iocify
|
|
79
124
|
# @raise [ArgumentError] if ref provided and ref is not a Symbol
|
80
125
|
# @raise [ArgumentError] if from provided and from is not a Symbol
|
81
126
|
# @raise [ArgumentError] if bean with same name was injected before
|
82
|
-
def
|
83
|
-
bean_definition
|
84
|
-
|
85
|
-
if bean_definition.nil?
|
127
|
+
def register_inject(bean_name, ref: nil, from: nil)
|
128
|
+
if !@bean_definition
|
86
129
|
raise ArgumentError, "#{self.to_s} is not registered as bean. Add `bean :bean_name` declaration"
|
87
130
|
end
|
88
131
|
|
89
|
-
bean_definition
|
132
|
+
bd = @bean_definition
|
133
|
+
|
134
|
+
bd.add_dependency(
|
90
135
|
bean_name: bean_name,
|
91
136
|
ref: ref,
|
92
137
|
package: from
|
@@ -95,10 +140,20 @@ module SmartIoC::Iocify
|
|
95
140
|
bean_method = Proc.new do
|
96
141
|
bean = instance_variable_get(:"@#{bean_name}")
|
97
142
|
return bean if bean
|
98
|
-
|
143
|
+
|
144
|
+
klass = self.is_a?(Class) ? self : self.class
|
145
|
+
|
146
|
+
bean = SmartIoC::Container.get_instance.get_bean(
|
147
|
+
ref || bean_name,
|
148
|
+
package: from,
|
149
|
+
parent_bean_definition: bd,
|
150
|
+
parent_bean_name: bd.name,
|
151
|
+
)
|
152
|
+
|
153
|
+
instance_variable_set(:"@#{bean_name}", bean)
|
99
154
|
end
|
100
155
|
|
101
|
-
if
|
156
|
+
if bd.is_instance?
|
102
157
|
define_method bean_name, &bean_method
|
103
158
|
private bean_name
|
104
159
|
else
|
data/lib/smart_ioc/version.rb
CHANGED
data/smart_ioc.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(spec)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_development_dependency 'bundler'
|
21
|
+
spec.add_development_dependency 'bundler'
|
22
22
|
spec.add_development_dependency 'rake'
|
23
23
|
spec.add_development_dependency "codecov"
|
24
24
|
end
|
@@ -4,27 +4,42 @@ describe SmartIoC::BeanFactory do
|
|
4
4
|
before :all do
|
5
5
|
SmartIoC.clear
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
bean :repo do
|
8
|
+
context :default
|
9
|
+
package :bean_factory
|
10
|
+
|
11
|
+
def call
|
12
|
+
'default'
|
13
|
+
end
|
10
14
|
end
|
11
15
|
|
12
|
-
|
13
|
-
|
14
|
-
|
16
|
+
bean :repo do
|
17
|
+
context :test
|
18
|
+
package :bean_factory
|
19
|
+
|
20
|
+
inject :dao
|
21
|
+
|
22
|
+
def call
|
23
|
+
dao.call
|
24
|
+
'test'
|
25
|
+
end
|
15
26
|
end
|
16
27
|
|
17
|
-
|
18
|
-
|
19
|
-
|
28
|
+
bean :dao do
|
29
|
+
context :default
|
30
|
+
package :bean_factory
|
31
|
+
|
32
|
+
def call
|
33
|
+
'dao'
|
34
|
+
end
|
20
35
|
end
|
21
36
|
|
22
37
|
class TestObject
|
23
38
|
end
|
24
39
|
|
25
|
-
|
26
|
-
|
27
|
-
|
40
|
+
bean :factory do
|
41
|
+
factory_method :build_bean
|
42
|
+
package :bean_factory
|
28
43
|
|
29
44
|
def build_bean
|
30
45
|
TestObject.new
|
@@ -56,17 +71,17 @@ describe SmartIoC::BeanFactory do
|
|
56
71
|
|
57
72
|
it 'returns proper bean for test context' do
|
58
73
|
SmartIoC.set_extra_context_for_package(:bean_factory, :test)
|
59
|
-
expect(SmartIoC.get_bean(:repo)).to
|
74
|
+
expect(SmartIoC.get_bean(:repo).call).to eq('test')
|
60
75
|
end
|
61
76
|
|
62
77
|
it 'returns proper bean for default context' do
|
63
78
|
SmartIoC.set_extra_context_for_package(:bean_factory, :default)
|
64
|
-
expect(SmartIoC.get_bean(:repo)).to
|
79
|
+
expect(SmartIoC.get_bean(:repo).call).to eq('default')
|
65
80
|
end
|
66
81
|
|
67
82
|
it 'returns proper bean for test context with fallback to default context' do
|
68
83
|
SmartIoC.set_extra_context_for_package(:bean_factory, :test)
|
69
|
-
expect(SmartIoC.get_bean(:dao)).to
|
84
|
+
expect(SmartIoC.get_bean(:dao).call).to eq('dao')
|
70
85
|
end
|
71
86
|
|
72
87
|
it 'updates dependencies' do
|
@@ -154,7 +169,7 @@ describe SmartIoC::BeanFactory do
|
|
154
169
|
end
|
155
170
|
|
156
171
|
it 'injects prototype beans with different object id' do
|
157
|
-
prototype_bean = SmartIoC.get_bean(:prototype_bean)
|
172
|
+
prototype_bean = SmartIoC.get_bean(:prototype_bean, package: :prototype)
|
158
173
|
repo1_object_id = prototype_bean.prototype_service1.prototype_repo.object_id
|
159
174
|
repo2_object_id = prototype_bean.prototype_service2.prototype_repo.object_id
|
160
175
|
|
@@ -162,7 +177,7 @@ describe SmartIoC::BeanFactory do
|
|
162
177
|
end
|
163
178
|
|
164
179
|
it 'injects singleton beans with same object id' do
|
165
|
-
prototype_bean = SmartIoC.get_bean(:prototype_bean)
|
180
|
+
prototype_bean = SmartIoC.get_bean(:prototype_bean, package: :prototype)
|
166
181
|
repo1_object_id = prototype_bean.prototype_service1.singleton_repo.object_id
|
167
182
|
repo2_object_id = prototype_bean.prototype_service2.singleton_repo.object_id
|
168
183
|
|
@@ -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.get_bean_definition(:my_bean, :my_package, :test)
|
36
36
|
end
|
37
37
|
|
38
38
|
it { expect(@bean_definition.name).to eq(:my_bean) }
|
@@ -18,7 +18,7 @@ describe SmartIoC::Container do
|
|
18
18
|
|
19
19
|
it 'loads recursive beans' do
|
20
20
|
users_creator = @container.get_bean(:users_creator)
|
21
|
-
uc2 = users_creator.send(:repository).users_creator
|
21
|
+
uc2 = users_creator.send(:repository).send(:users_creator)
|
22
22
|
expect(users_creator).to eq(uc2)
|
23
23
|
end
|
24
24
|
end
|
@@ -27,14 +27,16 @@ describe SmartIoC do
|
|
27
27
|
repository = @container.get_bean(:repository, package: :admins)
|
28
28
|
|
29
29
|
expect(repository.get(1)).to be_a(User)
|
30
|
-
expect(users_creator.send(:repository)).to be_a(
|
30
|
+
expect(users_creator.send(:repository)).to be_a(Class)
|
31
|
+
expect(users_creator.send(:repository).methods).to include(:put)
|
32
|
+
expect(users_creator.send(:repository).methods).to include(:get)
|
31
33
|
expect(users_creator.send(:logger)).to be_a(LoggerFactory::SmartIoCLogger)
|
32
34
|
end
|
33
35
|
|
34
36
|
it 'sets beans with extra package context' do
|
35
37
|
SmartIoC.set_extra_context_for_package(:admins, :test)
|
36
38
|
SmartIoC.force_clear_scopes
|
37
|
-
|
39
|
+
|
38
40
|
users_creator = @container.get_bean(:users_creator)
|
39
41
|
users_creator.create(1, 'test@test.com')
|
40
42
|
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
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.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruslan Gatiyatov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- docs/index.md
|
74
74
|
- lib/smart_ioc.rb
|
75
75
|
- lib/smart_ioc/args.rb
|
76
|
+
- lib/smart_ioc/bean.rb
|
76
77
|
- lib/smart_ioc/bean_definition.rb
|
77
78
|
- lib/smart_ioc/bean_definitions_storage.rb
|
78
79
|
- lib/smart_ioc/bean_dependency.rb
|
@@ -133,8 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
134
|
- !ruby/object:Gem::Version
|
134
135
|
version: '0'
|
135
136
|
requirements: []
|
136
|
-
|
137
|
-
rubygems_version: 2.5.2
|
137
|
+
rubygems_version: 3.1.2
|
138
138
|
signing_key:
|
139
139
|
specification_version: 4
|
140
140
|
summary: Inversion of Control Container
|