ioc_rb 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/ioc_rb/bean_factory.rb +25 -10
- data/lib/ioc_rb/container.rb +13 -0
- data/lib/ioc_rb/scopes/prototype_scope.rb +8 -0
- data/lib/ioc_rb/scopes/request_scope.rb +6 -0
- data/lib/ioc_rb/scopes/singleton_scope.rb +6 -0
- data/lib/ioc_rb/version.rb +1 -1
- data/spec/ioc_rb/container_spec.rb +11 -0
- 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: 85f0f26c7d82aeb28d8f04d99d0c52ebdeed61b5
|
4
|
+
data.tar.gz: 12d2af5706bf986c5c4b01c4fc6fd0a7c8910c9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e6ada01c013048e28219bcf6b2aa06adb2e9a31790263eced54e6b8a818fb3e74729df5553c7d2df2ab48408a7b0a55d325a67f783c674fda8bce5c6b279876d
|
7
|
+
data.tar.gz: 36cdcc328ddd3e1c34a8fd2e377ccef29696924df2436269b40849f5cae88dae80d4c5c50084813217db9478ed0cce39aa502a61648ec13e447c5c453326bb26
|
data/Gemfile.lock
CHANGED
data/lib/ioc_rb/bean_factory.rb
CHANGED
@@ -35,16 +35,7 @@ class IocRb::BeanFactory
|
|
35
35
|
# @param [BeanMetadata] bean metadata
|
36
36
|
# @return bean instance
|
37
37
|
def get_bean_with_metadata(bean_metadata)
|
38
|
-
|
39
|
-
when :singleton
|
40
|
-
@singleton_scope.get_bean(bean_metadata)
|
41
|
-
when :prototype
|
42
|
-
@prototype_scope.get_bean(bean_metadata)
|
43
|
-
when :request
|
44
|
-
@request_scope.get_bean(bean_metadata)
|
45
|
-
else
|
46
|
-
raise IocRb::Errors::UnsupportedScopeError, "Bean with name :#{bean_metadata.name} has unsupported scope :#{bean_metadata.scope}"
|
47
|
-
end
|
38
|
+
get_scope_by_metadata(bean_metadata).get_bean(bean_metadata)
|
48
39
|
end
|
49
40
|
|
50
41
|
# Create new bean instance according
|
@@ -73,6 +64,17 @@ class IocRb::BeanFactory
|
|
73
64
|
bean
|
74
65
|
end
|
75
66
|
|
67
|
+
# Delete bean from the container by it's +name+.
|
68
|
+
# @param [Symbol] bean name
|
69
|
+
# @raise MissingBeanError if bean with the specified name is not found
|
70
|
+
def delete_bean(name)
|
71
|
+
bean_metadata = @beans_metadata_storage.by_name(name)
|
72
|
+
unless bean_metadata
|
73
|
+
raise IocRb::Errors::MissingBeanError, "Bean with name :#{name} is not defined"
|
74
|
+
end
|
75
|
+
get_scope_by_metadata(bean_metadata).delete_bean(bean_metadata)
|
76
|
+
end
|
77
|
+
|
76
78
|
private
|
77
79
|
|
78
80
|
def set_bean_dependencies(bean, bean_metadata)
|
@@ -98,4 +100,17 @@ class IocRb::BeanFactory
|
|
98
100
|
end
|
99
101
|
end
|
100
102
|
|
103
|
+
def get_scope_by_metadata(bean_metadata)
|
104
|
+
case bean_metadata.scope
|
105
|
+
when :singleton
|
106
|
+
@singleton_scope
|
107
|
+
when :prototype
|
108
|
+
@prototype_scope
|
109
|
+
when :request
|
110
|
+
@request_scope
|
111
|
+
else
|
112
|
+
raise IocRb::Errors::UnsupportedScopeError, "Bean with name :#{bean_metadata.name} has unsupported scope :#{bean_metadata.scope}"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
101
116
|
end
|
data/lib/ioc_rb/container.rb
CHANGED
@@ -50,6 +50,19 @@ module IocRb
|
|
50
50
|
@beans_metadata_storage.put(bean)
|
51
51
|
end
|
52
52
|
|
53
|
+
# Registers new bean in container and replace existing instance if it's instantiated
|
54
|
+
# @param bean_name [Symbol] bean name
|
55
|
+
# @param options [Hash] includes bean class and bean scope
|
56
|
+
# @param &block [Proc] the block which describes bean dependencies,
|
57
|
+
# see more in the BeanMetadata
|
58
|
+
def replace_bean(bean_name, options, &block)
|
59
|
+
bean(bean_name, options, &block)
|
60
|
+
|
61
|
+
if @bean_factory.get_bean(bean_name)
|
62
|
+
@bean_factory.delete_bean(bean_name)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
53
66
|
# Returns bean instance from the container
|
54
67
|
# by the specified bean name
|
55
68
|
# @param name [Symbol] bean name
|
@@ -14,4 +14,12 @@ class IocRb::Scopes::PrototypeScope
|
|
14
14
|
def get_bean(bean_metadata)
|
15
15
|
@bean_factory.create_bean_and_save(bean_metadata, {})
|
16
16
|
end
|
17
|
+
|
18
|
+
# Delete bean from scope,
|
19
|
+
# because Prototype scope doesn't store bean
|
20
|
+
# then do nothing here
|
21
|
+
#
|
22
|
+
# @param bean_metadata [BeanMetadata] bean metadata
|
23
|
+
def delete_bean(bean_metadata)
|
24
|
+
end
|
17
25
|
end
|
@@ -24,4 +24,10 @@ class IocRb::Scopes::RequestScope
|
|
24
24
|
@bean_factory.create_bean_and_save(bean_metadata, RequestStore.store[:_iocrb_beans])
|
25
25
|
end
|
26
26
|
end
|
27
|
+
|
28
|
+
# Delete bean from scope
|
29
|
+
# @param bean_metadata [BeanMetadata] bean metadata
|
30
|
+
def delete_bean(bean_metadata)
|
31
|
+
RequestStore.store[:_iocrb_beans].delete(bean_metadata.name)
|
32
|
+
end
|
27
33
|
end
|
@@ -20,4 +20,10 @@ class IocRb::Scopes::SingletonScope
|
|
20
20
|
@bean_factory.create_bean_and_save(bean_metadata, @beans)
|
21
21
|
end
|
22
22
|
end
|
23
|
+
|
24
|
+
# Delete bean from scope
|
25
|
+
# @param bean_metadata [BeanMetadata] bean metadata
|
26
|
+
def delete_bean(bean_metadata)
|
27
|
+
@beans.delete(bean_metadata.name)
|
28
|
+
end
|
23
29
|
end
|
data/lib/ioc_rb/version.rb
CHANGED
@@ -33,6 +33,17 @@ describe IocRb::Container do
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
describe "#replace_bean" do
|
37
|
+
it "should replace bean definition" do
|
38
|
+
container = IocRb::Container.new
|
39
|
+
container.bean(:appender, class: Appender)
|
40
|
+
container[:appender].should be_a(Appender)
|
41
|
+
|
42
|
+
container.replace_bean(:appender, class: Logger)
|
43
|
+
container[:appender].should be_a(Logger)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
36
47
|
describe "passing bean definitions to container constructor" do
|
37
48
|
let(:resource) do
|
38
49
|
Proc.new do |c|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ioc_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Albert Gazizov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: request_store
|