smart_ioc 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: faf1a687610d2cc6e25623650bb6ff881938bd28
4
- data.tar.gz: 34208f444623db8654a9ff4501ef2c70b581aa5a
3
+ metadata.gz: cf89316b5fdc41e85e9cadcf0d5d4c0793aba48e
4
+ data.tar.gz: 52767b97eecaf1fba1141e3741139ba8871ecde5
5
5
  SHA512:
6
- metadata.gz: bdf1e74fcc8965d8ccd77c106cebca346200d49d9419b5530d3964c5048d76e6b6798da6cadd9ab656614c340bd7657cbbb96a141b425e1e77ff6a89331a0b8a
7
- data.tar.gz: 34933c3ad8765c8e9dd94fa6b80539a9c4b2c4227f1d344f413c0fec5fe55ae602db8e3e5017d87ebd9c7c07a846a0127833323e31b60ef082e36c9d4b3ea9eb
6
+ metadata.gz: 81d0b186ff1b8922d06add551b86376def059037bb49e890d1153986826125ff49ca2991103a63074a63daf96e85b31b680dc677c1943791ade889e7ad940909
7
+ data.tar.gz: ed5f32ac7c938c24100e7ab4c6560fd493117fc8798285aa7142004ec4afb07b03c9bac6c7a4dba168235c5d8d054866f67188d644fd88843e4114191850c4e4
data/Gemfile CHANGED
@@ -6,4 +6,5 @@ gemspec
6
6
  group :test do
7
7
  gem 'rspec'
8
8
  gem 'simplecov'
9
+ gem 'byebug'
9
10
  end
@@ -1,11 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smart_ioc (0.2.5)
4
+ smart_ioc (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ byebug (10.0.2)
9
10
  codecov (0.1.10)
10
11
  json
11
12
  simplecov
@@ -39,6 +40,7 @@ PLATFORMS
39
40
 
40
41
  DEPENDENCIES
41
42
  bundler (~> 1.3)
43
+ byebug
42
44
  codecov
43
45
  rake
44
46
  rspec
@@ -1,4 +1,5 @@
1
1
  require 'smart_ioc/version'
2
+ require 'benchmark'
2
3
 
3
4
  module SmartIoC
4
5
  autoload :Args, 'smart_ioc/args'
@@ -29,15 +30,32 @@ module SmartIoC
29
30
  require 'smart_ioc/railtie' if defined?(Rails)
30
31
 
31
32
  class << self
33
+ def is_benchmark_mode
34
+ @benchmark_mode
35
+ end
36
+
32
37
  # @param package_name [String or Symbol] package name for bean definitions
33
38
  # @param dir [String] absolute path with bean definitions
34
39
  # @return nil
35
40
  def find_package_beans(package_name, dir)
36
- bean_locator = SmartIoC::BeanLocator.new
37
- bean_locator.locate_beans(package_name.to_sym, dir)
41
+ time = Benchmark.realtime do
42
+ bean_locator = SmartIoC::BeanLocator.new
43
+ bean_locator.locate_beans(package_name.to_sym, dir)
44
+ end
45
+
46
+ time *= 1000
47
+
48
+ if is_benchmark_mode
49
+ puts "Search finished for '#{package_name}'. Time taken: #{"%.2f ms" % time}"
50
+ end
51
+
38
52
  nil
39
53
  end
40
54
 
55
+ def benchmark_mode(flag)
56
+ @benchmark_mode = !!flag
57
+ end
58
+
41
59
  # Load all beans (usually required for production env)
42
60
  def load_all_beans
43
61
  BeanLocations.all_bean_names.each do |bean|
@@ -2,9 +2,9 @@ class SmartIoC::BeanDefinition
2
2
  include SmartIoC::Args
3
3
 
4
4
  attr_reader :name, :package, :path, :klass, :scope, :instance, :factory_method,
5
- :context, :dependencies
5
+ :context, :dependencies, :after_init
6
6
 
7
- def initialize(name:, package:, path:, klass:, scope:, context:, instance:, factory_method:)
7
+ def initialize(name:, package:, path:, klass:, scope:, context:, instance:, factory_method:, after_init:)
8
8
  not_nil(name, :name)
9
9
  not_nil(package, :package)
10
10
  not_nil(path, :path)
@@ -20,6 +20,7 @@ class SmartIoC::BeanDefinition
20
20
  @scope = scope
21
21
  @instance = instance
22
22
  @factory_method = factory_method
23
+ @after_init = after_init
23
24
  @context = context
24
25
 
25
26
  @dependencies = []
@@ -49,6 +50,10 @@ class SmartIoC::BeanDefinition
49
50
  bean_definition.klass == @klass
50
51
  end
51
52
 
53
+ def singleton?
54
+ SmartIoC::Scopes::Singleton::VALUE == @scope
55
+ end
56
+
52
57
  def inspect
53
58
  str = []
54
59
  str << "class: #{@klass}"
@@ -1,5 +1,3 @@
1
- require 'thread'
2
-
3
1
  # Instantiates beans according to their scopes
4
2
  class SmartIoC::BeanFactory
5
3
  include SmartIoC::Errors
@@ -37,105 +35,39 @@ class SmartIoC::BeanFactory
37
35
  check_arg(package, :package, Symbol) if package
38
36
  check_arg(context, :context, Symbol) if context
39
37
 
40
- @semaphore.synchronize do
41
- result = get_or_build_bean(bean_name, package, context)
42
- end
43
- end
44
-
45
- private
46
-
47
- def get_or_build_bean(bean_name, package, context, history = Set.new)
48
38
  @bean_file_loader.require_bean(bean_name)
49
39
 
50
- context = autodetect_context(bean_name, package, context)
40
+ context = autodetect_context(bean_name, package, context)
51
41
  bean_definition = @bean_definitions_storage.find(bean_name, package, context)
52
- scope = get_scope(bean_definition)
53
- scope_bean = scope.get_bean(bean_definition.klass)
54
- is_recursive = history.include?(bean_name)
55
-
56
- history << bean_name
57
-
58
- if scope_bean && scope_bean.loaded
59
- update_dependencies(scope_bean.bean, bean_definition)
60
- scope_bean.bean
61
- else
62
- if is_recursive
63
- raise LoadRecursion.new(bean_definition)
64
- end
65
-
66
- beans_cache = init_bean_definition_cache(bean_definition)
42
+ scope = get_scope(bean_definition)
43
+ bean = scope.get_bean(bean_definition.klass)
67
44
 
68
- autodetect_bean_definitions_for_dependencies(bean_definition)
69
- preload_beans(bean_definition, beans_cache[bean_definition])
70
- load_bean(bean_definition, beans_cache)
45
+ if !bean
46
+ bean = init_bean(bean_definition)
71
47
  end
72
- end
73
48
 
74
- def load_bean(bean_definition, beans_cache)
75
- bd_opts = beans_cache[bean_definition]
76
- scope_bean = bd_opts[:scope_bean]
49
+ scope.save_bean(bean_definition.klass, bean)
50
+ bean
51
+ end
77
52
 
78
- bean_definition.dependencies.each do |dependency|
79
- bd = dependency.bean_definition
80
- dep_db_opts = bd_opts[:dependencies][dependency.bean_definition]
81
- dep_scope_bean = dep_db_opts[:scope_bean]
82
- dep_bean = load_bean(bd, bd_opts[:dependencies])
53
+ private
83
54
 
84
- scope_bean.bean.instance_variable_set(:"@#{dependency.bean}", dep_bean)
55
+ def init_bean(bean_definition)
56
+ bean = if bean_definition.is_instance?
57
+ bean_definition.klass.allocate
58
+ else
59
+ bean_definition.klass
85
60
  end
86
61
 
87
- if !scope_bean.loaded
88
- scope_bean.set_bean(scope_bean.bean.send(bean_definition.factory_method), true)
62
+ if bean_definition.has_factory_method?
63
+ bean = bean.send(bean_definition.factory_method)
89
64
  end
90
65
 
91
- scope_bean.bean
92
- end
93
-
94
- def inject_beans(bean_definition, beans_cache)
95
- bean = beans_cache[:scope_bean].bean
96
- bean_definition.dependencies.each do |dependency|
97
- bd = dependency.bean_definition
98
- dep_bean = beans_cache[:dependencies][bd][:scope_bean].bean
99
- bean.instance_variable_set(:"@#{dependency.bean}", dep_bean)
100
- inject_beans(bd, beans_cache[:dependencies][bd])
66
+ if bean_definition.after_init
67
+ bean.send(bean_definition.after_init)
101
68
  end
102
- end
103
-
104
- def init_bean_definition_cache(bean_definition)
105
- {
106
- bean_definition => {
107
- scope_bean: nil,
108
- dependencies: {
109
- }
110
- }
111
- }
112
- end
113
-
114
- def update_dependencies(bean, bean_definition, updated_beans = {})
115
- bean_definition.dependencies.each do |dependency|
116
- bd = autodetect_bean_definition(
117
- dependency.ref, dependency.package, bean_definition.package
118
- )
119
-
120
- scope = get_scope(bean_definition)
121
- dep_bean = updated_beans[bd]
122
-
123
- if !dep_bean && scope_bean = scope.get_bean(bd.klass)
124
- dep_bean = scope_bean.bean
125
- end
126
-
127
- if !dep_bean
128
- dep_bean = get_or_build_bean(bd.name, bd.package, bd.context)
129
69
 
130
- bean.instance_variable_set(:"@#{dependency.bean}", dep_bean)
131
-
132
- if !scope.is_a?(SmartIoC::Scopes::Prototype)
133
- updated_beans[bd] = dep_bean
134
- end
135
- else
136
- update_dependencies(dep_bean, bd, updated_beans)
137
- end
138
- end
70
+ bean
139
71
  end
140
72
 
141
73
  def autodetect_context(bean_name, package, context)
@@ -149,20 +81,6 @@ class SmartIoC::BeanFactory
149
81
  end
150
82
  end
151
83
 
152
- def autodetect_bean_definitions_for_dependencies(bean_definition)
153
- bean_definition.dependencies.each do |dependency|
154
- next if dependency.bean_definition
155
-
156
- @bean_file_loader.require_bean(dependency.ref)
157
-
158
- dependency.bean_definition = autodetect_bean_definition(
159
- dependency.ref, dependency.package, bean_definition.package
160
- )
161
-
162
- autodetect_bean_definitions_for_dependencies(dependency.bean_definition)
163
- end
164
- end
165
-
166
84
  def autodetect_bean_definition(bean, package, parent_bean_package)
167
85
  if package
168
86
  bean_context = @extra_package_contexts.get_context(package)
@@ -196,7 +114,13 @@ class SmartIoC::BeanFactory
196
114
  end
197
115
 
198
116
  if smart_bds.size > 1
199
- raise ArgumentError, "Unable to autodetect bean :#{bean}.\nSeveral definitions were found.\n#{smart_bds.map(&:inspect).join("\n\n")}. Set package directly for injected dependency"
117
+ raise ArgumentError.new(
118
+ %Q(Unable to autodetect bean :#{bean}.
119
+ Several definitions were found:\n
120
+ #{smart_bds.map(&:inspect).join("\n\n")}.
121
+ Set package directly for injected dependency
122
+ )
123
+ )
200
124
  end
201
125
 
202
126
  if smart_bds.size == 0
@@ -206,77 +130,6 @@ class SmartIoC::BeanFactory
206
130
  return smart_bds.first
207
131
  end
208
132
 
209
- def preload_beans(bean_definition, beans_cache)
210
- scope = get_scope(bean_definition)
211
-
212
- if scope_bean = scope.get_bean(bean_definition.klass)
213
- beans_cache[:scope_bean] = scope_bean
214
- else
215
- preload_bean_instance(bean_definition, beans_cache)
216
- end
217
-
218
- bean_definition.dependencies.each do |dependency|
219
- bd = dependency.bean_definition
220
-
221
- next if beans_cache[:dependencies].has_key?(bd)
222
-
223
- dep_bean_cache = init_bean_definition_cache(bd)
224
- beans_cache[:dependencies].merge!(dep_bean_cache)
225
- preload_beans(bd, dep_bean_cache[bd])
226
- end
227
- end
228
-
229
- def preload_bean_instance(bean_definition, beans_cache)
230
- return if beans_cache[:scope_bean]
231
-
232
- scope = get_scope(bean_definition)
233
- scope_bean = scope.get_bean(bean_definition.klass)
234
-
235
- if scope_bean
236
- beans_cache[:scope_bean] = scope_bean
237
- return scope_bean
238
- end
239
-
240
- bean = if bean_definition.is_instance?
241
- bean_definition.klass.allocate
242
- else
243
- bean_definition.klass
244
- end
245
-
246
- scope_bean = SmartIoC::Scopes::Bean.new(bean, !bean_definition.has_factory_method?)
247
-
248
- scope.save_bean(bean_definition.klass, scope_bean)
249
- beans_cache[:scope_bean] = scope_bean
250
-
251
- scope_bean
252
- end
253
-
254
- def init_factory_bean(bean_definition, bd_opts)
255
- scope_bean = bd_opts[:scope_bean]
256
-
257
- if !scope_bean.loaded
258
- scope_bean.set_bean(scope_bean.bean.send(bean_definition.factory_method), true)
259
- end
260
- end
261
-
262
- def get_cross_refference(refer_bean_definitions, current_bean_definition, seen_bean_definitions = [])
263
- current_bean_definition.dependencies.each do |dependency|
264
- bd = dependency.bean_definition
265
-
266
- next if seen_bean_definitions.include?(bd)
267
-
268
- if refer_bean_definitions.include?(bd)
269
- return bd
270
- end
271
-
272
- if crbd = get_cross_refference(refer_bean_definitions, bd, seen_bean_definitions + [bd])
273
- return crbd
274
- end
275
- end
276
-
277
- nil
278
- end
279
-
280
133
  def all_scopes
281
134
  [@singleton_scope, @prototype_scope, @thread_scope]
282
135
  end
@@ -14,7 +14,7 @@ module SmartIoC
14
14
  @container = nil
15
15
  end
16
16
 
17
- def get_bean(bean_name, package: nil, context: nil)
17
+ def get_bean(bean_name, package = nil, context = nil)
18
18
  get_instance.get_bean(bean_name, package: package, context: context)
19
19
  end
20
20
  end
@@ -36,8 +36,9 @@ module SmartIoC
36
36
  # @param path [String] bean file absolute path
37
37
  # @param scope [Symbol] scope value
38
38
  # @param context [Symbol] bean context
39
+ # @param after_init [Symbol] name of bean method that will be called after bean initialization
39
40
  # @return [SmartIoC::BeanDefinition] bean definition
40
- def register_bean(bean_name:, klass:, context:, scope:, path:,
41
+ def register_bean(bean_name:, klass:, context:, scope:, path:, after_init:,
41
42
  factory_method: nil, package_name: nil, instance: true)
42
43
  context ||= DEFAULT_CONTEXT
43
44
 
@@ -81,7 +82,8 @@ module SmartIoC
81
82
  instance: instance,
82
83
  factory_method: factory_method,
83
84
  context: context,
84
- scope: scope
85
+ scope: scope,
86
+ after_init: after_init,
85
87
  )
86
88
 
87
89
  bean_definitions_storage.push(bean_definition)
@@ -136,9 +138,9 @@ module SmartIoC
136
138
  def require_bean(bean_name)
137
139
  bean_factory.bean_file_loader.require_bean(bean_name)
138
140
  end
139
-
141
+
140
142
  private
141
-
143
+
142
144
  def bean_factory
143
145
  @bean_factory ||= SmartIoC::BeanFactory.new(bean_definitions_storage, extra_package_contexts)
144
146
  end
@@ -5,16 +5,6 @@ module SmartIoC::Errors
5
5
  end
6
6
  end
7
7
 
8
- class LoadRecursion < StandardError
9
- def initialize(bean_definition)
10
- super(%Q(
11
- Unable to create bean :#{bean_definitions.name}.
12
- Recursion found during bean load.
13
- #{bean_definition.inspect}
14
- ))
15
- end
16
- end
17
-
18
8
  class AmbiguousBeanDefinition < StandardError
19
9
  def initialize(bean_name, bean_definitions)
20
10
  super(%Q(
@@ -29,8 +29,9 @@ module SmartIoC::Iocify
29
29
  # @param factory_method [nil or Symbol] factory method to get bean
30
30
  # @param instance [Boolean] instance based bean or class-based
31
31
  # @param context [Symbol] set bean context (ex: :test)
32
+ # @param after_init [Symbol] name of bean method that will be called after bean initialization (ex: :test)
32
33
  # @return nil
33
- def bean(bean_name, scope: nil, package: nil, instance: true, factory_method: nil, context: nil)
34
+ def bean(bean_name, scope: nil, package: nil, instance: true, factory_method: nil, context: nil, after_init: nil)
34
35
  file_path = caller[0].split(':').first
35
36
 
36
37
  bean_definition = SmartIoC.get_bean_definition_by_class(self)
@@ -55,7 +56,8 @@ module SmartIoC::Iocify
55
56
  package_name: package,
56
57
  instance: instance,
57
58
  factory_method: factory_method,
58
- context: context
59
+ context: context,
60
+ after_init: after_init,
59
61
  )
60
62
 
61
63
  if bean_definition.is_instance?
@@ -90,16 +92,21 @@ module SmartIoC::Iocify
90
92
  package: from
91
93
  )
92
94
 
95
+ bean_method = Proc.new do
96
+ bean = instance_variable_get(:"@#{bean_name}")
97
+ return bean if bean
98
+ instance_variable_set(:"@#{bean_name}", SmartIoC::Container.get_bean(ref || bean_name, from))
99
+ end
100
+
93
101
  if bean_definition.is_instance?
94
- class_eval %Q(
95
- private
96
- attr_reader :#{bean_name}
97
- )
102
+ define_method bean_name, &bean_method
103
+ private bean_name
98
104
  else
105
+ define_singleton_method bean_name, &bean_method
106
+
99
107
  class_eval %Q(
100
108
  class << self
101
- private
102
- attr_reader :#{bean_name}
109
+ private :#{bean_name}
103
110
  end
104
111
  )
105
112
  end
@@ -1,3 +1,3 @@
1
1
  module SmartIoC
2
- VERSION = "0.2.5"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -4,14 +4,15 @@ describe SmartIoC::BeanDefinition do
4
4
  describe "::inspect" do
5
5
  it {
6
6
  bd = SmartIoC::BeanDefinition.new(
7
- name: :test_bean,
8
- package: :test_package,
9
- path: 'current_dir',
10
- klass: Object,
11
- scope: :singleton,
12
- context: :default,
13
- instance: false,
14
- factory_method: nil
7
+ name: :test_bean,
8
+ package: :test_package,
9
+ path: 'current_dir',
10
+ klass: Object,
11
+ scope: :singleton,
12
+ context: :default,
13
+ instance: false,
14
+ factory_method: nil,
15
+ after_init: nil,
15
16
  )
16
17
 
17
18
  str =
@@ -32,6 +32,15 @@ describe SmartIoC::BeanFactory do
32
32
  end
33
33
  end
34
34
 
35
+ it 'returns benchmark time to load bean' do
36
+ SmartIoC.benchmark_mode(true)
37
+
38
+ SmartIoC.set_extra_context_for_package(:bean_factory, :test)
39
+ SmartIoC.get_bean(:repo)
40
+
41
+ SmartIoC.benchmark_mode(false)
42
+ end
43
+
35
44
  it 'returns same instance for singleton scope' do
36
45
  SmartIoC.set_extra_context_for_package(:bean_factory, :test)
37
46
  instance1 = SmartIoC.get_bean(:repo)
@@ -67,7 +76,7 @@ describe SmartIoC::BeanFactory do
67
76
 
68
77
  inject :prototype_bean
69
78
 
70
- attr_reader :prototype_bean
79
+ public :prototype_bean
71
80
  end
72
81
 
73
82
  class SecondSingletonBean
@@ -81,7 +90,7 @@ describe SmartIoC::BeanFactory do
81
90
 
82
91
  inject :second_singleton_bean
83
92
 
84
- attr_reader :second_singleton_bean
93
+ public :second_singleton_bean
85
94
  end
86
95
 
87
96
  bean1 = SmartIoC.get_bean(:singleton_bean, package: :test)
@@ -95,7 +104,7 @@ describe SmartIoC::BeanFactory do
95
104
  second_singleton_bean2_object_id = bean2.prototype_bean.second_singleton_bean.object_id
96
105
 
97
106
  expect(bean1_object_id).to eq(bean2_object_id)
98
- expect(prototype_bean1_object_id).not_to eq(prototype_bean2_object_id)
107
+ expect(prototype_bean1_object_id).to eq(prototype_bean2_object_id)
99
108
  expect(second_singleton_bean1_object_id).to eq(second_singleton_bean2_object_id)
100
109
  end
101
110
 
@@ -108,7 +117,7 @@ describe SmartIoC::BeanFactory do
108
117
  inject :prototype_service1
109
118
  inject :prototype_service2
110
119
 
111
- attr_reader :prototype_service1, :prototype_service2
120
+ public :prototype_service1, :prototype_service2
112
121
  end
113
122
 
114
123
  class PrototypeService1
@@ -118,7 +127,7 @@ describe SmartIoC::BeanFactory do
118
127
  inject :prototype_repo
119
128
  inject :singleton_repo
120
129
 
121
- attr_reader :prototype_repo, :singleton_repo
130
+ public :prototype_repo, :singleton_repo
122
131
  end
123
132
 
124
133
  class PrototypeService2
@@ -128,16 +137,18 @@ describe SmartIoC::BeanFactory do
128
137
  inject :prototype_repo
129
138
  inject :singleton_repo
130
139
 
131
- attr_reader :prototype_repo, :singleton_repo
140
+ public :prototype_repo, :singleton_repo
132
141
  end
133
142
 
134
143
  class PrototypeRepo
135
144
  include SmartIoC::Iocify
145
+
136
146
  bean :prototype_repo, scope: :prototype, package: :prototype
137
147
  end
138
148
 
139
149
  class SingletonRepo
140
150
  include SmartIoC::Iocify
151
+
141
152
  bean :singleton_repo, scope: :singleton, package: :prototype
142
153
  end
143
154
  end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe SmartIoC do
4
+ it {
5
+ SmartIoC.clear
6
+ SmartIoC.benchmark_mode(true)
7
+
8
+ dir_path = File.join(File.expand_path(File.dirname(__FILE__)), 'example/admins')
9
+ SmartIoC.find_package_beans(:admins, dir_path)
10
+
11
+ SmartIoC.benchmark_mode(false)
12
+ }
13
+ end
@@ -1,20 +1,22 @@
1
- $data ||= {}
2
-
3
1
  class AdminsDAO
4
2
  include SmartIoC::Iocify
5
3
 
6
- bean :dao, instance: false
4
+ bean :dao, instance: false, after_init: :setup
7
5
 
8
6
  inject :config
9
7
 
10
8
  class << self
9
+ def setup
10
+ @data = {}
11
+ end
12
+
11
13
  def insert(entity)
12
14
  config.app_name
13
- $data[entity.id] = entity
15
+ @data[entity.id] = entity
14
16
  end
15
17
 
16
18
  def get(id)
17
- $data[id]
19
+ @data[id]
18
20
  end
19
21
  end
20
22
  end
@@ -3,7 +3,10 @@ class AdminsRepository
3
3
 
4
4
  bean :repository
5
5
 
6
- inject :dao
6
+ inject :dao, from: :admins
7
+ inject :users_creator
8
+
9
+ public :users_creator
7
10
 
8
11
  def put(user)
9
12
  dao.insert(user)
@@ -1,11 +1,13 @@
1
1
  class UsersDAO
2
2
  include SmartIoC::Iocify
3
3
 
4
- bean :dao, instance: false
5
-
6
- @data = {}
4
+ bean :dao, instance: false, after_init: :setup
7
5
 
8
6
  class << self
7
+ def setup
8
+ @data = {}
9
+ end
10
+
9
11
  def insert(entity)
10
12
  @data[entity.id] = entity
11
13
  end
@@ -6,6 +6,8 @@ class UsersRepository
6
6
  inject :users_creator # just for testing purposes (circular load check)
7
7
  inject :dao
8
8
 
9
+ public :users_creator
10
+
9
11
  def put(user)
10
12
  dao.insert(user)
11
13
  end
@@ -1,9 +1,13 @@
1
1
  class Config
2
2
  include SmartIoC::Iocify
3
3
 
4
- bean :config, factory_method: :get_config
4
+ bean :config, factory_method: :get_config, after_init: :setup
5
5
 
6
6
  class TestConfig
7
+ def setup
8
+ # do nothing; only for testing purposes
9
+ end
10
+
7
11
  def app_name
8
12
  'SmartIoC'
9
13
  end
@@ -9,7 +9,7 @@ describe 'Factory Method' do
9
9
 
10
10
  inject :test_config
11
11
 
12
- attr_reader :test_config
12
+ public :test_config
13
13
  end
14
14
 
15
15
 
@@ -20,7 +20,7 @@ describe 'Factory Method' do
20
20
 
21
21
  inject :test_config
22
22
 
23
- attr_reader :test_config
23
+ public :test_config
24
24
  end
25
25
 
26
26
  class TestConfig
@@ -52,7 +52,7 @@ describe 'Factory Method' do
52
52
  before :all do
53
53
  class SingletonBean
54
54
  include SmartIoC::Iocify
55
-
55
+
56
56
  bean :singleton_bean, package: :cross_refference
57
57
  end
58
58
 
@@ -61,10 +61,10 @@ describe 'Factory Method' do
61
61
 
62
62
  bean :other_singleton_bean, package: :cross_refference
63
63
  end
64
-
64
+
65
65
  class FactoryConfig
66
66
  include SmartIoC::Iocify
67
-
67
+
68
68
  bean :factory_config, factory_method: :build, package: :cross_refference
69
69
 
70
70
  inject :singleton_bean
@@ -85,7 +85,7 @@ describe 'Factory Method' do
85
85
 
86
86
  class FactoryLogger
87
87
  include SmartIoC::Iocify
88
-
88
+
89
89
  bean :factory_logger, factory_method: :build, package: :cross_refference
90
90
 
91
91
  inject :factory_config
@@ -99,7 +99,7 @@ describe 'Factory Method' do
99
99
  @singleton_bean = singleton_bean
100
100
  end
101
101
  end
102
-
102
+
103
103
  def build
104
104
  Logger.new(factory_config, singleton_bean)
105
105
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe SmartIoC::Container do
4
+ before :all do
5
+ SmartIoC.clear
6
+
7
+ dir_path = File.join(File.expand_path(File.dirname(__FILE__)), 'example/admins')
8
+ SmartIoC.find_package_beans(:admins, dir_path)
9
+
10
+ dir_path = File.join(File.expand_path(File.dirname(__FILE__)), 'example/utils')
11
+ SmartIoC.find_package_beans(:utils, dir_path)
12
+
13
+ dir_path = File.join(File.expand_path(File.dirname(__FILE__)), 'example/users')
14
+ SmartIoC.find_package_beans(:users, dir_path)
15
+
16
+ @container = SmartIoC.container
17
+ end
18
+
19
+ it 'loads recursive beans' do
20
+ users_creator = @container.get_bean(:users_creator)
21
+ uc2 = users_creator.send(:repository).users_creator
22
+ expect(users_creator).to eq(uc2)
23
+ end
24
+ end
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.2.5
4
+ version: 0.3.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: 2018-03-25 00:00:00.000000000 Z
11
+ date: 2018-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -87,7 +87,6 @@ files:
87
87
  - lib/smart_ioc/iocify.rb
88
88
  - lib/smart_ioc/railtie.rb
89
89
  - lib/smart_ioc/scopes.rb
90
- - lib/smart_ioc/scopes/bean.rb
91
90
  - lib/smart_ioc/scopes/prototype.rb
92
91
  - lib/smart_ioc/scopes/request.rb
93
92
  - lib/smart_ioc/scopes/singleton.rb
@@ -98,6 +97,7 @@ files:
98
97
  - spec/smart_ioc/bean_file_loader_spec.rb
99
98
  - spec/smart_ioc/bean_locations_spec.rb
100
99
  - spec/smart_ioc/bean_locator_spec.rb
100
+ - spec/smart_ioc/benchmark_mode_spec.rb
101
101
  - spec/smart_ioc/container_spec.rb
102
102
  - spec/smart_ioc/example/admins/repository/admins_dao.rb
103
103
  - spec/smart_ioc/example/admins/repository/admins_repository.rb
@@ -111,6 +111,7 @@ files:
111
111
  - spec/smart_ioc/factory_method_spec.rb
112
112
  - spec/smart_ioc/iocify_spec.rb
113
113
  - spec/smart_ioc/object_spec.rb
114
+ - spec/smart_ioc/recursive_spec.rb
114
115
  - spec/smart_ioc/smart_ioc_spec.rb
115
116
  - spec/spec_helper.rb
116
117
  homepage: http://github.com/droidlabs/smart_ioc
@@ -143,6 +144,7 @@ test_files:
143
144
  - spec/smart_ioc/bean_file_loader_spec.rb
144
145
  - spec/smart_ioc/bean_locations_spec.rb
145
146
  - spec/smart_ioc/bean_locator_spec.rb
147
+ - spec/smart_ioc/benchmark_mode_spec.rb
146
148
  - spec/smart_ioc/container_spec.rb
147
149
  - spec/smart_ioc/example/admins/repository/admins_dao.rb
148
150
  - spec/smart_ioc/example/admins/repository/admins_repository.rb
@@ -156,5 +158,6 @@ test_files:
156
158
  - spec/smart_ioc/factory_method_spec.rb
157
159
  - spec/smart_ioc/iocify_spec.rb
158
160
  - spec/smart_ioc/object_spec.rb
161
+ - spec/smart_ioc/recursive_spec.rb
159
162
  - spec/smart_ioc/smart_ioc_spec.rb
160
163
  - spec/spec_helper.rb
@@ -1,13 +0,0 @@
1
- class SmartIoC::Scopes::Bean
2
- attr_reader :bean, :loaded
3
-
4
- def initialize(bean, loaded)
5
- @bean = bean
6
- @loaded = loaded
7
- end
8
-
9
- def set_bean(bean, loaded)
10
- @bean = bean
11
- @loaded = loaded
12
- end
13
- end