needle 0.5.0 → 0.6.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.
Files changed (61) hide show
  1. data/benchmarks/instantiability.rb +26 -0
  2. data/benchmarks/instantiation.rb +33 -0
  3. data/benchmarks/interceptors.rb +42 -0
  4. data/benchmarks/interceptors2.rb +70 -0
  5. data/doc/README +3 -1
  6. data/doc/di-in-ruby.rdoc +201 -0
  7. data/doc/images/di_classdiagram.jpg +0 -0
  8. data/doc/manual/manual.yml +4 -0
  9. data/doc/manual/parts/01_alternatives.txt +11 -0
  10. data/doc/manual/parts/02_creating.txt +20 -0
  11. data/doc/manual/parts/02_namespaces.txt +1 -1
  12. data/doc/manual/parts/02_services.txt +15 -3
  13. data/doc/manual-html/chapter-1.html +34 -7
  14. data/doc/manual-html/chapter-2.html +43 -9
  15. data/doc/manual-html/chapter-3.html +6 -4
  16. data/doc/manual-html/chapter-4.html +6 -4
  17. data/doc/manual-html/chapter-5.html +6 -4
  18. data/doc/manual-html/chapter-6.html +6 -4
  19. data/doc/manual-html/chapter-7.html +6 -4
  20. data/doc/manual-html/index.html +19 -4
  21. data/lib/needle/container.rb +104 -66
  22. data/{test/tc_models.rb → lib/needle/lifecycle/deferred.rb} +14 -20
  23. data/lib/needle/lifecycle/initialize.rb +49 -0
  24. data/lib/needle/{models → lifecycle}/proxy.rb +16 -8
  25. data/lib/needle/lifecycle/singleton.rb +63 -0
  26. data/lib/needle/lifecycle/threaded.rb +58 -0
  27. data/lib/needle/pipeline/collection.rb +133 -0
  28. data/lib/needle/pipeline/element.rb +85 -0
  29. data/lib/needle/pipeline/interceptor.rb +46 -0
  30. data/lib/needle/registry.rb +48 -8
  31. data/lib/needle/service-point.rb +36 -39
  32. data/lib/needle/thread.rb +87 -0
  33. data/lib/needle/version.rb +1 -1
  34. data/{lib/needle/models/prototype.rb → test/lifecycle/tc_deferred.rb} +15 -17
  35. data/test/lifecycle/tc_initialize.rb +62 -0
  36. data/test/{models → lifecycle}/tc_proxy.rb +5 -5
  37. data/test/lifecycle/tc_singleton.rb +32 -0
  38. data/{lib/needle/models/prototype-deferred.rb → test/lifecycle/tc_threaded.rb} +24 -18
  39. data/test/models/model_test.rb +131 -0
  40. data/test/models/tc_prototype.rb +9 -30
  41. data/test/models/tc_prototype_deferred.rb +9 -31
  42. data/test/models/tc_prototype_deferred_initialize.rb +32 -0
  43. data/test/models/tc_prototype_initialize.rb +32 -0
  44. data/test/models/tc_singleton.rb +8 -29
  45. data/test/models/tc_singleton_deferred.rb +8 -30
  46. data/test/models/tc_singleton_deferred_initialize.rb +32 -0
  47. data/test/models/tc_singleton_initialize.rb +32 -0
  48. data/test/models/tc_threaded.rb +32 -0
  49. data/test/models/tc_threaded_deferred.rb +32 -0
  50. data/test/models/tc_threaded_deferred_initialize.rb +32 -0
  51. data/test/models/tc_threaded_initialize.rb +32 -0
  52. data/test/pipeline/tc_collection.rb +116 -0
  53. data/test/pipeline/tc_element.rb +72 -0
  54. data/test/tc_container.rb +77 -36
  55. data/test/tc_logger.rb +5 -0
  56. data/test/tc_registry.rb +39 -1
  57. data/test/tc_service_point.rb +43 -7
  58. metadata +39 -12
  59. data/lib/needle/models/singleton-deferred.rb +0 -57
  60. data/lib/needle/models/singleton.rb +0 -56
  61. data/lib/needle/models.rb +0 -44
data/test/tc_registry.rb CHANGED
@@ -28,7 +28,45 @@ 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 4, @registry.service_models.length
31
+ assert_equal 12, @registry.service_models.length
32
+ end
33
+
34
+ def test_new_no_options!
35
+ reg = Needle::Registry.new! do
36
+ svc1 { Object.new }
37
+ end
38
+
39
+ assert_respond_to reg, :svc1
40
+ end
41
+
42
+ def test_new_with_options!
43
+ reg = Needle::Registry.new!( :logs => { :device => STDOUT } ) do
44
+ svc1 { Object.new }
45
+ end
46
+
47
+ assert_respond_to reg, :svc1
48
+ assert_equal STDOUT, reg.logs.device
49
+ end
50
+
51
+ def test_new_no_options
52
+ reg = Needle::Registry.new do |r|
53
+ r.register( :svc ) { Object.new }
54
+ end
55
+
56
+ assert_respond_to reg, :svc
57
+ end
58
+
59
+ def test_new_with_options
60
+ reg = Needle::Registry.new( :logs => { :device => STDOUT } ) do |r|
61
+ r.register( :svc ) { Object.new }
62
+ end
63
+
64
+ assert_respond_to reg, :svc
65
+ assert_equal STDOUT, reg.logs.device
66
+ end
67
+
68
+ def test_fullname
69
+ assert_equal "[]", @registry.fullname
32
70
  end
33
71
 
34
72
  end
@@ -17,17 +17,27 @@
17
17
  $:.unshift "../lib"
18
18
 
19
19
  require 'needle/interceptor'
20
+ require 'needle/pipeline/interceptor'
20
21
  require 'needle/service-point'
21
22
  require 'test/unit'
22
23
 
23
24
  class TC_ServicePoint < Test::Unit::TestCase
24
25
 
25
26
  class Model
26
- def initialize(container,opts={},&callback)
27
- @callback = callback
27
+ attr_reader :service_point
28
+ attr_reader :name
29
+ attr_reader :priority
30
+ attr_accessor :succ
31
+
32
+ def initialize(point,name,priority,opts={},&callback)
33
+ @point = point
34
+ @priority = priority || 0
28
35
  end
29
- def instance
30
- @callback.call
36
+ def call( *args )
37
+ succ.call( *args )
38
+ end
39
+ def <=>( item )
40
+ priority <=> item.priority
31
41
  end
32
42
  end
33
43
 
@@ -36,8 +46,13 @@ class TC_ServicePoint < Test::Unit::TestCase
36
46
  self
37
47
  end
38
48
 
39
- def service_models
40
- { :mock => Model }
49
+ def []( name )
50
+ case name
51
+ when :service_models
52
+ { :mock => [ :mock ] }
53
+ when :pipeline_elements
54
+ { :mock => Model, :interceptor => Needle::Pipeline::InterceptorElement }
55
+ end
41
56
  end
42
57
 
43
58
  def fullname
@@ -71,7 +86,7 @@ class TC_ServicePoint < Test::Unit::TestCase
71
86
 
72
87
  assert_nothing_raised do
73
88
  point =
74
- Needle::ServicePoint.new( @container, "test", :model => Model ) {
89
+ Needle::ServicePoint.new( @container, "test", :pipeline => [] ) {
75
90
  Hash.new }
76
91
  end
77
92
  end
@@ -85,6 +100,27 @@ class TC_ServicePoint < Test::Unit::TestCase
85
100
  assert_instance_of Hash, inst
86
101
  end
87
102
 
103
+ def test_constructor_parms_single
104
+ point =
105
+ Needle::ServicePoint.new( @container, "test", :model => :mock ) do |c,p|
106
+ assert_equal @container, c
107
+ Hash.new
108
+ end
109
+
110
+ point.instance
111
+ end
112
+
113
+ def test_constructor_parms_multiple
114
+ point =
115
+ Needle::ServicePoint.new( @container, "test", :model => :mock ) do |c,p|
116
+ assert_equal @container, c
117
+ assert_equal point, p
118
+ Hash.new
119
+ end
120
+
121
+ point.instance
122
+ end
123
+
88
124
  def test_interceptor
89
125
  point =
90
126
  Needle::ServicePoint.new( @container, "test", :model => :mock ) {
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: 0.5.0
7
- date: 2004-10-14
6
+ version: 0.6.0
7
+ date: 2004-10-21
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
@@ -26,9 +26,15 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
26
26
  version:
27
27
  platform: ruby
28
28
  files:
29
+ - benchmarks/instantiation.rb
30
+ - benchmarks/interceptors.rb
31
+ - benchmarks/instantiability.rb
32
+ - benchmarks/interceptors2.rb
29
33
  - doc/README
30
34
  - doc/LICENSE-RUBY
35
+ - doc/di-in-ruby.rdoc
31
36
  - doc/manual-html
37
+ - doc/images
32
38
  - doc/LICENSE-BSD
33
39
  - doc/LICENSE-GPL
34
40
  - doc/manual
@@ -41,7 +47,7 @@ files:
41
47
  - doc/manual-html/chapter-6.html
42
48
  - doc/manual-html/chapter-7.html
43
49
  - doc/manual-html/index.html
44
- - doc/manual/img
50
+ - doc/images/di_classdiagram.jpg
45
51
  - doc/manual/manual.css
46
52
  - doc/manual/manual.yml
47
53
  - doc/manual/tutorial.erb
@@ -53,6 +59,7 @@ files:
53
59
  - doc/manual/parts/02_namespaces.txt
54
60
  - doc/manual/parts/01_support.txt
55
61
  - doc/manual/parts/02_services.txt
62
+ - doc/manual/parts/01_alternatives.txt
56
63
  - doc/manual/parts/01_what_is_needle.txt
57
64
  - doc/manual/parts/02_creating.txt
58
65
  - doc/manual/parts/01_license.txt
@@ -63,35 +70,55 @@ files:
63
70
  - lib/needle/interceptor.rb
64
71
  - lib/needle/service-point.rb
65
72
  - lib/needle/logger.rb
66
- - lib/needle/models.rb
73
+ - lib/needle/thread.rb
74
+ - lib/needle/pipeline
67
75
  - lib/needle/registry.rb
76
+ - lib/needle/lifecycle
68
77
  - lib/needle/interceptor-chain.rb
69
- - lib/needle/models
70
78
  - lib/needle/include-exclude.rb
71
79
  - lib/needle/container.rb
72
80
  - lib/needle/logging-interceptor.rb
73
81
  - lib/needle/version.rb
74
82
  - lib/needle/log-factory.rb
75
83
  - lib/needle/errors.rb
76
- - lib/needle/models/prototype.rb
77
- - lib/needle/models/singleton.rb
78
- - lib/needle/models/singleton-deferred.rb
79
- - lib/needle/models/prototype-deferred.rb
80
- - lib/needle/models/proxy.rb
84
+ - lib/needle/pipeline/interceptor.rb
85
+ - lib/needle/pipeline/collection.rb
86
+ - lib/needle/pipeline/element.rb
87
+ - lib/needle/lifecycle/singleton.rb
88
+ - lib/needle/lifecycle/deferred.rb
89
+ - lib/needle/lifecycle/initialize.rb
90
+ - lib/needle/lifecycle/proxy.rb
91
+ - lib/needle/lifecycle/threaded.rb
81
92
  - test/tc_service_point.rb
82
93
  - test/tc_registry.rb
94
+ - test/pipeline
83
95
  - test/tc_interceptor.rb
84
96
  - test/ALL-TESTS.rb
97
+ - test/lifecycle
85
98
  - test/models
86
99
  - test/tc_interceptor_chain.rb
87
100
  - test/tc_logger.rb
88
- - test/tc_models.rb
89
101
  - test/tc_container.rb
102
+ - test/pipeline/tc_element.rb
103
+ - test/pipeline/tc_collection.rb
104
+ - test/lifecycle/tc_singleton.rb
105
+ - test/lifecycle/tc_threaded.rb
106
+ - test/lifecycle/tc_initialize.rb
107
+ - test/lifecycle/tc_deferred.rb
108
+ - test/lifecycle/tc_proxy.rb
109
+ - test/models/tc_singleton_deferred_initialize.rb
110
+ - test/models/tc_singleton_initialize.rb
90
111
  - test/models/tc_prototype.rb
91
112
  - test/models/tc_singleton.rb
92
113
  - test/models/tc_singleton_deferred.rb
114
+ - test/models/tc_threaded_deferred.rb
115
+ - test/models/tc_prototype_initialize.rb
116
+ - test/models/tc_threaded.rb
117
+ - test/models/tc_prototype_deferred_initialize.rb
118
+ - test/models/tc_threaded_initialize.rb
119
+ - test/models/tc_threaded_deferred_initialize.rb
93
120
  - test/models/tc_prototype_deferred.rb
94
- - test/models/tc_proxy.rb
121
+ - test/models/model_test.rb
95
122
  test_files:
96
123
  - test/ALL-TESTS.rb
97
124
  rdoc_options:
@@ -1,57 +0,0 @@
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
- require 'needle/models/proxy'
18
- require 'thread'
19
-
20
- module Needle
21
- module Models
22
-
23
- # The definition of the "singleton-deferred" lifecycle service model.
24
- # This will result in deferred instantiation of the requested service,
25
- # with a new instance being returned the first time #instance is invoked,
26
- # and that same instance returned every time thereafter.
27
- #
28
- # This model is thread-safe.
29
- class SingletonDeferred
30
-
31
- # Create a new SingletonDeferred service model.
32
- def initialize( container, opts={}, &callback )
33
- @container = container
34
- @callback = callback
35
- @mutex = Mutex.new
36
- @instance = nil
37
- end
38
-
39
- # Return the cached instance, if it exists. Otherwise, create new
40
- # Proxy instance that wraps the container and callback references of
41
- # this service model, and cache it. Then return that instance.
42
- #
43
- # This method is thread-safe.
44
- def instance
45
- return @instance if @instance
46
-
47
- @mutex.synchronize do
48
- return @instance if @instance
49
- @instance = Proxy.new( @container, &@callback )
50
- end
51
-
52
- return @instance
53
- end
54
- end
55
-
56
- end
57
- end
@@ -1,56 +0,0 @@
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
- require 'thread'
18
-
19
- module Needle
20
- module Models
21
-
22
- # The definition of the "singleton" lifecycle service model. This will
23
- # result in immediate instantiation of the requested service, with a new
24
- # instance being returned the first time #instance is invoked, and that
25
- # same instance returned every time thereafter.
26
- #
27
- # This model is thread-safe.
28
- class Singleton
29
-
30
- # Create a new instance of the singleton service model.
31
- def initialize( container, opts={}, &callback )
32
- @container = container
33
- @callback = callback
34
- @mutex = Mutex.new
35
- @instance = nil
36
- end
37
-
38
- # Return the cached instance, if it exists. Otherwise, create new
39
- # instance by invoking the registered callback, caching the result.
40
- # Then return that instance.
41
- #
42
- # This method is thread-safe.
43
- def instance
44
- return @instance if @instance
45
-
46
- @mutex.synchronize do
47
- return @instance if @instance
48
- @instance = @callback.call( @container )
49
- end
50
-
51
- return @instance
52
- end
53
- end
54
-
55
- end
56
- end
data/lib/needle/models.rb DELETED
@@ -1,44 +0,0 @@
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
- require 'needle/models/prototype'
18
- require 'needle/models/prototype-deferred'
19
- require 'needle/models/singleton'
20
- require 'needle/models/singleton-deferred'
21
-
22
- module Needle
23
- module Models
24
-
25
- # A convenience method for registering all standard service models with
26
- # a container. This also defines a <tt>:service_models</tt> service,
27
- # implemented as a Hash, which is used for keeping the references to
28
- # installed service models.
29
- #
30
- # This method is called internally by Registry when it is instantiated,
31
- # and should never be called directly.
32
- def register( registry )
33
- registry.register( :service_models, :model => Singleton ) { Hash.new }
34
- registry[:service_models].update(
35
- :singleton => Singleton,
36
- :singleton_deferred => SingletonDeferred,
37
- :prototype => Prototype,
38
- :prototype_deferred => PrototypeDeferred
39
- )
40
- end
41
- module_function :register
42
-
43
- end
44
- end