smart_ioc 0.3.9 → 0.4.0
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 +22 -24
- data/lib/smart_ioc/bean_definition.rb +2 -1
- data/lib/smart_ioc/bean_definitions_storage.rb +11 -22
- data/lib/smart_ioc/bean_factory.rb +6 -6
- data/lib/smart_ioc/container.rb +6 -7
- data/lib/smart_ioc/iocify.rb +12 -57
- data/lib/smart_ioc/version.rb +1 -1
- data/lib/smart_ioc.rb +14 -10
- data/spec/smart_ioc/bean_definition_spec.rb +2 -1
- data/spec/smart_ioc/bean_factory_spec.rb +17 -32
- data/spec/smart_ioc/bean_locator_spec.rb +2 -2
- data/spec/smart_ioc/example/admins/repository/admins_dao.rb +4 -2
- data/spec/smart_ioc/example/admins/repository/admins_repository.rb +7 -1
- 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 +2 -4
- metadata +3 -5
- data/lib/smart_ioc/bean.rb +0 -59
- data/lib/smart_ioc/string_utils.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec0b7417468b3bf40350a297a885266b3067809f6ca074326614b8663ca762bb
|
4
|
+
data.tar.gz: 6508ed9fdee9191479459541718c63172bd7a9d47fa1de63bdd9ab883737927b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2daa5061878b44419bbb0d3e4e2c5c7edcc526259b27e04f1600a3b2c994e06a03722c160e6b6c01a67a587cb99efa2dd385bbc215f275989ec0e622d78eb9a7
|
7
|
+
data.tar.gz: fb7fbdc1dc05f2462f5aaf399564423798129e9e6ed324dceaa8e47747707083c40857f6528047c0d703b2cad45889900cf60924e33d339e775ef31215c97f66
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
3.1.1
|
data/Gemfile.lock
CHANGED
@@ -1,38 +1,36 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
smart_ioc (0.
|
4
|
+
smart_ioc (0.4.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
9
|
byebug (11.1.3)
|
10
|
-
codecov (0.
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
rspec (3.
|
20
|
-
rspec-
|
21
|
-
|
22
|
-
rspec-mocks (~> 3.9.0)
|
23
|
-
rspec-core (3.9.2)
|
24
|
-
rspec-support (~> 3.9.3)
|
25
|
-
rspec-expectations (3.9.2)
|
10
|
+
codecov (0.6.0)
|
11
|
+
simplecov (>= 0.15, < 0.22)
|
12
|
+
diff-lcs (1.5.0)
|
13
|
+
docile (1.4.0)
|
14
|
+
rake (13.0.6)
|
15
|
+
rspec (3.11.0)
|
16
|
+
rspec-core (~> 3.11.0)
|
17
|
+
rspec-expectations (~> 3.11.0)
|
18
|
+
rspec-mocks (~> 3.11.0)
|
19
|
+
rspec-core (3.11.0)
|
20
|
+
rspec-support (~> 3.11.0)
|
21
|
+
rspec-expectations (3.11.0)
|
26
22
|
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
-
rspec-support (~> 3.
|
28
|
-
rspec-mocks (3.
|
23
|
+
rspec-support (~> 3.11.0)
|
24
|
+
rspec-mocks (3.11.0)
|
29
25
|
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
-
rspec-support (~> 3.
|
31
|
-
rspec-support (3.
|
32
|
-
simplecov (0.
|
26
|
+
rspec-support (~> 3.11.0)
|
27
|
+
rspec-support (3.11.0)
|
28
|
+
simplecov (0.21.2)
|
33
29
|
docile (~> 1.1)
|
34
30
|
simplecov-html (~> 0.11)
|
35
|
-
|
31
|
+
simplecov_json_formatter (~> 0.1)
|
32
|
+
simplecov-html (0.12.3)
|
33
|
+
simplecov_json_formatter (0.1.4)
|
36
34
|
|
37
35
|
PLATFORMS
|
38
36
|
ruby
|
@@ -47,4 +45,4 @@ DEPENDENCIES
|
|
47
45
|
smart_ioc!
|
48
46
|
|
49
47
|
BUNDLED WITH
|
50
|
-
2.
|
48
|
+
2.1.4
|
@@ -47,7 +47,7 @@ class SmartIoC::BeanDefinition
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def ==(bean_definition)
|
50
|
-
bean_definition.
|
50
|
+
bean_definition.klass == @klass
|
51
51
|
end
|
52
52
|
|
53
53
|
def singleton?
|
@@ -56,6 +56,7 @@ class SmartIoC::BeanDefinition
|
|
56
56
|
|
57
57
|
def inspect
|
58
58
|
str = []
|
59
|
+
str << "class: #{@klass}"
|
59
60
|
str << "name: :#{@name}"
|
60
61
|
str << "package: :#{@package}"
|
61
62
|
str << "context: :#{@context}"
|
@@ -20,18 +20,15 @@ class SmartIoC::BeanDefinitionsStorage
|
|
20
20
|
end
|
21
21
|
|
22
22
|
if existing_bd
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
EOF
|
33
|
-
|
34
|
-
puts message
|
23
|
+
error_msg =
|
24
|
+
%Q(Not able to add bean to definitions storage.
|
25
|
+
Bean definition already exists.
|
26
|
+
New bean details:
|
27
|
+
#{bean_definition.inspect}
|
28
|
+
Existing bean details:
|
29
|
+
#{existing_bd.inspect})
|
30
|
+
|
31
|
+
raise ArgumentError, error_msg
|
35
32
|
end
|
36
33
|
|
37
34
|
@collection.push(bean_definition)
|
@@ -49,16 +46,8 @@ class SmartIoC::BeanDefinitionsStorage
|
|
49
46
|
# @param klass [Class] bean class
|
50
47
|
# @return bean definition [BeanDefinition] or nil
|
51
48
|
def find_by_class(klass)
|
52
|
-
|
53
|
-
|
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}
|
49
|
+
klass_str = klass.to_s
|
50
|
+
@collection.detect {|bd| bd.klass.to_s == klass_str}
|
62
51
|
end
|
63
52
|
|
64
53
|
def filter_by(bean_name, package = nil, context = nil)
|
@@ -31,7 +31,7 @@ class SmartIoC::BeanFactory
|
|
31
31
|
# @return bean instance
|
32
32
|
# @raise [ArgumentError] if bean is not found
|
33
33
|
# @raise [ArgumentError] if ambiguous bean definition was found
|
34
|
-
def get_bean(bean_name, package: nil, parent_bean_definition: nil, context: nil
|
34
|
+
def get_bean(bean_name, package: nil, parent_bean_definition: nil, context: nil)
|
35
35
|
check_arg(bean_name, :bean_name, Symbol)
|
36
36
|
check_arg(package, :package, Symbol) if package
|
37
37
|
check_arg(parent_bean_definition, :parent_bean_definition, SmartIoC::BeanDefinition) if parent_bean_definition
|
@@ -40,7 +40,7 @@ class SmartIoC::BeanFactory
|
|
40
40
|
@bean_file_loader.require_bean(bean_name)
|
41
41
|
|
42
42
|
parent_package_name = parent_bean_definition ? parent_bean_definition.package : nil
|
43
|
-
context = autodetect_context(bean_name, package, parent_package_name, context
|
43
|
+
context = autodetect_context(bean_name, package, parent_package_name, context)
|
44
44
|
bean_definition = @bean_definitions_storage.find(bean_name, package, context, parent_package_name)
|
45
45
|
scope = get_scope(bean_definition)
|
46
46
|
bean = scope.get_bean(bean_definition.klass)
|
@@ -76,18 +76,18 @@ class SmartIoC::BeanFactory
|
|
76
76
|
bean
|
77
77
|
end
|
78
78
|
|
79
|
-
def autodetect_context(bean_name, package, parent_bean_package, context
|
79
|
+
def autodetect_context(bean_name, package, parent_bean_package, context)
|
80
80
|
return context if context
|
81
81
|
|
82
82
|
if package
|
83
83
|
@extra_package_contexts.get_context(package)
|
84
84
|
else
|
85
|
-
bean_definition = autodetect_bean_definition(bean_name, package, parent_bean_package
|
85
|
+
bean_definition = autodetect_bean_definition(bean_name, package, parent_bean_package)
|
86
86
|
@extra_package_contexts.get_context(bean_definition.package)
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
|
-
def autodetect_bean_definition(bean, package, parent_bean_package
|
90
|
+
def autodetect_bean_definition(bean, package, parent_bean_package)
|
91
91
|
if package
|
92
92
|
bean_context = @extra_package_contexts.get_context(package)
|
93
93
|
bds = @bean_definitions_storage.filter_by_with_drop_to_default_context(bean, package, bean_context)
|
@@ -121,7 +121,7 @@ class SmartIoC::BeanFactory
|
|
121
121
|
|
122
122
|
if smart_bds.size > 1
|
123
123
|
raise ArgumentError.new(
|
124
|
-
%Q(Unable to autodetect bean :#{bean}
|
124
|
+
%Q(Unable to autodetect bean :#{bean}.
|
125
125
|
Several definitions were found:\n
|
126
126
|
#{smart_bds.map(&:inspect).join("\n\n")}.
|
127
127
|
Set package directly for injected dependency
|
data/lib/smart_ioc/container.rb
CHANGED
@@ -57,6 +57,8 @@ module SmartIoC
|
|
57
57
|
raise ArgumentError, "bean scope should be one of #{allowed_scopes.inspect}"
|
58
58
|
end
|
59
59
|
|
60
|
+
package_name ||= SmartIoC::BeanLocations.get_bean_package(path)
|
61
|
+
|
60
62
|
if !package_name
|
61
63
|
raise ArgumentError, %Q(
|
62
64
|
Package name should be given for bean :#{bean_name}.
|
@@ -86,12 +88,10 @@ module SmartIoC
|
|
86
88
|
end
|
87
89
|
|
88
90
|
# Returns bean definition for specific class
|
89
|
-
# @param
|
90
|
-
# @param package [Symbol]
|
91
|
-
# @param context [Symbol]
|
91
|
+
# @param klass [Class] class name
|
92
92
|
# return [BeanDefinition]
|
93
|
-
def
|
94
|
-
bean_definitions_storage.
|
93
|
+
def get_bean_definition_by_class(klass)
|
94
|
+
bean_definitions_storage.find_by_class(klass)
|
95
95
|
end
|
96
96
|
|
97
97
|
# Sets new load proc
|
@@ -117,13 +117,12 @@ module SmartIoC
|
|
117
117
|
# @param optional parent_bean_definition [SmartIoc::BeanDefinition] bean definition of parent bean
|
118
118
|
# @param optional context [Symbol] package context
|
119
119
|
# @return bean instance from container
|
120
|
-
def get_bean(bean_name, package: nil, context: nil, parent_bean_definition: nil
|
120
|
+
def get_bean(bean_name, package: nil, context: nil, parent_bean_definition: nil)
|
121
121
|
bean_factory.get_bean(
|
122
122
|
bean_name,
|
123
123
|
package: package,
|
124
124
|
parent_bean_definition: parent_bean_definition,
|
125
125
|
context: context,
|
126
|
-
parent_bean_name: parent_bean_name,
|
127
126
|
)
|
128
127
|
end
|
129
128
|
|
data/lib/smart_ioc/iocify.rb
CHANGED
@@ -2,9 +2,6 @@
|
|
2
2
|
# Example of usage:
|
3
3
|
# class Bar
|
4
4
|
# bean :bar
|
5
|
-
#
|
6
|
-
# def call
|
7
|
-
# end
|
8
5
|
# end
|
9
6
|
#
|
10
7
|
# class Foo
|
@@ -15,7 +12,6 @@
|
|
15
12
|
# inject :some_bar, ref: bar, from: :repository
|
16
13
|
#
|
17
14
|
# def hello_world
|
18
|
-
# some_bar.call
|
19
15
|
# puts 'Hello world'
|
20
16
|
# end
|
21
17
|
# end
|
@@ -27,35 +23,6 @@ module SmartIoC::Iocify
|
|
27
23
|
end
|
28
24
|
|
29
25
|
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
|
-
|
59
26
|
# @param bean_name [Symbol] bean name
|
60
27
|
# @param scope [Symbol] bean scope (defaults to :singleton)
|
61
28
|
# @param package [nil or Symbol]
|
@@ -64,11 +31,10 @@ module SmartIoC::Iocify
|
|
64
31
|
# @param context [Symbol] set bean context (ex: :test)
|
65
32
|
# @param after_init [Symbol] name of bean method that will be called after bean initialization (ex: :test)
|
66
33
|
# @return nil
|
67
|
-
def bean(bean_name, scope: nil, package: nil, instance: true, factory_method: nil, context: nil, after_init: nil
|
68
|
-
file_path
|
69
|
-
|
70
|
-
|
71
|
-
bean_definition = SmartIoC.get_bean_definition(bean_name, package, context)
|
34
|
+
def bean(bean_name, scope: nil, package: nil, instance: true, factory_method: nil, context: nil, after_init: nil)
|
35
|
+
file_path = caller[0].split(':').first
|
36
|
+
|
37
|
+
bean_definition = SmartIoC.get_bean_definition_by_class(self)
|
72
38
|
|
73
39
|
if bean_definition
|
74
40
|
if bean_definition.path == file_path
|
@@ -102,20 +68,9 @@ module SmartIoC::Iocify
|
|
102
68
|
)
|
103
69
|
end
|
104
70
|
|
105
|
-
self.instance_variable_set(:@bean_definition, bean_definition)
|
106
|
-
|
107
71
|
nil
|
108
72
|
end
|
109
73
|
|
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
|
-
|
119
74
|
# @param bean_name [Symbol] injected bean name
|
120
75
|
# @param ref [Symbol] refferece bean to be sef as bean_name
|
121
76
|
# @param from [Symbol] package name
|
@@ -124,14 +79,14 @@ module SmartIoC::Iocify
|
|
124
79
|
# @raise [ArgumentError] if ref provided and ref is not a Symbol
|
125
80
|
# @raise [ArgumentError] if from provided and from is not a Symbol
|
126
81
|
# @raise [ArgumentError] if bean with same name was injected before
|
127
|
-
def
|
128
|
-
|
82
|
+
def inject(bean_name, ref: nil, from: nil)
|
83
|
+
bean_definition = SmartIoC::Container.get_instance.get_bean_definition_by_class(self)
|
84
|
+
|
85
|
+
if bean_definition.nil?
|
129
86
|
raise ArgumentError, "#{self.to_s} is not registered as bean. Add `bean :bean_name` declaration"
|
130
87
|
end
|
131
88
|
|
132
|
-
|
133
|
-
|
134
|
-
bd.add_dependency(
|
89
|
+
bean_definition.add_dependency(
|
135
90
|
bean_name: bean_name,
|
136
91
|
ref: ref,
|
137
92
|
package: from
|
@@ -142,18 +97,18 @@ module SmartIoC::Iocify
|
|
142
97
|
return bean if bean
|
143
98
|
|
144
99
|
klass = self.is_a?(Class) ? self : self.class
|
100
|
+
bean_definition = SmartIoC::Container.get_instance.get_bean_definition_by_class(klass)
|
145
101
|
|
146
102
|
bean = SmartIoC::Container.get_instance.get_bean(
|
147
103
|
ref || bean_name,
|
148
104
|
package: from,
|
149
|
-
parent_bean_definition:
|
150
|
-
parent_bean_name: bd.name,
|
105
|
+
parent_bean_definition: bean_definition
|
151
106
|
)
|
152
107
|
|
153
108
|
instance_variable_set(:"@#{bean_name}", bean)
|
154
109
|
end
|
155
110
|
|
156
|
-
if
|
111
|
+
if bean_definition.is_instance?
|
157
112
|
define_method bean_name, &bean_method
|
158
113
|
private bean_name
|
159
114
|
else
|
data/lib/smart_ioc/version.rb
CHANGED
data/lib/smart_ioc.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'smart_ioc/version'
|
2
2
|
require 'benchmark'
|
3
|
+
require 'forwardable'
|
3
4
|
|
4
5
|
module SmartIoC
|
5
6
|
autoload :Args, 'smart_ioc/args'
|
@@ -15,7 +16,6 @@ module SmartIoC
|
|
15
16
|
autoload :InjectMetadata, 'smart_ioc/inject_metadata'
|
16
17
|
autoload :Iocify, 'smart_ioc/iocify'
|
17
18
|
autoload :Scopes, 'smart_ioc/scopes'
|
18
|
-
autoload :StringUtils, 'smart_ioc/string_utils'
|
19
19
|
|
20
20
|
module Scopes
|
21
21
|
autoload :Bean, 'smart_ioc/scopes/bean'
|
@@ -74,14 +74,18 @@ module SmartIoC
|
|
74
74
|
Container.get_instance
|
75
75
|
end
|
76
76
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
77
|
+
extend Forwardable
|
78
|
+
|
79
|
+
container_methods = [
|
80
|
+
:register_bean,
|
81
|
+
:get_bean_definition_by_class,
|
82
|
+
:set_extra_context_for_package,
|
83
|
+
:get_bean,
|
84
|
+
:clear_scopes,
|
85
|
+
:force_clear_scopes,
|
86
|
+
:set_load_proc
|
87
|
+
]
|
88
|
+
|
89
|
+
def_delegators :container, *container_methods
|
84
90
|
end
|
85
91
|
end
|
86
|
-
|
87
|
-
require 'smart_ioc/bean'
|
@@ -4,42 +4,27 @@ describe SmartIoC::BeanFactory do
|
|
4
4
|
before :all do
|
5
5
|
SmartIoC.clear
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
package :bean_factory
|
10
|
-
|
11
|
-
def call
|
12
|
-
'default'
|
13
|
-
end
|
7
|
+
class Repo
|
8
|
+
include SmartIoC::Iocify
|
9
|
+
bean :repo, context: :default, package: :bean_factory
|
14
10
|
end
|
15
11
|
|
16
|
-
|
17
|
-
|
18
|
-
package :bean_factory
|
19
|
-
|
20
|
-
inject :dao
|
21
|
-
|
22
|
-
def call
|
23
|
-
dao.call
|
24
|
-
'test'
|
25
|
-
end
|
12
|
+
class TestRepo
|
13
|
+
include SmartIoC::Iocify
|
14
|
+
bean :repo, context: :test, package: :bean_factory
|
26
15
|
end
|
27
16
|
|
28
|
-
|
29
|
-
|
30
|
-
package :bean_factory
|
31
|
-
|
32
|
-
def call
|
33
|
-
'dao'
|
34
|
-
end
|
17
|
+
class DAO
|
18
|
+
include SmartIoC::Iocify
|
19
|
+
bean :dao, context: :default, package: :bean_factory
|
35
20
|
end
|
36
21
|
|
37
22
|
class TestObject
|
38
23
|
end
|
39
24
|
|
40
|
-
|
41
|
-
|
42
|
-
package
|
25
|
+
class Factory
|
26
|
+
include SmartIoC::Iocify
|
27
|
+
bean :factory, factory_method: :build_bean, package: :bean_factory
|
43
28
|
|
44
29
|
def build_bean
|
45
30
|
TestObject.new
|
@@ -71,17 +56,17 @@ describe SmartIoC::BeanFactory do
|
|
71
56
|
|
72
57
|
it 'returns proper bean for test context' do
|
73
58
|
SmartIoC.set_extra_context_for_package(:bean_factory, :test)
|
74
|
-
expect(SmartIoC.get_bean(:repo)
|
59
|
+
expect(SmartIoC.get_bean(:repo)).to be_a(TestRepo)
|
75
60
|
end
|
76
61
|
|
77
62
|
it 'returns proper bean for default context' do
|
78
63
|
SmartIoC.set_extra_context_for_package(:bean_factory, :default)
|
79
|
-
expect(SmartIoC.get_bean(:repo)
|
64
|
+
expect(SmartIoC.get_bean(:repo)).to be_a(Repo)
|
80
65
|
end
|
81
66
|
|
82
67
|
it 'returns proper bean for test context with fallback to default context' do
|
83
68
|
SmartIoC.set_extra_context_for_package(:bean_factory, :test)
|
84
|
-
expect(SmartIoC.get_bean(:dao)
|
69
|
+
expect(SmartIoC.get_bean(:dao)).to be_a(DAO)
|
85
70
|
end
|
86
71
|
|
87
72
|
it 'updates dependencies' do
|
@@ -169,7 +154,7 @@ describe SmartIoC::BeanFactory do
|
|
169
154
|
end
|
170
155
|
|
171
156
|
it 'injects prototype beans with different object id' do
|
172
|
-
prototype_bean = SmartIoC.get_bean(:prototype_bean
|
157
|
+
prototype_bean = SmartIoC.get_bean(:prototype_bean)
|
173
158
|
repo1_object_id = prototype_bean.prototype_service1.prototype_repo.object_id
|
174
159
|
repo2_object_id = prototype_bean.prototype_service2.prototype_repo.object_id
|
175
160
|
|
@@ -177,7 +162,7 @@ describe SmartIoC::BeanFactory do
|
|
177
162
|
end
|
178
163
|
|
179
164
|
it 'injects singleton beans with same object id' do
|
180
|
-
prototype_bean = SmartIoC.get_bean(:prototype_bean
|
165
|
+
prototype_bean = SmartIoC.get_bean(:prototype_bean)
|
181
166
|
repo1_object_id = prototype_bean.prototype_service1.singleton_repo.object_id
|
182
167
|
repo2_object_id = prototype_bean.prototype_service2.singleton_repo.object_id
|
183
168
|
|
@@ -13,8 +13,8 @@ describe SmartIoC::BeanLocator do
|
|
13
13
|
locations = SmartIoC::BeanLocations.get_bean_locations(:repository)
|
14
14
|
|
15
15
|
expect(locations[:test].size).to eq(3)
|
16
|
-
expect(locations[:test][0]).to match(/example\/admins\/repository\/
|
17
|
-
expect(locations[:test][1]).to match(/example\/admins\/repository\/admins_repository.rb/)
|
16
|
+
expect(locations[:test][0]).to match(/example\/admins\/repository\/admins_repository.rb/)
|
17
|
+
expect(locations[:test][1]).to match(/example\/admins\/repository\/test\/admins_repository.rb/)
|
18
18
|
expect(locations[:test][2]).to match(/example\/users\/repository\/users_repository.rb/)
|
19
19
|
}
|
20
20
|
|
@@ -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_by_class(BeanClass)
|
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).
|
21
|
+
uc2 = users_creator.send(:repository).users_creator
|
22
22
|
expect(users_creator).to eq(uc2)
|
23
23
|
end
|
24
24
|
end
|
@@ -27,16 +27,14 @@ 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(
|
31
|
-
expect(users_creator.send(:repository).methods).to include(:put)
|
32
|
-
expect(users_creator.send(:repository).methods).to include(:get)
|
30
|
+
expect(users_creator.send(:repository)).to be_a(AdminsRepository)
|
33
31
|
expect(users_creator.send(:logger)).to be_a(LoggerFactory::SmartIoCLogger)
|
34
32
|
end
|
35
33
|
|
36
34
|
it 'sets beans with extra package context' do
|
37
35
|
SmartIoC.set_extra_context_for_package(:admins, :test)
|
38
36
|
SmartIoC.force_clear_scopes
|
39
|
-
|
37
|
+
|
40
38
|
users_creator = @container.get_bean(:users_creator)
|
41
39
|
users_creator.create(1, 'test@test.com')
|
42
40
|
|
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.
|
4
|
+
version: 0.4.0
|
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: 2022-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,7 +73,6 @@ 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
|
77
76
|
- lib/smart_ioc/bean_definition.rb
|
78
77
|
- lib/smart_ioc/bean_definitions_storage.rb
|
79
78
|
- lib/smart_ioc/bean_dependency.rb
|
@@ -91,7 +90,6 @@ files:
|
|
91
90
|
- lib/smart_ioc/scopes/prototype.rb
|
92
91
|
- lib/smart_ioc/scopes/request.rb
|
93
92
|
- lib/smart_ioc/scopes/singleton.rb
|
94
|
-
- lib/smart_ioc/string_utils.rb
|
95
93
|
- lib/smart_ioc/version.rb
|
96
94
|
- smart_ioc.gemspec
|
97
95
|
- spec/smart_ioc/bean_definition_spec.rb
|
@@ -135,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
133
|
- !ruby/object:Gem::Version
|
136
134
|
version: '0'
|
137
135
|
requirements: []
|
138
|
-
rubygems_version: 3.
|
136
|
+
rubygems_version: 3.3.7
|
139
137
|
signing_key:
|
140
138
|
specification_version: 4
|
141
139
|
summary: Inversion of Control Container
|
data/lib/smart_ioc/bean.rb
DELETED
@@ -1,59 +0,0 @@
|
|
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
|
-
str = SmartIoC::StringUtils
|
13
|
-
file_path = caller[0].split(':').first
|
14
|
-
package = klass.instance_variable_get(:@package) || SmartIoC::BeanLocations.get_bean_package(file_path)
|
15
|
-
context = klass.instance_variable_get(:@context) || :default
|
16
|
-
|
17
|
-
if package.nil?
|
18
|
-
raise ArgumentError, "package is not defined for bean :#{bean_name}"
|
19
|
-
end
|
20
|
-
|
21
|
-
package_mod = str.camelize(package)
|
22
|
-
context_mod = str.camelize(context || :default)
|
23
|
-
|
24
|
-
class_name = str.camelize(bean_name)
|
25
|
-
klass_name = "#{package_mod}::#{context_mod}::#{class_name}"
|
26
|
-
|
27
|
-
eval(
|
28
|
-
%Q(
|
29
|
-
module #{package_mod}
|
30
|
-
module #{context_mod}
|
31
|
-
if constants.include?(:"#{class_name}")
|
32
|
-
remove_const :"#{class_name}"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
#{klass_name} = klass
|
38
|
-
)
|
39
|
-
)
|
40
|
-
|
41
|
-
klass.instance_exec do
|
42
|
-
bean(
|
43
|
-
bean_name,
|
44
|
-
file_path: file_path,
|
45
|
-
scope: instance_variable_get(:@scope),
|
46
|
-
package: package,
|
47
|
-
instance: instance_variable_get(:@instance) || false,
|
48
|
-
factory_method: instance_variable_get(:@factory_method),
|
49
|
-
context: context,
|
50
|
-
after_init: instance_variable_get(:@after_init),
|
51
|
-
)
|
52
|
-
end
|
53
|
-
|
54
|
-
(klass.instance_variable_get(:@injects) || []).each do |inject|
|
55
|
-
klass.register_inject(inject[:bean_name], ref: inject[:ref], from: inject[:from])
|
56
|
-
end
|
57
|
-
|
58
|
-
klass
|
59
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
module SmartIoC
|
2
|
-
class StringUtils
|
3
|
-
class << self
|
4
|
-
def camelize(term)
|
5
|
-
string = term.to_s
|
6
|
-
string = string.sub(/^[a-z\d]*/) { |match| match.capitalize }
|
7
|
-
string.gsub!(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }
|
8
|
-
string.gsub!("/".freeze, "::".freeze)
|
9
|
-
string
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|