ioc_rb 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/lib/ioc_rb/bean_factory.rb +15 -3
- data/lib/ioc_rb/bean_metadata.rb +11 -6
- data/lib/ioc_rb/version.rb +1 -1
- data/spec/ioc_rb/container_spec.rb +24 -0
- metadata +20 -15
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/lib/ioc_rb/bean_factory.rb
CHANGED
@@ -60,9 +60,22 @@ class IocRb::BeanFactory
|
|
60
60
|
end
|
61
61
|
bean = bean_metadata.instance ? bean_class.new : bean_class
|
62
62
|
|
63
|
-
|
64
|
-
|
63
|
+
if bean_metadata.has_factory_method?
|
64
|
+
set_bean_dependencies(bean, bean_metadata)
|
65
|
+
bean = bean.send(bean_metadata.factory_method)
|
66
|
+
beans_storage[bean_metadata.name] = bean
|
67
|
+
else
|
68
|
+
# put to container first to prevent circular dependencies
|
69
|
+
beans_storage[bean_metadata.name] = bean
|
70
|
+
set_bean_dependencies(bean, bean_metadata)
|
71
|
+
end
|
65
72
|
|
73
|
+
bean
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def set_bean_dependencies(bean, bean_metadata)
|
66
79
|
bean_metadata.attrs.each do |attr|
|
67
80
|
bean_metadata = @beans_metadata_storage.by_name(attr.ref)
|
68
81
|
unless bean_metadata
|
@@ -83,7 +96,6 @@ class IocRb::BeanFactory
|
|
83
96
|
end
|
84
97
|
end
|
85
98
|
end
|
86
|
-
bean
|
87
99
|
end
|
88
100
|
|
89
101
|
end
|
data/lib/ioc_rb/bean_metadata.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Stores bean specific data: bean class, name,
|
2
2
|
# scope and bean dependencies
|
3
3
|
class IocRb::BeanMetadata
|
4
|
-
attr_reader :name, :bean_class, :scope, :instance, :attrs
|
4
|
+
attr_reader :name, :bean_class, :scope, :instance, :factory_method, :attrs
|
5
5
|
|
6
6
|
# Constructor
|
7
7
|
# @param name [Symbol] bean name
|
@@ -17,11 +17,12 @@ class IocRb::BeanMetadata
|
|
17
17
|
def initialize(name, options, &block)
|
18
18
|
IocRb::ArgsValidator.has_key!(options, :class)
|
19
19
|
|
20
|
-
@name
|
21
|
-
@bean_class
|
22
|
-
@scope
|
23
|
-
@instance
|
24
|
-
@
|
20
|
+
@name = name
|
21
|
+
@bean_class = options[:class]
|
22
|
+
@scope = options[:scope] || :singleton
|
23
|
+
@instance = options[:instance].nil? ? true : options[:instance]
|
24
|
+
@factory_method = options[:factory_method]
|
25
|
+
@attrs = []
|
25
26
|
|
26
27
|
if @bean_class.respond_to?(:_iocrb_injectable_attrs)
|
27
28
|
@bean_class._iocrb_injectable_attrs.each do |attr, options|
|
@@ -35,6 +36,10 @@ class IocRb::BeanMetadata
|
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
39
|
+
def has_factory_method?
|
40
|
+
!!@factory_method
|
41
|
+
end
|
42
|
+
|
38
43
|
class Attribute
|
39
44
|
attr_reader :name, :ref
|
40
45
|
|
data/lib/ioc_rb/version.rb
CHANGED
@@ -119,4 +119,28 @@ describe IocRb::Container do
|
|
119
119
|
first_validator.should_not == second_validator
|
120
120
|
end
|
121
121
|
end
|
122
|
+
|
123
|
+
describe "factory method" do
|
124
|
+
module Test
|
125
|
+
class Config
|
126
|
+
end
|
127
|
+
class ConfigsFactory
|
128
|
+
def load_config
|
129
|
+
Config.new
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
let(:container) do
|
135
|
+
IocRb::Container.new do |c|
|
136
|
+
c.bean :config, class: Test::ConfigsFactory, factory_method: :load_config
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should instantiate bean using factory method" do
|
141
|
+
container[:config].should be_instance_of(Test::Config)
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
122
146
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: ioc_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Albert Gazizov
|
@@ -10,18 +10,18 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-
|
13
|
+
date: 2014-04-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
+
prerelease: false
|
17
|
+
name: request_store
|
18
|
+
type: :runtime
|
16
19
|
version_requirements: !ruby/object:Gem::Requirement
|
17
20
|
requirements:
|
18
21
|
- - ! '>='
|
19
22
|
- !ruby/object:Gem::Version
|
20
23
|
version: '0'
|
21
24
|
none: false
|
22
|
-
name: request_store
|
23
|
-
type: :runtime
|
24
|
-
prerelease: false
|
25
25
|
requirement: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ! '>='
|
@@ -29,15 +29,15 @@ dependencies:
|
|
29
29
|
version: '0'
|
30
30
|
none: false
|
31
31
|
- !ruby/object:Gem::Dependency
|
32
|
+
prerelease: false
|
33
|
+
name: activesupport
|
34
|
+
type: :runtime
|
32
35
|
version_requirements: !ruby/object:Gem::Requirement
|
33
36
|
requirements:
|
34
37
|
- - ! '>='
|
35
38
|
- !ruby/object:Gem::Version
|
36
39
|
version: '0'
|
37
40
|
none: false
|
38
|
-
name: activesupport
|
39
|
-
type: :runtime
|
40
|
-
prerelease: false
|
41
41
|
requirement: !ruby/object:Gem::Requirement
|
42
42
|
requirements:
|
43
43
|
- - ! '>='
|
@@ -45,15 +45,15 @@ dependencies:
|
|
45
45
|
version: '0'
|
46
46
|
none: false
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
|
+
prerelease: false
|
49
|
+
name: bundler
|
50
|
+
type: :development
|
48
51
|
version_requirements: !ruby/object:Gem::Requirement
|
49
52
|
requirements:
|
50
53
|
- - ~>
|
51
54
|
- !ruby/object:Gem::Version
|
52
55
|
version: '1.3'
|
53
56
|
none: false
|
54
|
-
name: bundler
|
55
|
-
type: :development
|
56
|
-
prerelease: false
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ~>
|
@@ -61,15 +61,15 @@ dependencies:
|
|
61
61
|
version: '1.3'
|
62
62
|
none: false
|
63
63
|
- !ruby/object:Gem::Dependency
|
64
|
+
prerelease: false
|
65
|
+
name: rake
|
66
|
+
type: :development
|
64
67
|
version_requirements: !ruby/object:Gem::Requirement
|
65
68
|
requirements:
|
66
69
|
- - ! '>='
|
67
70
|
- !ruby/object:Gem::Version
|
68
71
|
version: '0'
|
69
72
|
none: false
|
70
|
-
name: rake
|
71
|
-
type: :development
|
72
|
-
prerelease: false
|
73
73
|
requirement: !ruby/object:Gem::Requirement
|
74
74
|
requirements:
|
75
75
|
- - ! '>='
|
@@ -120,12 +120,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
120
120
|
requirements:
|
121
121
|
- - ! '>='
|
122
122
|
- !ruby/object:Gem::Version
|
123
|
+
segments:
|
124
|
+
- 0
|
125
|
+
hash: -861473019195277398
|
123
126
|
version: '0'
|
124
127
|
none: false
|
125
128
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
129
|
requirements:
|
127
130
|
- - ! '>='
|
128
131
|
- !ruby/object:Gem::Version
|
132
|
+
segments:
|
133
|
+
- 0
|
134
|
+
hash: -861473019195277398
|
129
135
|
version: '0'
|
130
136
|
none: false
|
131
137
|
requirements: []
|
@@ -138,4 +144,3 @@ test_files:
|
|
138
144
|
- spec/ioc_rb/container_spec.rb
|
139
145
|
- spec/ioc_rb/inject_spec.rb
|
140
146
|
- spec/spec_helper.rb
|
141
|
-
has_rdoc:
|