takai-object_injection 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README ADDED
File without changes
@@ -0,0 +1,27 @@
1
+ # -*- mode: ruby; coding: utf-8-unix -*-
2
+ #
3
+ # Copyright 2009 Naoto Takai
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ if RUBY_VERSION < '1.9.1'
18
+ raise "ObjectInjection requires Ruby 1.9.1 or later."
19
+ else
20
+ require 'rubygems'
21
+ require 'methopara' if RUBY_VERSION == '1.9.1'
22
+
23
+ require 'object_injection/container.rb'
24
+ require 'object_injection/definition.rb'
25
+ require 'object_injection/dictionary.rb'
26
+ require 'object_injection/errors.rb'
27
+ end
@@ -0,0 +1,78 @@
1
+ # -*- mode: ruby; coding: utf-8-unix -*-
2
+ #
3
+ # Copyright 2009 Naoto Takai
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module ObjectInjection
18
+ class Container
19
+
20
+ def initialize
21
+ @dic = Dictionary.new
22
+ @pool = {}
23
+ @checked = false
24
+ end
25
+
26
+ def add klass
27
+ @checked = false
28
+
29
+ d = Definition.new klass
30
+ @dic.add d
31
+ end
32
+
33
+ def get key
34
+ check_dictionary
35
+
36
+ if cached = @pool[key]
37
+ return cached
38
+ else
39
+ obj = lookup_and_setup key
40
+ @pool[key] = obj
41
+
42
+ obj
43
+ end
44
+ end
45
+
46
+ private
47
+ def check_dictionary
48
+ unless @chekced
49
+ @dic.check_constructor_dependency
50
+ @checked = true
51
+ end
52
+ end
53
+ def lookup_and_setup key
54
+ d = @dic.lookup key
55
+ obj = instatiate d
56
+ setup_properties obj, d
57
+
58
+ obj
59
+ end
60
+
61
+ def instatiate d
62
+ klass = d.source
63
+ args = instantiate_args d
64
+
65
+ klass.new *args
66
+ end
67
+ def instantiate_args d
68
+ d.constructor_depends.map do |dep|
69
+ get dep
70
+ end
71
+ end
72
+ def setup_properties obj, d
73
+ d.property_depends.each do |dep|
74
+ obj.__send__ "#{dep.to_s.downcase}=", get(dep)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,84 @@
1
+ # -*- mode: ruby; coding: utf-8-unix -*-
2
+ #
3
+ # Copyright 2009 Naoto Takai
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module ObjectInjection
18
+ class Definition
19
+ attr_reader :constructor_depends
20
+ attr_reader :property_depends
21
+ attr_reader :provides
22
+ attr_reader :source
23
+
24
+ def initialize klass
25
+ @source = klass
26
+ @provides = extract_provides klass
27
+ @property_depends = extract_property_depends klass
28
+ @constructor_depends = extract_constructor_depends klass
29
+ end
30
+
31
+ def to_sym
32
+ to_class_name(@source).to_sym
33
+ end
34
+
35
+ def eql? other
36
+ self.source.eql? other.source
37
+ end
38
+
39
+ def hash
40
+ self.source.hash
41
+ end
42
+
43
+ private
44
+ def extract_provides klass
45
+ name = to_class_name(klass)
46
+ cases = split_camelcase(name)
47
+ join_as_sym(cases)
48
+ end
49
+
50
+ def extract_property_depends klass
51
+ methods = klass.public_instance_methods
52
+ methods.grep(/^(\w+)=$/){ |m|
53
+ snail_to_camel($1).to_sym
54
+ }
55
+ end
56
+
57
+ def extract_constructor_depends klass
58
+ params = klass.instance_method(:initialize).parameters
59
+ params.map {|p|
60
+ snail_to_camel(p.last.to_s).to_sym
61
+ }
62
+ end
63
+
64
+ def to_class_name klass
65
+ fqn = klass.name or
66
+ raise DefinitionError.new("Anonymous class is not supported.")
67
+ fqn.split('::').last
68
+ end
69
+
70
+ def split_camelcase name
71
+ name.scan(/[A-Z][a-z0-9_]*/)
72
+ end
73
+
74
+ def join_as_sym(cases)
75
+ cases.map.with_index do |c, i|
76
+ cases[i, cases.size].join.to_sym
77
+ end
78
+ end
79
+
80
+ def snail_to_camel pattern
81
+ pattern.split('_').map(&:capitalize).join
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,64 @@
1
+ # -*- mode: ruby; coding: utf-8-unix -*-
2
+ #
3
+ # Copyright 2009 Naoto Takai
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module ObjectInjection
18
+ class Dictionary
19
+ def initialize
20
+ @definitions = []
21
+ @providings = {}
22
+ end
23
+ def add d
24
+ if @definitions.find {|i| i.eql? d }
25
+ raise DuplicateDefinitionError.new("#{d.source} is duplicate.")
26
+ end
27
+ @definitions.push d
28
+ add_providings_to_map d
29
+ end
30
+ def lookup key
31
+ @providings[key]
32
+ end
33
+ def check_constructor_dependency
34
+ @definitions.each do |d|
35
+ scan_dependencies @definitions.dup, d
36
+ end
37
+ end
38
+
39
+ private
40
+ def add_providings_to_map d
41
+ d.provides.each do |k|
42
+ add_or_remove_keywords k, d
43
+ end
44
+ end
45
+ def add_or_remove_keywords k, d
46
+ unless @providings[k]
47
+ @providings[k] = d
48
+ else
49
+ # remove ambiguous keyword.
50
+ @providings.delete k
51
+ end
52
+ end
53
+ def scan_dependencies defs, d
54
+ d.constructor_depends.each do |dep|
55
+ found = lookup(dep) or
56
+ raise DependencyNotFoundError.new("#{d} depends on #{dep} but not found}")
57
+ defs.delete(found) or
58
+ raise CircularReferenceError.new
59
+
60
+ scan_dependencies defs, found
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,25 @@
1
+ # -*- mode: ruby; coding: utf-8-unix -*-
2
+ #
3
+ # Copyright 2009 Naoto Takai
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module ObjectInjection
18
+ class ObjectInjectionError < StandardError; end
19
+
20
+ class CircularReferenceError < ObjectInjectionError; end
21
+ class ContainerError < ObjectInjectionError; end
22
+ class DefinitionError < ObjectInjectionError; end
23
+ class DependencyNotFoundError < ObjectInjectionError; end
24
+ class DuplicateDefinitionError < ObjectInjectionError; end
25
+ end
@@ -0,0 +1,50 @@
1
+ # -*- mode: ruby; coding: utf-8-unix -*-
2
+ #
3
+ # Copyright 2009 Naoto Takai
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ Gem::Specification.new do |spec|
18
+ spec.name = 'object_injection'
19
+ spec.version = '0.1.1'
20
+
21
+ spec.authors = ['Naoto Takai']
22
+ spec.date = '2009-03-24'
23
+ spec.description = 'ObjectInjection is dependency injection container.'
24
+ spec.email = 'takai@recompile.net'
25
+ spec.files = %w{
26
+ LICENSE
27
+ README
28
+ lib
29
+ lib/object_injection
30
+ lib/object_injection/container.rb
31
+ lib/object_injection/dictionary.rb
32
+ lib/object_injection/definition.rb
33
+ lib/object_injection/errors.rb
34
+ lib/object_injection.rb
35
+ object_injection.gemspec
36
+ spec
37
+ spec/dictionary_spec.rb
38
+ spec/spec.opts
39
+ spec/spec_helper.rb
40
+ spec/definition_spec.rb
41
+ spec/container_spec.rb
42
+ }
43
+ spec.has_rdoc = true
44
+ spec.homepage = 'http://github.com/takai/object_injection/'
45
+ spec.rdoc_options = ['--inline-source', '--charset=UTF-8']
46
+ spec.require_paths = ['lib']
47
+ spec.required_ruby_version = '>= 1.9.1'
48
+ spec.rubygems_version = '1.3.1'
49
+ spec.summary = 'Dependency injection container for Ruby.'
50
+ end
@@ -0,0 +1,77 @@
1
+ # -*- mode: ruby; coding: utf-8-unix -*-
2
+ #
3
+ # Copyright 2009 Naoto Takai
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "#{File.dirname(__FILE__)}/spec_helper"
18
+
19
+ describe ObjectInjection::Container do
20
+
21
+ include ObjectInjection
22
+
23
+ before :each do
24
+ @container = Container.new
25
+ end
26
+
27
+ context "with property injection" do
28
+ class Juicer
29
+ attr_accessor :fruit
30
+ end
31
+
32
+ class Fruit
33
+ end
34
+
35
+ it "should return instance of added class when #get (1)" do
36
+ @container.add Fruit
37
+ @container.get(:Fruit).should be_a Fruit
38
+ end
39
+
40
+ it "should return instance of added class when #get (2)" do
41
+ @container.add Fruit
42
+ @container.add Juicer
43
+
44
+ juicer = @container.get(:Juicer)
45
+ juicer.should be_a Juicer
46
+ juicer.fruit.should be_a Fruit
47
+ end
48
+ end
49
+
50
+ context "with constructor injection" do
51
+ class Juicer
52
+ def initialize fruit
53
+ end
54
+ end
55
+
56
+ class Fruit
57
+ end
58
+
59
+ it "should return instance of juicer" do
60
+ @container.add Fruit
61
+ @container.add Juicer
62
+
63
+ juicer = @container.get(:Juicer)
64
+ end
65
+ end
66
+
67
+ context "generic behaviour" do
68
+ class Fruit; end
69
+
70
+ it "should be same instances" do
71
+ @container.add Fruit
72
+ @container.get(:Fruit).should equal @container.get(:Fruit)
73
+ end
74
+ end
75
+
76
+ end
77
+
@@ -0,0 +1,88 @@
1
+ # -*- mode: ruby; coding: utf-8-unix -*-
2
+ #
3
+ # Copyright 2009 Naoto Takai
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "#{File.dirname(__FILE__)}/spec_helper"
18
+
19
+ describe ObjectInjection::Definition do
20
+
21
+ include ObjectInjection
22
+
23
+ class Manager; end
24
+ class ObjectManager; end
25
+ class SingletonObjectManager; end
26
+ module Nested
27
+ module Nested
28
+ class Manager; end
29
+ end
30
+ end
31
+ class A; end
32
+
33
+ it "should hold source class" do
34
+ Definition.new(Manager).source.should equal Manager
35
+ end
36
+
37
+ it "should know providings of given class" do
38
+ m = Definition.new Manager
39
+ m.provides.should eql [:Manager]
40
+ om = Definition.new ObjectManager
41
+ om.provides.should eql [:ObjectManager, :Manager]
42
+ som = Definition.new SingletonObjectManager
43
+ som.provides.should eql [:SingletonObjectManager, :ObjectManager, :Manager]
44
+ nnm = Definition.new Nested::Nested::Manager
45
+ nnm.provides.should eql [:Manager]
46
+ a = Definition.new A
47
+ a.provides.should eql [:A]
48
+ end
49
+
50
+ it "should raise DefinitionError when anonymous class is passed" do
51
+ ->{ Definition.new Class.new }.should raise_error DefinitionError
52
+ end
53
+
54
+ it "should know property dependencies of given class" do
55
+ class Juicer
56
+ attr_writer :fruit
57
+ attr_writer :fruit_mix
58
+ end
59
+
60
+ j = Definition.new Juicer
61
+ j.property_depends.should eql [:Fruit, :FruitMix]
62
+ end
63
+
64
+ it "should know constructor dependencies of given class" do
65
+ class Juicer
66
+ def initialize fruit, fruit_mix
67
+ end
68
+ end
69
+ class Fruit
70
+ end
71
+
72
+ j = Definition.new Juicer
73
+ j.constructor_depends.should eql [:Fruit, :FruitMix]
74
+ f = Definition.new Fruit
75
+ f.constructor_depends.should eql []
76
+ end
77
+
78
+ it "should convert to symbol" do
79
+ Definition.new(Manager).to_sym.should eql :Manager
80
+ Definition.new(ObjectManager).to_sym.should eql :ObjectManager
81
+ end
82
+
83
+ it "should eql having same source" do
84
+ Definition.new(Manager).should eql Definition.new(Manager)
85
+ end
86
+
87
+ end
88
+
@@ -0,0 +1,117 @@
1
+ # -*- mode: ruby; coding: utf-8-unix -*-
2
+ #
3
+ # Copyright 2009 Naoto Takai
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "#{File.dirname(__FILE__)}/spec_helper"
18
+
19
+ describe ObjectInjection::Dictionary do
20
+
21
+ include ObjectInjection
22
+
23
+ class Term; end
24
+ class TechnicalTerm; end
25
+ class LegalTerm; end
26
+
27
+ class A
28
+ def initialize b
29
+ end
30
+ end
31
+
32
+ class B
33
+ def initialize c
34
+ end
35
+ end
36
+
37
+ class C
38
+ def initialize a
39
+ end
40
+ end
41
+
42
+ class D
43
+ def initialize e
44
+ end
45
+ end
46
+
47
+ class E
48
+ end
49
+
50
+ class F
51
+ def initialize h
52
+ end
53
+ end
54
+ class G
55
+ def initialize h
56
+ end
57
+ end
58
+ class H
59
+ end
60
+
61
+ before :each do
62
+ @dic = Dictionary.new
63
+ end
64
+
65
+ it "should look-up definition by provides" do
66
+ tt = Definition.new TechnicalTerm
67
+ @dic.add tt
68
+ @dic.lookup(:Term).should eql tt
69
+ end
70
+
71
+ it "should return nil when ambiguous term is added" do
72
+ tt = Definition.new TechnicalTerm
73
+ lt = Definition.new LegalTerm
74
+ @dic.add tt
75
+ @dic.add lt
76
+ @dic.lookup(:TechnicalTerm).should eql tt
77
+ @dic.lookup(:LegalTerm).should eql lt
78
+ @dic.lookup(:Term).should be_nil
79
+ end
80
+
81
+ it "should raise error when circular reference found" do
82
+ @dic.add Definition.new(A)
83
+ @dic.add Definition.new(B)
84
+ @dic.add Definition.new(C)
85
+
86
+ -> {
87
+ @dic.check_constructor_dependency
88
+ }.should raise_error CircularReferenceError
89
+ end
90
+
91
+ it "should not raise error when circular reference is not found (1)" do
92
+ @dic.add Definition.new(D)
93
+ @dic.add Definition.new(E)
94
+
95
+ -> {
96
+ @dic.check_constructor_dependency
97
+ }.should_not raise_error
98
+ end
99
+
100
+ it "should not raise error when circular reference is not found (2)" do
101
+ @dic.add Definition.new(F)
102
+ @dic.add Definition.new(G)
103
+ @dic.add Definition.new(H)
104
+
105
+ -> {
106
+ @dic.check_constructor_dependency
107
+ }.should_not raise_error
108
+ end
109
+
110
+ it "should raise error when duplicate definition is added" do
111
+ -> {
112
+ @dic.add Definition.new(Term)
113
+ @dic.add Definition.new(Term)
114
+ }.should raise_error DuplicateDefinitionError
115
+ end
116
+
117
+ end
@@ -0,0 +1 @@
1
+ -c
@@ -0,0 +1,19 @@
1
+ # -*- mode: ruby; coding: utf-8-unix -*-
2
+ #
3
+ # Copyright 2009 Naoto Takai
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ $LOAD_PATH << "#{File.dirname(__FILE__)}/../lib"
18
+
19
+ require 'object_injection'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: takai-object_injection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Naoto Takai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: ObjectInjection is dependency injection container.
17
+ email: takai@recompile.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - LICENSE
26
+ - README
27
+ - lib
28
+ - lib/object_injection
29
+ - lib/object_injection/container.rb
30
+ - lib/object_injection/dictionary.rb
31
+ - lib/object_injection/definition.rb
32
+ - lib/object_injection/errors.rb
33
+ - lib/object_injection.rb
34
+ - object_injection.gemspec
35
+ - spec
36
+ - spec/dictionary_spec.rb
37
+ - spec/spec.opts
38
+ - spec/spec_helper.rb
39
+ - spec/definition_spec.rb
40
+ - spec/container_spec.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/takai/object_injection/
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --inline-source
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.9.1
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: Dependency injection container for Ruby.
68
+ test_files: []
69
+