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.
- data/benchmarks/instantiability.rb +26 -0
- data/benchmarks/instantiation.rb +33 -0
- data/benchmarks/interceptors.rb +42 -0
- data/benchmarks/interceptors2.rb +70 -0
- data/doc/README +3 -1
- data/doc/di-in-ruby.rdoc +201 -0
- data/doc/images/di_classdiagram.jpg +0 -0
- data/doc/manual/manual.yml +4 -0
- data/doc/manual/parts/01_alternatives.txt +11 -0
- data/doc/manual/parts/02_creating.txt +20 -0
- data/doc/manual/parts/02_namespaces.txt +1 -1
- data/doc/manual/parts/02_services.txt +15 -3
- data/doc/manual-html/chapter-1.html +34 -7
- data/doc/manual-html/chapter-2.html +43 -9
- data/doc/manual-html/chapter-3.html +6 -4
- data/doc/manual-html/chapter-4.html +6 -4
- data/doc/manual-html/chapter-5.html +6 -4
- data/doc/manual-html/chapter-6.html +6 -4
- data/doc/manual-html/chapter-7.html +6 -4
- data/doc/manual-html/index.html +19 -4
- data/lib/needle/container.rb +104 -66
- data/{test/tc_models.rb → lib/needle/lifecycle/deferred.rb} +14 -20
- data/lib/needle/lifecycle/initialize.rb +49 -0
- data/lib/needle/{models → lifecycle}/proxy.rb +16 -8
- data/lib/needle/lifecycle/singleton.rb +63 -0
- data/lib/needle/lifecycle/threaded.rb +58 -0
- data/lib/needle/pipeline/collection.rb +133 -0
- data/lib/needle/pipeline/element.rb +85 -0
- data/lib/needle/pipeline/interceptor.rb +46 -0
- data/lib/needle/registry.rb +48 -8
- data/lib/needle/service-point.rb +36 -39
- data/lib/needle/thread.rb +87 -0
- data/lib/needle/version.rb +1 -1
- data/{lib/needle/models/prototype.rb → test/lifecycle/tc_deferred.rb} +15 -17
- data/test/lifecycle/tc_initialize.rb +62 -0
- data/test/{models → lifecycle}/tc_proxy.rb +5 -5
- data/test/lifecycle/tc_singleton.rb +32 -0
- data/{lib/needle/models/prototype-deferred.rb → test/lifecycle/tc_threaded.rb} +24 -18
- data/test/models/model_test.rb +131 -0
- data/test/models/tc_prototype.rb +9 -30
- data/test/models/tc_prototype_deferred.rb +9 -31
- data/test/models/tc_prototype_deferred_initialize.rb +32 -0
- data/test/models/tc_prototype_initialize.rb +32 -0
- data/test/models/tc_singleton.rb +8 -29
- data/test/models/tc_singleton_deferred.rb +8 -30
- data/test/models/tc_singleton_deferred_initialize.rb +32 -0
- data/test/models/tc_singleton_initialize.rb +32 -0
- data/test/models/tc_threaded.rb +32 -0
- data/test/models/tc_threaded_deferred.rb +32 -0
- data/test/models/tc_threaded_deferred_initialize.rb +32 -0
- data/test/models/tc_threaded_initialize.rb +32 -0
- data/test/pipeline/tc_collection.rb +116 -0
- data/test/pipeline/tc_element.rb +72 -0
- data/test/tc_container.rb +77 -36
- data/test/tc_logger.rb +5 -0
- data/test/tc_registry.rb +39 -1
- data/test/tc_service_point.rb +43 -7
- metadata +39 -12
- data/lib/needle/models/singleton-deferred.rb +0 -57
- data/lib/needle/models/singleton.rb +0 -56
- data/lib/needle/models.rb +0 -44
@@ -0,0 +1,87 @@
|
|
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
|
+
|
21
|
+
# A subclass of Mutex that allows clients to discover which thread has the
|
22
|
+
# mutex locked. This is necessary for (among other things) discovering
|
23
|
+
# cycles in the dependency graph of services.
|
24
|
+
class QueryableMutex < Mutex
|
25
|
+
|
26
|
+
# Checks to see if the current thread has the mutex locked, and if it
|
27
|
+
# does, raises an exception. Otherwise, locks the mutex and sets the
|
28
|
+
# locking thread to Thread.current.
|
29
|
+
def lock
|
30
|
+
if @locking_thread == Thread.current
|
31
|
+
raise ThreadError,
|
32
|
+
"an attempt was made to reacquire an existing lock " +
|
33
|
+
"(this could happen if you have a circular dependency on a service)"
|
34
|
+
end
|
35
|
+
|
36
|
+
while (Thread.critical = true; @locked)
|
37
|
+
@waiting.push Thread.current
|
38
|
+
Thread.stop
|
39
|
+
end
|
40
|
+
@locked = true
|
41
|
+
@locking_thread = Thread.current
|
42
|
+
Thread.critical = false
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
# Attempts to acquire the lock. Returns +true+ if the lock was acquired,
|
47
|
+
# and +false+ if the mutex was already locked.
|
48
|
+
def try_lock
|
49
|
+
result = false
|
50
|
+
Thread.critical = true
|
51
|
+
unless @locked
|
52
|
+
@locked = true
|
53
|
+
result = true
|
54
|
+
@locking_thread = Thread.current
|
55
|
+
end
|
56
|
+
Thread.critical = false
|
57
|
+
result
|
58
|
+
end
|
59
|
+
|
60
|
+
# Unlocks the mutex and sets the locking thread to +nil+.
|
61
|
+
def unlock
|
62
|
+
return unless @locked
|
63
|
+
Thread.critical = true
|
64
|
+
@locked = false
|
65
|
+
begin
|
66
|
+
t = @waiting.shift
|
67
|
+
t.wakeup if t
|
68
|
+
rescue ThreadError
|
69
|
+
retry
|
70
|
+
end
|
71
|
+
@locking_thread = nil
|
72
|
+
Thread.critical = false
|
73
|
+
begin
|
74
|
+
t.run if t
|
75
|
+
rescue ThreadError
|
76
|
+
end
|
77
|
+
self
|
78
|
+
end
|
79
|
+
|
80
|
+
# Return +true+ if the current thread has locked this mutex.
|
81
|
+
def self_locked?
|
82
|
+
@locking_thread == Thread.current
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
data/lib/needle/version.rb
CHANGED
@@ -14,26 +14,24 @@
|
|
14
14
|
# =============================================================================
|
15
15
|
#++
|
16
16
|
|
17
|
-
|
18
|
-
module Models
|
17
|
+
$:.unshift "../../lib"
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
# instance being returned every time #instance is invoked.
|
23
|
-
class Prototype
|
19
|
+
require "needle/lifecycle/deferred"
|
20
|
+
require "test/unit"
|
24
21
|
|
25
|
-
|
26
|
-
def initialize( container, opts={}, &callback )
|
27
|
-
@container = container
|
28
|
-
@callback = callback
|
29
|
-
end
|
22
|
+
class TC_Lifecycle_Deferred < Test::Unit::TestCase
|
30
23
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
end
|
24
|
+
def test_instantiability
|
25
|
+
instantiated = false
|
26
|
+
element = Needle::Lifecycle::Deferred.new( nil )
|
27
|
+
element.succ = Proc.new { instantiated = true; Hash.new }
|
37
28
|
|
29
|
+
assert !instantiated
|
30
|
+
proto = element.call
|
31
|
+
assert !instantiated
|
32
|
+
proto[:test] = :value
|
33
|
+
assert instantiated
|
34
|
+
assert_equal :value, proto[:test]
|
38
35
|
end
|
36
|
+
|
39
37
|
end
|
@@ -0,0 +1,62 @@
|
|
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/lifecycle/initialize"
|
20
|
+
require "test/unit"
|
21
|
+
|
22
|
+
class TC_Lifecycle_Initialize < Test::Unit::TestCase
|
23
|
+
|
24
|
+
class MockNoInit
|
25
|
+
end
|
26
|
+
|
27
|
+
class MockInit
|
28
|
+
attr_reader :value
|
29
|
+
|
30
|
+
def initialize_service
|
31
|
+
@value = :initialize
|
32
|
+
end
|
33
|
+
|
34
|
+
def custom_init
|
35
|
+
@value = :custom
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_no_initialize
|
40
|
+
element = Needle::Lifecycle::Initialize.new( nil )
|
41
|
+
element.succ = Proc.new { MockNoInit.new }
|
42
|
+
result = nil
|
43
|
+
assert_nothing_raised { result = element.call }
|
44
|
+
assert_instance_of MockNoInit, result
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_initialize
|
48
|
+
element = Needle::Lifecycle::Initialize.new( nil )
|
49
|
+
element.succ = Proc.new { MockInit.new }
|
50
|
+
result = element.call
|
51
|
+
assert_equal :initialize, result.value
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_custom_initialize
|
55
|
+
element = Needle::Lifecycle::Initialize.new( nil, nil, nil,
|
56
|
+
:init_method => :custom_init )
|
57
|
+
element.succ = Proc.new { MockInit.new }
|
58
|
+
result = element.call
|
59
|
+
assert_equal :custom, result.value
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -16,13 +16,13 @@
|
|
16
16
|
|
17
17
|
$:.unshift "../../lib"
|
18
18
|
|
19
|
-
require "needle/
|
19
|
+
require "needle/lifecycle/proxy"
|
20
20
|
require "test/unit"
|
21
21
|
|
22
|
-
class
|
22
|
+
class TC_LifecycleProxy < Test::Unit::TestCase
|
23
23
|
|
24
24
|
def test_fail
|
25
|
-
proxy = Needle::
|
25
|
+
proxy = Needle::Lifecycle::Proxy.new( nil ) { Bogus.new }
|
26
26
|
assert_raise( NameError ) do
|
27
27
|
proxy.hello
|
28
28
|
end
|
@@ -33,7 +33,7 @@ class TC_ModelsProxy < Test::Unit::TestCase
|
|
33
33
|
|
34
34
|
def test_succeed
|
35
35
|
instantiated = false
|
36
|
-
proxy = Needle::
|
36
|
+
proxy = Needle::Lifecycle::Proxy.new( nil ) { instantiated = true; Hash.new }
|
37
37
|
assert !instantiated
|
38
38
|
proxy[:test] = :value
|
39
39
|
assert instantiated
|
@@ -41,7 +41,7 @@ class TC_ModelsProxy < Test::Unit::TestCase
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def test_container
|
44
|
-
proxy = Needle::
|
44
|
+
proxy = Needle::Lifecycle::Proxy.new( nil, :container ) do |c|
|
45
45
|
assert_equal :container, c
|
46
46
|
Hash.new
|
47
47
|
end
|
@@ -0,0 +1,32 @@
|
|
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/lifecycle/singleton"
|
20
|
+
require "test/unit"
|
21
|
+
|
22
|
+
class TC_Lifecycle_Singleton < Test::Unit::TestCase
|
23
|
+
|
24
|
+
def test_multiplicity
|
25
|
+
element = Needle::Lifecycle::Singleton.new( nil )
|
26
|
+
element.succ = Proc.new { Hash.new }
|
27
|
+
p1 = element.call
|
28
|
+
p2 = element.call
|
29
|
+
assert_same p1, p2
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -14,28 +14,34 @@
|
|
14
14
|
# =============================================================================
|
15
15
|
#++
|
16
16
|
|
17
|
-
|
17
|
+
$:.unshift "../../lib"
|
18
18
|
|
19
|
-
|
20
|
-
|
19
|
+
require "needle/lifecycle/threaded"
|
20
|
+
require "test/unit"
|
21
21
|
|
22
|
-
|
23
|
-
# This will result in deferred instantiation of the requested service,
|
24
|
-
# with a new instance being returned every time #instance is invoked.
|
25
|
-
class PrototypeDeferred
|
22
|
+
class TC_Lifecycle_Threaded < Test::Unit::TestCase
|
26
23
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
def setup
|
25
|
+
mock = Struct.new( :fullname ).new( "test" )
|
26
|
+
@counter = 0
|
27
|
+
@element = Needle::Lifecycle::Threaded.new( mock )
|
28
|
+
@element.succ = Proc.new { @counter += 1 }
|
29
|
+
end
|
32
30
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
31
|
+
def test_single_thread
|
32
|
+
@element.call
|
33
|
+
@element.call
|
34
|
+
@element.call
|
35
|
+
assert_equal 1, @counter
|
36
|
+
end
|
39
37
|
|
38
|
+
def test_multi_thread
|
39
|
+
threads = []
|
40
|
+
threads << Thread.new { @element.call }
|
41
|
+
threads << Thread.new { @element.call }
|
42
|
+
threads << Thread.new { @element.call }
|
43
|
+
threads.each { |t| t.join }
|
44
|
+
assert_equal 3, @counter
|
40
45
|
end
|
46
|
+
|
41
47
|
end
|
@@ -0,0 +1,131 @@
|
|
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"
|
20
|
+
require "test/unit"
|
21
|
+
|
22
|
+
class ModelTest_MockService
|
23
|
+
attr_reader :init_result
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
@init_result = :none
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize_service
|
30
|
+
@init_result = :initialized
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
module ModelTest
|
36
|
+
|
37
|
+
def self.append_features( base )
|
38
|
+
super
|
39
|
+
base.extend ClassFeatures
|
40
|
+
end
|
41
|
+
|
42
|
+
module ClassFeatures
|
43
|
+
attr_reader :model
|
44
|
+
|
45
|
+
def use( model )
|
46
|
+
@model = model
|
47
|
+
end
|
48
|
+
|
49
|
+
def assert_prototype
|
50
|
+
define_method( :test_multiplicity ) do
|
51
|
+
o1 = @registry[ :test ]
|
52
|
+
o2 = @registry[ :test ]
|
53
|
+
assert_not_same o1, o2
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def assert_singleton
|
58
|
+
define_method( :test_multiplicity ) do
|
59
|
+
o1 = @registry[ :test ]
|
60
|
+
o2 = @registry[ :test ]
|
61
|
+
assert_same o1, o2
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def assert_threaded
|
66
|
+
define_method( :extra_setup ) do
|
67
|
+
cache = Thread.current[:threaded_services]
|
68
|
+
cache.delete "[].test" if cache
|
69
|
+
end
|
70
|
+
|
71
|
+
define_method( :test_multiplicity_singlethread ) do
|
72
|
+
@registry[ :test ].init_result
|
73
|
+
@registry[ :test ].init_result
|
74
|
+
@registry[ :test ].init_result
|
75
|
+
assert_equal 1, @count
|
76
|
+
end
|
77
|
+
|
78
|
+
define_method( :test_multiplicity_multithread ) do
|
79
|
+
threads = []
|
80
|
+
threads << Thread.new { @registry[ :test ].init_result }
|
81
|
+
threads << Thread.new { @registry[ :test ].init_result }
|
82
|
+
threads << Thread.new { @registry[ :test ].init_result }
|
83
|
+
threads.each { |t| t.join }
|
84
|
+
assert_equal 3, @count
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def assert_immediate
|
89
|
+
define_method( :test_laziness ) do
|
90
|
+
@registry[ :test ]
|
91
|
+
assert_equal 1, @count
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def assert_deferred
|
96
|
+
define_method( :test_laziness ) do
|
97
|
+
o = @registry[ :test ]
|
98
|
+
assert_equal 0, @count
|
99
|
+
o.init_result
|
100
|
+
assert_equal 1, @count
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def assert_no_init
|
105
|
+
define_method( :test_initialize ) do
|
106
|
+
o = @registry[ :test ]
|
107
|
+
assert_equal :none, o.init_result
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def assert_init
|
112
|
+
define_method( :test_initialize ) do
|
113
|
+
o = @registry[ :test ]
|
114
|
+
assert_equal :initialized, o.init_result
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def setup
|
120
|
+
@count = 0
|
121
|
+
@registry = Needle::Registry.new do |r|
|
122
|
+
r.register :test, :model => self.class.model do
|
123
|
+
@count += 1
|
124
|
+
ModelTest_MockService.new
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
extra_setup if respond_to?( :extra_setup )
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
data/test/models/tc_prototype.rb
CHANGED
@@ -16,38 +16,17 @@
|
|
16
16
|
|
17
17
|
$:.unshift "../../lib"
|
18
18
|
|
19
|
-
require "needle
|
19
|
+
require "needle"
|
20
20
|
require "test/unit"
|
21
|
+
require File.join( File.dirname( __FILE__ ), "model_test" )
|
21
22
|
|
22
|
-
class
|
23
|
+
class TC_Model_Prototype < Test::Unit::TestCase
|
24
|
+
include ModelTest
|
23
25
|
|
24
|
-
|
25
|
-
instantiated = false
|
26
|
-
model = Needle::Models::Prototype.new( nil ) {
|
27
|
-
instantiated = true
|
28
|
-
Hash.new
|
29
|
-
}
|
30
|
-
|
31
|
-
assert !instantiated
|
32
|
-
proto = model.instance
|
33
|
-
assert instantiated
|
34
|
-
proto[:test] = :value
|
35
|
-
assert_equal :value, proto[:test]
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_container
|
39
|
-
model = Needle::Models::Prototype.new( :container ) { |c|
|
40
|
-
assert_equal :container, c
|
41
|
-
Hash.new
|
42
|
-
}
|
43
|
-
model.instance
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_model
|
47
|
-
model = Needle::Models::Prototype.new( nil ) { Hash.new }
|
48
|
-
p1 = model.instance
|
49
|
-
p2 = model.instance
|
50
|
-
assert_not_same p1, p2
|
51
|
-
end
|
26
|
+
use :prototype
|
52
27
|
|
28
|
+
assert_prototype
|
29
|
+
assert_immediate
|
30
|
+
assert_no_init
|
31
|
+
|
53
32
|
end
|
@@ -16,39 +16,17 @@
|
|
16
16
|
|
17
17
|
$:.unshift "../../lib"
|
18
18
|
|
19
|
-
require "needle
|
19
|
+
require "needle"
|
20
20
|
require "test/unit"
|
21
|
+
require File.join( File.dirname( __FILE__ ), "model_test" )
|
21
22
|
|
22
|
-
class
|
23
|
+
class TC_Model_Prototype_Deferred < Test::Unit::TestCase
|
24
|
+
include ModelTest
|
23
25
|
|
24
|
-
|
25
|
-
instantiated = false
|
26
|
-
model = Needle::Models::PrototypeDeferred.new( nil ) {
|
27
|
-
instantiated = true
|
28
|
-
Hash.new
|
29
|
-
}
|
30
|
-
|
31
|
-
assert !instantiated
|
32
|
-
proto = model.instance
|
33
|
-
assert !instantiated
|
34
|
-
proto[:test] = :value
|
35
|
-
assert instantiated
|
36
|
-
assert_equal :value, proto[:test]
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_container
|
40
|
-
model = Needle::Models::PrototypeDeferred.new( :container ) { |c|
|
41
|
-
assert_equal :container, c
|
42
|
-
Hash.new
|
43
|
-
}
|
44
|
-
model.instance[:test] = :value
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_model
|
48
|
-
model = Needle::Models::PrototypeDeferred.new( nil ) { Hash.new }
|
49
|
-
p1 = model.instance
|
50
|
-
p2 = model.instance
|
51
|
-
assert_not_same p1, p2
|
52
|
-
end
|
26
|
+
use :prototype_deferred
|
53
27
|
|
28
|
+
assert_prototype
|
29
|
+
assert_deferred
|
30
|
+
assert_no_init
|
31
|
+
|
54
32
|
end
|
@@ -0,0 +1,32 @@
|
|
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"
|
20
|
+
require "test/unit"
|
21
|
+
require File.join( File.dirname( __FILE__ ), "model_test" )
|
22
|
+
|
23
|
+
class TC_Model_Prototype_Deferred_Initialize < Test::Unit::TestCase
|
24
|
+
include ModelTest
|
25
|
+
|
26
|
+
use :prototype_deferred_initialize
|
27
|
+
|
28
|
+
assert_prototype
|
29
|
+
assert_deferred
|
30
|
+
assert_init
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,32 @@
|
|
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"
|
20
|
+
require "test/unit"
|
21
|
+
require File.join( File.dirname( __FILE__ ), "model_test" )
|
22
|
+
|
23
|
+
class TC_Model_Prototype_Initialize < Test::Unit::TestCase
|
24
|
+
include ModelTest
|
25
|
+
|
26
|
+
use :prototype_initialize
|
27
|
+
|
28
|
+
assert_prototype
|
29
|
+
assert_immediate
|
30
|
+
assert_init
|
31
|
+
|
32
|
+
end
|
data/test/models/tc_singleton.rb
CHANGED
@@ -16,38 +16,17 @@
|
|
16
16
|
|
17
17
|
$:.unshift "../../lib"
|
18
18
|
|
19
|
-
require "needle
|
19
|
+
require "needle"
|
20
20
|
require "test/unit"
|
21
|
+
require File.join( File.dirname( __FILE__ ), "model_test" )
|
21
22
|
|
22
|
-
class
|
23
|
+
class TC_Model_Singleton < Test::Unit::TestCase
|
24
|
+
include ModelTest
|
23
25
|
|
24
|
-
|
25
|
-
instantiated = false
|
26
|
-
model = Needle::Models::Singleton.new( nil ) {
|
27
|
-
instantiated = true
|
28
|
-
Hash.new
|
29
|
-
}
|
26
|
+
use :singleton
|
30
27
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
proto[:test] = :value
|
35
|
-
assert_equal :value, proto[:test]
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_container
|
39
|
-
model = Needle::Models::Singleton.new( :container ) { |c|
|
40
|
-
assert_equal :container, c
|
41
|
-
Hash.new
|
42
|
-
}
|
43
|
-
model.instance
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_model
|
47
|
-
model = Needle::Models::Singleton.new( nil ) { Hash.new }
|
48
|
-
p1 = model.instance
|
49
|
-
p2 = model.instance
|
50
|
-
assert_same p1, p2
|
51
|
-
end
|
28
|
+
assert_singleton
|
29
|
+
assert_immediate
|
30
|
+
assert_no_init
|
52
31
|
|
53
32
|
end
|