needle 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/tc_container.rb CHANGED
@@ -16,67 +16,73 @@
16
16
 
17
17
  $:.unshift "../lib"
18
18
 
19
- require 'needle/container'
20
- require 'needle/registry'
21
19
  require 'test/unit'
22
20
 
23
- class TC_Container < Test::Unit::TestCase
24
-
25
- class TC_DefinitionContext < Test::Unit::TestCase
26
- class MockContainer
27
- attr_reader :events
28
- def initialize; @events = []; end
29
- def method_missing(s,*a,&b)
30
- @events << { :name => s, :args => a, :block => b }
31
- end
32
- end
33
-
34
- def setup
35
- @container = MockContainer.new
36
- @ctx = Needle::Container::DefinitionContext.new( @container )
37
- end
38
-
39
- def test_register
40
- assert_nothing_raised do
41
- @ctx.hello { "world" }
42
- end
43
- assert_equal :register, @container.events[0][:name]
44
- assert_equal [ :hello ], @container.events[0][:args]
45
- assert_not_nil @container.events[0][:block]
46
- end
21
+ require 'needle/container'
22
+ require 'needle/definition-context'
23
+ require 'needle/interceptor'
24
+ require 'needle/lifecycle/deferred'
25
+ require 'needle/lifecycle/initialize'
26
+ require 'needle/lifecycle/singleton'
27
+ require 'needle/lifecycle/threaded'
28
+ require 'needle/log-factory'
29
+ require 'needle/logging-interceptor'
30
+ require 'needle/pipeline/interceptor'
47
31
 
48
- def test_reference_bad
49
- assert_raise( NoMethodError ) do
50
- @ctx.hello( :arg )
51
- end
52
- end
32
+ class TC_Container < Test::Unit::TestCase
53
33
 
54
- def test_register_good
55
- assert_nothing_raised do
56
- @ctx.hello
57
- end
58
- assert_equal :[], @container.events[0][:name]
59
- assert_equal [ :hello ], @container.events[0][:args]
60
- assert_nil @container.events[0][:block]
61
- end
34
+ class CustomContainer < Needle::Container
35
+ end
62
36
 
63
- def test_intercept
64
- assert_nothing_raised do
65
- @ctx.intercept( :foo )
66
- end
67
- assert_equal :intercept, @container.events[0][:name]
68
- assert_equal [ :foo ], @container.events[0][:args]
69
- assert_nil @container.events[0][:block]
70
- end
37
+ class CustomBuilder < Needle::DefinitionContext
38
+ end
71
39
 
72
- def test_this_container
73
- assert_equal @container, @ctx.this_container
74
- end
40
+ class CustomInterceptor < Needle::Interceptor
41
+ end
75
42
 
43
+ def new_container( *args )
44
+ container = Needle::Container.new( *args )
45
+
46
+ container.register( :pipeline_elements, :pipeline=>[] ) { Hash.new }
47
+ container.pipeline( :pipeline_elements ).add( :singleton,
48
+ Needle::Lifecycle::Singleton )
49
+
50
+ container[:pipeline_elements].update(
51
+ :singleton => Needle::Lifecycle::Singleton,
52
+ :initialize => Needle::Lifecycle::Initialize,
53
+ :deferred => Needle::Lifecycle::Deferred,
54
+ :interceptor => Needle::Pipeline::InterceptorElement,
55
+ :threaded => Needle::Lifecycle::Threaded
56
+ )
57
+
58
+ container.register( :service_models, :pipeline=>[:singleton] ) { Hash.new }
59
+ container[:service_models].update(
60
+ :prototype => [],
61
+ :prototype_initialize => [ :initialize ],
62
+ :prototype_deferred => [ :deferred ],
63
+ :prototype_deferred_initialize => [ :deferred, :initialize ],
64
+ :singleton => [ :singleton ],
65
+ :singleton_initialize => [ :singleton, :initialize ],
66
+ :singleton_deferred => [ :singleton, :deferred ],
67
+ :singleton_deferred_initialize => [ :singleton, :deferred, :initialize ],
68
+ :threaded => [ :threaded ],
69
+ :threaded_initialize => [ :threaded, :initialize ],
70
+ :threaded_deferred => [ :threaded, :deferred ],
71
+ :threaded_deferred_initialize => [ :threaded, :deferred, :initialize ]
72
+ )
73
+
74
+ container.register( :definition_context_factory ) { Needle::DefinitionContext }
75
+ container.register( :namespace_impl_factory ) { Needle::Container }
76
+ container.register( :interceptor_impl_factory ) { Needle::Interceptor }
77
+
78
+ container.register( :logs ) { LogFactory.new( opts[:logs] || {} ) }
79
+ container.register( :logging_interceptor ) { Needle::LoggingInterceptor }
80
+
81
+ container
76
82
  end
77
83
 
78
84
  def test_default
79
- container = Needle::Container.new
85
+ container = new_container
80
86
  assert_nil container.parent
81
87
  assert_nil container.name
82
88
  assert_equal container, container.root
@@ -84,7 +90,7 @@ class TC_Container < Test::Unit::TestCase
84
90
  end
85
91
 
86
92
  def test_named
87
- container = Needle::Container.new( nil, "name" )
93
+ container = new_container( nil, "name" )
88
94
  assert_nil container.parent
89
95
  assert_equal "name", container.name
90
96
  assert_equal container, container.root
@@ -92,36 +98,36 @@ class TC_Container < Test::Unit::TestCase
92
98
  end
93
99
 
94
100
  def test_nested
95
- outer = Needle::Container.new
96
- inner = Needle::Container.new( outer )
101
+ outer = new_container
102
+ inner = new_container( outer )
97
103
  assert_same outer, inner.parent
98
104
  assert_equal outer, inner.root
99
105
  end
100
106
 
101
107
  def test_root
102
- outer = Needle::Container.new
103
- middle = Needle::Container.new( outer )
104
- inner = Needle::Container.new( middle )
108
+ outer = new_container
109
+ middle = new_container( outer )
110
+ inner = new_container( middle )
105
111
  assert_same middle, inner.parent
106
112
  assert_equal outer, inner.root
107
113
  end
108
114
 
109
115
  def test_nested_named
110
- outer = Needle::Container.new( nil, "outer" )
111
- inner = Needle::Container.new( outer, "inner" )
116
+ outer = new_container( nil, "outer" )
117
+ inner = new_container( outer, "inner" )
112
118
  assert_equal "inner", inner.name
113
119
  assert_equal "outer.inner", inner.fullname
114
120
  end
115
121
 
116
122
  def test_service_not_found
117
- container = Needle::Container.new
123
+ container = new_container
118
124
  assert_raise( Needle::ServiceNotFound ) do
119
125
  container[:test]
120
126
  end
121
127
  end
122
128
 
123
129
  def test_register
124
- container = Needle::Container.new
130
+ container = new_container
125
131
  container.register( :test, :pipeline=>[] ) { Hash.new }
126
132
 
127
133
  assert_nothing_raised { container[:test] }
@@ -134,7 +140,7 @@ class TC_Container < Test::Unit::TestCase
134
140
  end
135
141
 
136
142
  def test_builder
137
- container = Needle::Container.new
143
+ container = new_container
138
144
  b1 = container.builder
139
145
  b2 = container.builder
140
146
 
@@ -142,7 +148,7 @@ class TC_Container < Test::Unit::TestCase
142
148
  end
143
149
 
144
150
  def test_define_block
145
- container = Needle::Container.new
151
+ container = new_container
146
152
 
147
153
  container.define do |b|
148
154
  b.test( :pipeline=>[] ) { Hash.new }
@@ -158,14 +164,14 @@ class TC_Container < Test::Unit::TestCase
158
164
  end
159
165
 
160
166
  def test_define_noblock
161
- container = Needle::Container.new
167
+ container = new_container
162
168
  container.define.test( :pipeline=>[] ) { Hash.new }
163
169
  assert container.has_key?( :test )
164
170
  assert_instance_of Hash, container.test
165
171
  end
166
172
 
167
173
  def test_define!
168
- container = Needle::Container.new
174
+ container = new_container
169
175
 
170
176
  container.define! do
171
177
  test( :pipeline=>[] ) { Hash.new }
@@ -181,7 +187,7 @@ class TC_Container < Test::Unit::TestCase
181
187
  end
182
188
 
183
189
  def test_namespace
184
- container = Needle::Container.new
190
+ container = new_container
185
191
  container.namespace( :test, :pipeline=>[] )
186
192
  assert_instance_of Needle::Container, container.test
187
193
 
@@ -193,7 +199,7 @@ class TC_Container < Test::Unit::TestCase
193
199
  end
194
200
 
195
201
  def test_namespace_define
196
- container = Needle::Container.new
202
+ container = new_container
197
203
  container.namespace_define( :test, :pipeline=>[] ) do |b|
198
204
  b.item( :pipeline=>[] ) { Hash.new }
199
205
  end
@@ -202,7 +208,7 @@ class TC_Container < Test::Unit::TestCase
202
208
  end
203
209
 
204
210
  def test_namespace_define!
205
- container = Needle::Container.new
211
+ container = new_container
206
212
  container.namespace_define!( :test, :pipeline=>[] ) do
207
213
  item( :pipeline=>[] ) { Hash.new }
208
214
  end
@@ -211,7 +217,7 @@ class TC_Container < Test::Unit::TestCase
211
217
  end
212
218
 
213
219
  def test_namespace!
214
- container = Needle::Container.new
220
+ container = new_container
215
221
  container.namespace!( :test, :pipeline=>[] ) do
216
222
  item( :pipeline=>[] ) { Hash.new }
217
223
  end
@@ -220,7 +226,7 @@ class TC_Container < Test::Unit::TestCase
220
226
  end
221
227
 
222
228
  def test_has_key
223
- container = Needle::Container.new
229
+ container = new_container
224
230
 
225
231
  assert !container.has_key?(:test)
226
232
  container.register( :test, :pipeline=>[] ) { Hash.new }
@@ -228,7 +234,7 @@ class TC_Container < Test::Unit::TestCase
228
234
  end
229
235
 
230
236
  def test_knows_key
231
- container = Needle::Container.new
237
+ container = new_container
232
238
 
233
239
  assert !container.knows_key?(:test)
234
240
  container.register( :test, :pipeline=>[] ) { Hash.new }
@@ -236,8 +242,8 @@ class TC_Container < Test::Unit::TestCase
236
242
  end
237
243
 
238
244
  def test_parent_knows_key
239
- outer = Needle::Container.new
240
- inner = Needle::Container.new( outer )
245
+ outer = new_container
246
+ inner = new_container( outer )
241
247
 
242
248
  outer.register( :test, :pipeline=>[] ) { Hash.new }
243
249
  assert !inner.has_key?(:test)
@@ -245,8 +251,8 @@ class TC_Container < Test::Unit::TestCase
245
251
  end
246
252
 
247
253
  def test_service_in_parent
248
- outer = Needle::Container.new
249
- inner = Needle::Container.new( outer )
254
+ outer = new_container
255
+ inner = new_container( outer )
250
256
 
251
257
  outer.register( :test, :pipeline=>[] ) { Hash.new }
252
258
  assert_nothing_raised do
@@ -255,8 +261,8 @@ class TC_Container < Test::Unit::TestCase
255
261
  end
256
262
 
257
263
  def test_service_not_in_parent
258
- outer = Needle::Container.new
259
- inner = Needle::Container.new( outer )
264
+ outer = new_container
265
+ inner = new_container( outer )
260
266
 
261
267
  assert_raise( Needle::ServiceNotFound ) do
262
268
  inner[:test]
@@ -264,14 +270,14 @@ class TC_Container < Test::Unit::TestCase
264
270
  end
265
271
 
266
272
  def test_intercept_not_found
267
- container = Needle::Container.new
273
+ container = new_container
268
274
  assert_raise( Needle::ServiceNotFound ) do
269
275
  container.intercept( :test )
270
276
  end
271
277
  end
272
278
 
273
279
  def test_intercept
274
- container = Needle::Registry.new
280
+ container = new_container
275
281
  container.register( :test ) { Hash.new }
276
282
 
277
283
  filtered = false
@@ -284,25 +290,25 @@ class TC_Container < Test::Unit::TestCase
284
290
  end
285
291
 
286
292
  def test_find_definition_missing
287
- container = Needle::Container.new
293
+ container = new_container
288
294
  assert_nil container.find_definition( :bogus )
289
295
  end
290
296
 
291
297
  def test_find_definition_found_local
292
- container = Needle::Container.new
298
+ container = new_container
293
299
  container.register( :test, :pipeline=>[] ) { Object.new }
294
300
  assert_not_nil container.find_definition( :test )
295
301
  end
296
302
 
297
303
  def test_find_definition_found_ancestor
298
- outer = Needle::Container.new
299
- inner = Needle::Container.new( outer )
304
+ outer = new_container
305
+ inner = new_container( outer )
300
306
  outer.register( :test, :pipeline=>[] ) { Object.new }
301
307
  assert_not_nil inner.find_definition( :test )
302
308
  end
303
309
 
304
310
  def test_pipeline
305
- container = Needle::Container.new
311
+ container = new_container
306
312
  container.register( :test, :pipeline=>[] ) { Object.new }
307
313
  assert_instance_of Needle::Pipeline::Collection, container.pipeline( :test )
308
314
 
@@ -312,7 +318,7 @@ class TC_Container < Test::Unit::TestCase
312
318
  end
313
319
 
314
320
  def test_require_default
315
- container = Needle::Container.new
321
+ container = new_container
316
322
  container.register( :service_models, :pipeline=>[] ) { Hash[ :singleton => [] ] }
317
323
  container.require( "services", "A::B::C" )
318
324
 
@@ -321,7 +327,7 @@ class TC_Container < Test::Unit::TestCase
321
327
  end
322
328
 
323
329
  def test_require_custom
324
- container = Needle::Container.new
330
+ container = new_container
325
331
  container.register( :service_models , :pipeline=>[] ) { Hash[ :singleton => [] ] }
326
332
  container.require( "services", "A::B::C", :register_other_services )
327
333
 
@@ -329,4 +335,39 @@ class TC_Container < Test::Unit::TestCase
329
335
  assert_not_nil container[:blah][:baz]
330
336
  end
331
337
 
338
+ def test_custom_namespace_impl
339
+ container = new_container
340
+ container.namespace :subspace
341
+
342
+ subspace = container.subspace
343
+ subspace.register( :namespace_impl_factory ) { CustomContainer }
344
+ subspace.namespace :custom_namespace
345
+ ns = subspace.custom_namespace
346
+
347
+ assert_instance_of Needle::Container, subspace
348
+ assert_instance_of CustomContainer, ns
349
+ end
350
+
351
+ def test_custom_builder_impl
352
+ container = new_container
353
+ container.namespace :subspace
354
+
355
+ subspace = container.subspace
356
+ subspace.register( :definition_context_factory ) { CustomBuilder }
357
+
358
+ assert_equal Needle::DefinitionContext, container.builder.class
359
+ assert_equal CustomBuilder, subspace.builder.class
360
+ end
361
+
362
+ def test_custom_interceptor_impl
363
+ container = new_container
364
+ container.namespace :subspace
365
+
366
+ subspace = container.subspace
367
+ subspace.register( :interceptor_impl_factory ) { CustomInterceptor }
368
+
369
+ assert_equal Needle::Interceptor, container.intercept( :logs ).class
370
+ assert_equal CustomInterceptor, subspace.intercept( :logs ).class
371
+ end
372
+
332
373
  end
@@ -0,0 +1,75 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Needle dependency injection
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Needle
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # needle website : http://needle.rubyforge.org
13
+ # project website: http://rubyforge.org/projects/needle
14
+ # =============================================================================
15
+ #++
16
+
17
+ $:.unshift "../lib"
18
+
19
+ require 'needle/definition-context'
20
+ require 'needle/registry'
21
+ require 'test/unit'
22
+
23
+ class TC_DefinitionContext < Test::Unit::TestCase
24
+
25
+ class MockContainer
26
+ attr_reader :events
27
+ def initialize; @events = []; end
28
+ def method_missing(s,*a,&b)
29
+ @events << { :name => s, :args => a, :block => b }
30
+ end
31
+ end
32
+
33
+ def setup
34
+ @container = MockContainer.new
35
+ @ctx = Needle::DefinitionContext.new( @container )
36
+ end
37
+
38
+ def test_register
39
+ assert_nothing_raised do
40
+ @ctx.hello { "world" }
41
+ end
42
+ assert_equal :register, @container.events[0][:name]
43
+ assert_equal [ :hello ], @container.events[0][:args]
44
+ assert_not_nil @container.events[0][:block]
45
+ end
46
+
47
+ def test_reference_bad
48
+ assert_raise( NoMethodError ) do
49
+ @ctx.hello( :arg )
50
+ end
51
+ end
52
+
53
+ def test_register_good
54
+ assert_nothing_raised do
55
+ @ctx.hello
56
+ end
57
+ assert_equal :[], @container.events[0][:name]
58
+ assert_equal [ :hello ], @container.events[0][:args]
59
+ assert_nil @container.events[0][:block]
60
+ end
61
+
62
+ def test_intercept
63
+ assert_nothing_raised do
64
+ @ctx.intercept( :foo )
65
+ end
66
+ assert_equal :intercept, @container.events[0][:name]
67
+ assert_equal [ :foo ], @container.events[0][:args]
68
+ assert_nil @container.events[0][:block]
69
+ end
70
+
71
+ def test_this_container
72
+ assert_equal @container, @ctx.this_container
73
+ end
74
+
75
+ end
data/test/tc_registry.rb CHANGED
@@ -86,4 +86,27 @@ class TC_Registry < Test::Unit::TestCase
86
86
  assert_nil @registry.fullname
87
87
  end
88
88
 
89
+ def test_nested_parent
90
+ inner = Needle::Registry.new( :parent => @registry )
91
+ assert_equal @registry, inner.parent
92
+ end
93
+
94
+ def test_explicit_name
95
+ reg = Needle::Registry.new( :name => :test )
96
+ assert_equal :test, reg.name
97
+ assert_nil reg.fullname
98
+ end
99
+
100
+ def test_nested_fullname
101
+ middle = Needle::Registry.new( :parent => @registry, :name => :middle )
102
+ inner = Needle::Registry.new( :parent => middle, :name => :test )
103
+ assert_equal "middle.test", inner.fullname
104
+ end
105
+
106
+ def test_bootstrap_once
107
+ inner = Needle::Registry.new( :parent => @registry )
108
+ assert inner.knows_key?( :pipeline_elements )
109
+ assert !inner.has_key?( :pipeline_elements )
110
+ end
111
+
89
112
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.1
3
3
  specification_version: 1
4
4
  name: needle
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.0
7
- date: 2004-11-04
6
+ version: 1.1.0
7
+ date: 2004-11-11
8
8
  summary: Needle is a Dependency Injection/Inversion of Control container for Ruby. It supports both type-2 (setter) and type-3 (constructor) injection. It takes advantage of the dynamic nature of Ruby to provide a rich and flexible approach to injecting dependencies.
9
9
  require_paths:
10
10
  - lib
@@ -50,6 +50,7 @@ files:
50
50
  - doc/manual-html/chapter-6.html
51
51
  - doc/manual-html/chapter-7.html
52
52
  - doc/manual-html/chapter-8.html
53
+ - doc/manual-html/chapter-9.html
53
54
  - doc/manual-html/index.html
54
55
  - doc/images/di_classdiagram.jpg
55
56
  - doc/manual/manual.css
@@ -69,6 +70,7 @@ files:
69
70
  - doc/manual/parts/02_services.txt
70
71
  - doc/manual/parts/04_setup.txt
71
72
  - doc/manual/parts/01_alternatives.txt
73
+ - doc/manual/parts/customizing_contexts.txt
72
74
  - doc/manual/parts/04_overview.txt
73
75
  - doc/manual/parts/models_models.txt
74
76
  - doc/manual/parts/logging_overview.txt
@@ -81,8 +83,10 @@ files:
81
83
  - doc/manual/parts/01_license.txt
82
84
  - doc/manual/parts/01_use_cases.txt
83
85
  - doc/manual/parts/libraries_creating.txt
86
+ - doc/manual/parts/customizing_interceptors.txt
84
87
  - doc/manual/parts/02_overview.txt
85
88
  - doc/manual/parts/03_conventional.txt
89
+ - doc/manual/parts/customizing_namespaces.txt
86
90
  - doc/manual/parts/logging_configuration.txt
87
91
  - doc/manual/parts/interceptors_custom.txt
88
92
  - doc/manual/parts/libraries_overview.txt
@@ -104,6 +108,7 @@ files:
104
108
  - lib/needle/version.rb
105
109
  - lib/needle/log-factory.rb
106
110
  - lib/needle/errors.rb
111
+ - lib/needle/definition-context.rb
107
112
  - lib/needle/pipeline/interceptor.rb
108
113
  - lib/needle/pipeline/collection.rb
109
114
  - lib/needle/pipeline/element.rb
@@ -115,6 +120,7 @@ files:
115
120
  - test/tc_service_point.rb
116
121
  - test/tc_registry.rb
117
122
  - test/pipeline
123
+ - test/tc_definition_context.rb
118
124
  - test/tc_interceptor.rb
119
125
  - test/ALL-TESTS.rb
120
126
  - test/lifecycle