needle 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/benchmarks/instantiation.rb +39 -0
  2. data/doc/faq/faq.yml +137 -102
  3. data/doc/manual-html/chapter-1.html +15 -19
  4. data/doc/manual-html/chapter-2.html +52 -23
  5. data/doc/manual-html/chapter-3.html +7 -9
  6. data/doc/manual-html/chapter-4.html +6 -8
  7. data/doc/manual-html/chapter-5.html +16 -19
  8. data/doc/manual-html/chapter-6.html +31 -8
  9. data/doc/manual-html/chapter-7.html +19 -8
  10. data/doc/manual-html/chapter-8.html +8 -11
  11. data/doc/manual-html/chapter-9.html +7 -6
  12. data/doc/manual-html/index.html +5 -3
  13. data/doc/manual/manual.yml +2 -1
  14. data/doc/manual/parts/01_alternatives.txt +2 -1
  15. data/doc/manual/parts/02_services.txt +33 -0
  16. data/doc/manual/parts/logging_logfactory.txt +9 -0
  17. data/doc/manual/parts/models_models.txt +4 -0
  18. data/doc/manual/parts/models_pipelines.txt +1 -0
  19. data/lib/needle/container.rb +64 -19
  20. data/lib/needle/definition-context.rb +34 -6
  21. data/lib/needle/lifecycle/multiton.rb +64 -0
  22. data/lib/needle/lifecycle/singleton.rb +2 -2
  23. data/lib/needle/lifecycle/threaded.rb +4 -3
  24. data/lib/needle/pipeline/collection.rb +10 -1
  25. data/lib/needle/pipeline/element.rb +6 -0
  26. data/lib/needle/registry.rb +18 -5
  27. data/lib/needle/service-point.rb +21 -10
  28. data/lib/needle/version.rb +1 -1
  29. data/test/lifecycle/tc_multiton.rb +43 -0
  30. data/test/lifecycle/tc_singleton.rb +18 -2
  31. data/test/lifecycle/tc_threaded.rb +12 -6
  32. data/test/pipeline/tc_collection.rb +26 -2
  33. data/test/services.rb +8 -0
  34. data/test/tc_container.rb +82 -0
  35. data/test/tc_definition_context.rb +63 -7
  36. data/test/tc_registry.rb +13 -1
  37. data/test/tc_service_point.rb +49 -0
  38. metadata +4 -2
@@ -24,10 +24,22 @@ class TC_DefinitionContext < Test::Unit::TestCase
24
24
 
25
25
  class MockContainer
26
26
  attr_reader :events
27
- def initialize; @events = []; end
27
+ attr_reader :defaults
28
+ def initialize; @events = []; @defaults = Hash.new; end
28
29
  def method_missing(s,*a,&b)
29
30
  @events << { :name => s, :args => a, :block => b }
30
31
  end
32
+ def use!( opts )
33
+ orig = @defaults
34
+ @defaults = opts
35
+
36
+ if block_given?
37
+ yield self
38
+ @defaults = orig
39
+ end
40
+
41
+ orig
42
+ end
31
43
  end
32
44
 
33
45
  def setup
@@ -44,17 +56,17 @@ class TC_DefinitionContext < Test::Unit::TestCase
44
56
  assert_not_nil @container.events[0][:block]
45
57
  end
46
58
 
47
- def test_reference_bad
48
- assert_raise( NoMethodError ) do
49
- @ctx.hello( :arg )
50
- end
59
+ def test_reference_missing_parameterized
60
+ @ctx.hello( :arg )
61
+ assert_equal( { :name => :get, :args => [ :hello, :arg ], :block => nil },
62
+ @container.events[0] )
51
63
  end
52
64
 
53
- def test_register_good
65
+ def test_reference_missing_empty
54
66
  assert_nothing_raised do
55
67
  @ctx.hello
56
68
  end
57
- assert_equal :[], @container.events[0][:name]
69
+ assert_equal :get, @container.events[0][:name]
58
70
  assert_equal [ :hello ], @container.events[0][:args]
59
71
  assert_nil @container.events[0][:block]
60
72
  end
@@ -72,4 +84,48 @@ class TC_DefinitionContext < Test::Unit::TestCase
72
84
  assert_equal @container, @ctx.this_container
73
85
  end
74
86
 
87
+ def test_use_bang_without_block
88
+ @ctx.use! :foo => :bar
89
+ assert_equal :bar, @container.defaults[:foo]
90
+ @ctx.use! :blah => :baz
91
+ assert_equal :baz, @container.defaults[:blah]
92
+ assert_nil @container.defaults[:foo]
93
+ end
94
+
95
+ def test_use_bang_with_block
96
+ @ctx.use! :foo => :bar do |r|
97
+ assert_equal r.object_id, @ctx.object_id
98
+ assert_equal :bar, @container.defaults[:foo]
99
+ @ctx.use! :blah => :baz do
100
+ assert_nil @container.defaults[:foo]
101
+ assert_equal :baz, @container.defaults[:blah]
102
+ end
103
+ assert_nil @container.defaults[:blah]
104
+ assert_equal :bar, @container.defaults[:foo]
105
+ end
106
+ assert_nil @container.defaults[:foo]
107
+ end
108
+
109
+ def test_use_without_block
110
+ @ctx.use :foo => :bar
111
+ assert_equal :bar, @container.defaults[:foo]
112
+ @ctx.use :blah => :baz
113
+ assert_equal :baz, @container.defaults[:blah]
114
+ assert_equal :bar, @container.defaults[:foo]
115
+ end
116
+
117
+ def test_use_with_block
118
+ @ctx.use :foo => :bar do |r|
119
+ assert_equal r.object_id, @ctx.object_id
120
+ assert_equal :bar, @container.defaults[:foo]
121
+ @ctx.use :blah => :baz do
122
+ assert_equal :bar, @container.defaults[:foo]
123
+ assert_equal :baz, @container.defaults[:blah]
124
+ end
125
+ assert_nil @container.defaults[:blah]
126
+ assert_equal :bar, @container.defaults[:foo]
127
+ end
128
+ assert_nil @container.defaults[:foo]
129
+ end
130
+
75
131
  end
data/test/tc_registry.rb CHANGED
@@ -28,7 +28,7 @@ class TC_Registry < Test::Unit::TestCase
28
28
  def test_bootstrap
29
29
  assert_respond_to @registry, :service_models
30
30
  assert_instance_of Hash, @registry.service_models
31
- assert_equal 12, @registry.service_models.length
31
+ assert_equal 16, @registry.service_models.length
32
32
  end
33
33
 
34
34
  def test_define_no_options
@@ -109,4 +109,16 @@ class TC_Registry < Test::Unit::TestCase
109
109
  assert !inner.has_key?( :pipeline_elements )
110
110
  end
111
111
 
112
+ def test_parameterized_service
113
+ reg = Needle::Registry.define do |b|
114
+ b.require 'services', "A::B::C", :register_parameterized_services
115
+ end
116
+
117
+ assert_raise( ArgumentError ) do
118
+ reg.baz1 "a", :b, 3
119
+ end
120
+
121
+ assert_equal "a:b:3", reg.baz2( "a", :b, 3 )
122
+ end
123
+
112
124
  end
@@ -16,6 +16,7 @@
16
16
 
17
17
  $:.unshift "../lib"
18
18
 
19
+ require 'needle'
19
20
  require 'needle/interceptor'
20
21
  require 'needle/pipeline/interceptor'
21
22
  require 'needle/service-point'
@@ -39,13 +40,26 @@ class TC_ServicePoint < Test::Unit::TestCase
39
40
  def <=>( item )
40
41
  priority <=> item.priority
41
42
  end
43
+ def reset!
44
+ end
42
45
  end
43
46
 
44
47
  class Container
48
+ def initialize( descended=true )
49
+ @descended = descended
50
+ end
51
+
45
52
  def root
46
53
  self
47
54
  end
48
55
 
56
+ def non_public_services_exist?
57
+ true
58
+ end
59
+
60
+ def non_public_services_exist=( flag )
61
+ end
62
+
49
63
  def []( name )
50
64
  case name
51
65
  when :service_models
@@ -58,6 +72,10 @@ class TC_ServicePoint < Test::Unit::TestCase
58
72
  def fullname
59
73
  "container"
60
74
  end
75
+
76
+ def descended_from?( c )
77
+ @descended
78
+ end
61
79
  end
62
80
 
63
81
  class Interceptor
@@ -142,4 +160,35 @@ class TC_ServicePoint < Test::Unit::TestCase
142
160
  assert_instance_of Needle::InterceptorChainBuilder::InterceptedServiceProxy, inst
143
161
  end
144
162
 
163
+ def test_interceptor_after_instance
164
+ reg = Needle::Registry.new
165
+ reg.define.foo { "hello" }
166
+
167
+ events = []
168
+ reg.intercept( :foo ).doing do |ch,ctx|
169
+ events << "first"
170
+ ch.process_next ctx
171
+ end
172
+ reg.intercept( :foo ).doing do |ch,ctx|
173
+ events << "second"
174
+ ch.process_next ctx
175
+ end
176
+
177
+ obj1 = reg.foo
178
+ obj1.length
179
+ assert_equal [ "first", "second" ], events
180
+
181
+ events = []
182
+ reg.intercept( :foo ).doing do |ch,ctx|
183
+ events << "third"
184
+ ch.process_next ctx
185
+ end
186
+
187
+ obj2 = reg.foo
188
+ obj2.length
189
+ assert_equal [ "first", "second", "third" ], events
190
+
191
+ assert_not_same obj1, obj2
192
+ end
193
+
145
194
  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.1.0
7
- date: 2004-11-11
6
+ version: 1.2.0
7
+ date: 2004-11-18
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
@@ -112,6 +112,7 @@ files:
112
112
  - lib/needle/pipeline/interceptor.rb
113
113
  - lib/needle/pipeline/collection.rb
114
114
  - lib/needle/pipeline/element.rb
115
+ - lib/needle/lifecycle/multiton.rb
115
116
  - lib/needle/lifecycle/singleton.rb
116
117
  - lib/needle/lifecycle/deferred.rb
117
118
  - lib/needle/lifecycle/initialize.rb
@@ -134,6 +135,7 @@ files:
134
135
  - test/lifecycle/tc_singleton.rb
135
136
  - test/lifecycle/tc_threaded.rb
136
137
  - test/lifecycle/tc_initialize.rb
138
+ - test/lifecycle/tc_multiton.rb
137
139
  - test/lifecycle/tc_deferred.rb
138
140
  - test/lifecycle/tc_proxy.rb
139
141
  - test/models/tc_singleton_deferred_initialize.rb