needle 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/doc/LICENSE-BSD +27 -0
  2. data/doc/LICENSE-GPL +280 -0
  3. data/doc/LICENSE-RUBY +56 -0
  4. data/doc/README +70 -0
  5. data/doc/manual/chapter.erb +18 -0
  6. data/doc/manual/index.erb +29 -0
  7. data/doc/manual/manual.css +192 -0
  8. data/doc/manual/manual.rb +240 -0
  9. data/doc/manual/manual.yml +48 -0
  10. data/doc/manual/page.erb +86 -0
  11. data/doc/manual/parts/01_license.txt +5 -0
  12. data/doc/manual/parts/01_support.txt +1 -0
  13. data/doc/manual/parts/01_use_cases.txt +141 -0
  14. data/doc/manual/parts/01_what_is_needle.txt +1 -0
  15. data/doc/manual/parts/02_creating.txt +9 -0
  16. data/doc/manual/parts/02_namespaces.txt +47 -0
  17. data/doc/manual/parts/02_overview.txt +3 -0
  18. data/doc/manual/parts/02_services.txt +44 -0
  19. data/doc/manual/tutorial.erb +30 -0
  20. data/doc/manual-html/chapter-1.html +354 -0
  21. data/doc/manual-html/chapter-2.html +310 -0
  22. data/doc/manual-html/chapter-3.html +154 -0
  23. data/doc/manual-html/chapter-4.html +154 -0
  24. data/doc/manual-html/chapter-5.html +154 -0
  25. data/doc/manual-html/chapter-6.html +154 -0
  26. data/doc/manual-html/chapter-7.html +154 -0
  27. data/doc/manual-html/index.html +177 -0
  28. data/doc/manual-html/manual.css +192 -0
  29. data/lib/needle/container.rb +318 -0
  30. data/lib/needle/errors.rb +32 -0
  31. data/lib/needle/include-exclude.rb +116 -0
  32. data/lib/needle/interceptor-chain.rb +162 -0
  33. data/lib/needle/interceptor.rb +189 -0
  34. data/lib/needle/log-factory.rb +207 -0
  35. data/lib/needle/logger.rb +161 -0
  36. data/lib/needle/logging-interceptor.rb +62 -0
  37. data/lib/needle/models/prototype-deferred.rb +41 -0
  38. data/lib/needle/models/prototype.rb +39 -0
  39. data/lib/needle/models/proxy.rb +84 -0
  40. data/lib/needle/models/singleton-deferred.rb +57 -0
  41. data/lib/needle/models/singleton.rb +56 -0
  42. data/lib/needle/models.rb +44 -0
  43. data/lib/needle/registry.rb +110 -0
  44. data/lib/needle/service-point.rb +109 -0
  45. data/lib/needle/version.rb +28 -0
  46. data/lib/needle.rb +54 -0
  47. data/test/ALL-TESTS.rb +21 -0
  48. data/test/models/tc_prototype.rb +53 -0
  49. data/test/models/tc_prototype_deferred.rb +54 -0
  50. data/test/models/tc_proxy.rb +51 -0
  51. data/test/models/tc_singleton.rb +53 -0
  52. data/test/models/tc_singleton_deferred.rb +54 -0
  53. data/test/tc_container.rb +246 -0
  54. data/test/tc_interceptor.rb +92 -0
  55. data/test/tc_interceptor_chain.rb +181 -0
  56. data/test/tc_logger.rb +181 -0
  57. data/test/tc_models.rb +44 -0
  58. data/test/tc_registry.rb +34 -0
  59. data/test/tc_service_point.rb +100 -0
  60. metadata +107 -0
@@ -0,0 +1,354 @@
1
+ <html>
2
+ <head>
3
+ <title>Needle Manual :: Chapter 1: Introduction</title>
4
+ <link type="text/css" rel="stylesheet" href="manual.css" />
5
+ </head>
6
+
7
+ <body>
8
+ <div id="banner">
9
+ <table border='0' cellpadding='0' cellspacing='0' width='100%'>
10
+ <tr><td valign='top' align='left'>
11
+ <div class="title">
12
+ <span class="product">Needle&mdash;</span><br />
13
+ <span class="tagline">to the point --></span>
14
+ </div>
15
+ </td><td valign='middle' align='right'>
16
+ <div class="info">
17
+ Needle Version: <strong>0.5.0</strong><br />
18
+ Manual Last Updated: <strong>2004-10-15 03:41 GMT</strong>
19
+ </div>
20
+ </td></tr>
21
+ </table>
22
+ </div>
23
+
24
+ <table border='0' width='100%' cellpadding='0' cellspacing='0'>
25
+ <tr><td valign='top'>
26
+
27
+ <div id="navigation">
28
+ <h1>Needle Manual</h1>
29
+
30
+ <h2>Chapters</h2>
31
+ <ol type="I">
32
+
33
+ <li><strong>
34
+ <a href="chapter-1.html">
35
+ Introduction
36
+ </a>
37
+ </strong> <big>&larr;</big>
38
+ <ol type="1">
39
+
40
+ <li><a href="chapter-1.html#s1">What is Needle?</a></li>
41
+
42
+ <li><a href="chapter-1.html#s2">How Can It Help Me?</a></li>
43
+
44
+ <li><a href="chapter-1.html#s3">License Information</a></li>
45
+
46
+ <li><a href="chapter-1.html#s4">Support</a></li>
47
+
48
+ </ol>
49
+ </li>
50
+
51
+ <li>
52
+ <a href="chapter-2.html">
53
+ Registry
54
+ </a>
55
+
56
+ <ol type="1">
57
+
58
+ <li><a href="chapter-2.html#s1">Overview</a></li>
59
+
60
+ <li><a href="chapter-2.html#s2">Creating</a></li>
61
+
62
+ <li><a href="chapter-2.html#s3">Services</a></li>
63
+
64
+ <li><a href="chapter-2.html#s4">Namespaces</a></li>
65
+
66
+ </ol>
67
+ </li>
68
+
69
+ <li>
70
+ <a href="chapter-3.html">
71
+ Dependency Injection
72
+ </a>
73
+
74
+ <ol type="1">
75
+
76
+ </ol>
77
+ </li>
78
+
79
+ <li>
80
+ <a href="chapter-4.html">
81
+ Interceptors
82
+ </a>
83
+
84
+ <ol type="1">
85
+
86
+ </ol>
87
+ </li>
88
+
89
+ <li>
90
+ <a href="chapter-5.html">
91
+ Service Models
92
+ </a>
93
+
94
+ <ol type="1">
95
+
96
+ </ol>
97
+ </li>
98
+
99
+ <li>
100
+ <a href="chapter-6.html">
101
+ Logging
102
+ </a>
103
+
104
+ <ol type="1">
105
+
106
+ </ol>
107
+ </li>
108
+
109
+ <li>
110
+ <a href="chapter-7.html">
111
+ Creating Libraries
112
+ </a>
113
+
114
+ <ol type="1">
115
+
116
+ </ol>
117
+ </li>
118
+
119
+ </ol>
120
+
121
+ <h2>API Reference</h2>
122
+
123
+ <ul>
124
+ <li><a href="http://needle.rubyforge.org/api/index.html">Needle API</a></li>
125
+ </ul>
126
+
127
+ <h2>Tutorials</h2>
128
+ <ol>
129
+
130
+ </ol>
131
+
132
+ <p align="center"><strong>More To Come...</strong></p>
133
+
134
+ <div class="license">
135
+ <a href="http://creativecommons.org/licenses/by-sa/2.0/"><img alt="Creative Commons License" border="0" src="http://creativecommons.org/images/public/somerights" /></a><br />
136
+ This manual is licensed under a <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative Commons License</a>.
137
+ </div>
138
+ </div>
139
+
140
+ </td><td valign='top' width="100%">
141
+
142
+ <div id="content">
143
+
144
+ <h1>1. Introduction</h1>
145
+
146
+
147
+
148
+ <h2>
149
+ <a name="s1"></a>
150
+ 1.1. What is Needle?
151
+ </h2>
152
+
153
+
154
+
155
+ <div class="section">
156
+ <p>Needle is a dependency injection (also, inversion of control) container for <a href="http://www.ruby-lang.org">Ruby</a>.<br />
157
+ </p>
158
+ </div>
159
+
160
+
161
+
162
+ <h2>
163
+ <a name="s2"></a>
164
+ 1.2. How Can It Help Me?
165
+ </h2>
166
+
167
+
168
+
169
+ <div class="section">
170
+ <p>So, what can Needle do for you? Ultimately, it can reduce the amount of code that you have to write, simplifying many common programming tasks for you. This has the two-fold benefit of both decreasing application development time, and of decreasing the effort needed to maintain your application.</p>
171
+
172
+ <p>But what, <em>specifically</em>, can Needle do for you?</p>
173
+
174
+ <p>Try these on for size:</p>
175
+
176
+ <ul>
177
+ <li><a href="#logexec">Log Method Execution</a></li>
178
+ <li><a href="#refsvc">Reference Another Service</a></li>
179
+ <li><a href="#unittest">Unit Testing</a></li>
180
+ <li><a href="#lifecycle">Lifecycle Management</a></li>
181
+ </ul>
182
+
183
+ <p>(Thanks to Howard Lewis Ship for his <a href="http://jakarta.apache.org/hivemind">HiveMind</a> documentation, from which some of the above bullet points were adapted.)</p>
184
+
185
+ <h3>Log Method Execution <a name="#logexec"></a></h3>
186
+
187
+ <p>Needle has an integrated logging framework, and the ability to log execution trace information without modifying a single line of code in your classes. This means that you can easily see what methods get called, with what arguments, and what the return values are, all without having to physically modify any of your classes.</p>
188
+
189
+ <p>Consider the following code, demonstrating how this would be done without Needle:</p>
190
+
191
+ <pre>
192
+ def foo( arg1, arg2 )
193
+ @log.debug( "in foo with #{arg1} and #{arg2}" ) if @log.debug?
194
+ ...
195
+ result = the_result_of_the_method
196
+ @log.debug( "finishing foo with #{result}" ) if @log.debug
197
+ return result
198
+ rescue Exception =&gt; e
199
+ @log.debug( "foo raised exception #{e.message} (#{e.class})" ) if @log.debug?
200
+ raise
201
+ end
202
+ </pre>
203
+
204
+ <p>Now, multiply that by the number of methods in your class&#8230; the logging messages quickly overpower the rest of the code, and detract from the flow of your program. This makes your program harder to debug, test, and maintain.</p>
205
+
206
+ <p>Now, consider the same method using Needle&#8217;s integrated logging framework&#8230;</p>
207
+
208
+ <pre>
209
+ def foo( arg1, arg2 )
210
+ ...
211
+ return the_result_of_the_method
212
+ end
213
+ </pre>
214
+
215
+ <p>Then, when you define the service that you want to add the logging to:</p>
216
+
217
+ <pre>
218
+ registry.register( :service_name_here ) { |reg| ... }
219
+ registry.intercept( :service_name_here ).with! { logging_interceptor }
220
+ </pre>
221
+
222
+ <p>That&#8217;s right. There&#8217;s no explicit logging code in there. Instead, you just tell Needle that the methods of the class should be logged, and away it goes. This has the added benefit of allowing your objects to be unit tested, without spewing log messages everywhere.</p>
223
+
224
+ <h3>Reference Another Service <a name="#refsvc"></a></h3>
225
+
226
+ <p>Invariably in a large application services will reference other services. This is typically accomplished through something like this:</p>
227
+
228
+ <pre>
229
+ class Component
230
+ ...
231
+ def foo( parms )
232
+ @service ||= lookup_service
233
+ @service.do_something( parms )
234
+ end
235
+
236
+ def lookup_service
237
+ ...
238
+ end
239
+ ...
240
+ end
241
+ </pre>
242
+
243
+ <p>Whether the lookup is done lazily, as shown above, or when the class is first instantiated is irrelevant. The point is that you either have to implement a bunch of code to look up a service based on some criteria, or you hard code the class of the service (which creates tight coupling and makes things like unit testing harder).</p>
244
+
245
+ <p>With Needle, you just declare a setter for the service, and then tell Needle that the class depends on the other service:</p>
246
+
247
+ <pre>
248
+ class Component
249
+ attr_writer :service
250
+ ...
251
+ def foo( parms )
252
+ @service.do_something( parms )
253
+ end
254
+ ...
255
+ end
256
+
257
+ registry.register( :component ) do |reg|
258
+ c = Component.new
259
+ c.service = reg.some_other_service
260
+ c
261
+ end
262
+ </pre>
263
+
264
+ <p>Then, when your service is instantiated, Needle will automatically look for and instantiate the dependencies for you. This makes for cleaner code, and looser coupling between services.</p>
265
+
266
+ <h3>Unit Testing <a name="#unittest"></a></h3>
267
+
268
+ <p>Large applications can prove troublesome to unit test exhaustively, especially if there is any kind of tight coupling between components. Such coupling of components can make it difficult to test them separately.</p>
269
+
270
+ <p>Needle, by its very nature, encourages loose coupling of components. Also, because dependencies are never instantiated in code, but are instead accepted via setters or constructor arguments, it is trivial to replace those dependencies with mock objects at unit test time.</p>
271
+
272
+ <p>Consider this tightly coupled example:</p>
273
+
274
+ <pre>
275
+ def foo( args )
276
+ @some_dependency ||= MyNewDependency.new
277
+ @some_dependency.do_something(args)
278
+ end
279
+ </pre>
280
+
281
+ <p>It is impossible to test the method <code>#foo</code> without also testing the MyNewDependency class. However, if the <code>@some_dependency</code> object is made a property that is set externally, you can replace it at test time with a blank:</p>
282
+
283
+ <pre>
284
+ attr_writer :some_dependency
285
+
286
+ def foo( args )
287
+ @some_dependency.do_something( args )
288
+ end
289
+ </pre>
290
+
291
+ <p>The unit test would become something like this:</p>
292
+
293
+ <pre>
294
+ def test_foo
295
+ @obj.some_dependecy = MyMockDependency.new
296
+ @obj.foo( args )
297
+ assert @obj.is_in_some_state
298
+ end
299
+ </pre>
300
+
301
+ <h3>Lifecycle Management <a name="#lifecycle"></a></h3>
302
+
303
+ <p>Singleton objects are a fact of life in complex systems. The singleton design pattern is powerful and useful. However, using the Singleton mixin, or declaring methods at the class level, can make your code difficult to unit test since the state of such objects cannot be easily reset.</p>
304
+
305
+ <p>Needle has a solution. You can tell Needle to treat a service as either a <em>prototype</em> service (meaning it will be instantiated every time you ask for it, like calling <code>#new</code>), or a <em>singleton</em> service (meaning it will only be instantiated once, and the same instance will be returned for subsequent requests).</p>
306
+
307
+ <p>Your object is still just a plain ol&#8217; ordinary Ruby object, but Needle has effectively transformed it into a singleton. This means you can unit test it as if it were nothing special, but when it is used in your application it will act like a singleton.</p>
308
+
309
+ <p>Lifecycle management also means that you can control <em>when</em> a service is instantiated. The <em>prototype</em> and <em>singleton</em> models will always be instantiated as soon as they are requested. Sometimes, though, you don&#8217;t want that&#8212;you&#8217;d like the instantiation to be deferred as late as possible.</p>
310
+
311
+ <p>With Needle, you can indicate that a service should use deferred instantiation. This will cause the service to not actually be instantiated until a method is actually invoked on it. Using this model, you can have services depend on themselves, or other forms of cyclical dependencies.</p>
312
+ </div>
313
+
314
+
315
+
316
+ <h2>
317
+ <a name="s3"></a>
318
+ 1.3. License Information
319
+ </h2>
320
+
321
+
322
+
323
+ <div class="section">
324
+ <p>Needle is made available under either the <span class="caps">BSD</span> license, or the same license Ruby (which, by extension, also allows the <span class="caps">GPL</span> as a permissable license as well). You can view the full text of any of these licenses in the <code>doc</code> subdirectory of the Needle distrubtion. The texts of the <span class="caps">BSD</span> and <span class="caps">GPL</span> licenses are also available online: <a href="http://www.opensource.org/licenses/bsd-license.php">BSD</a> and <a href="http://www.opensource.org/licenses/gpl-license.php">GPL</a>.</p>
325
+
326
+ <p>This manual (in any form, be it source or otherwise) and the scripts and templates used to generate it, are all distributed under the <a href="http://creativecommons.org">Creative Commons</a> <a href="http://creativecommons.org/licenses/by-sa/2.0">Attribution-ShareAlike</a> license.</p>
327
+
328
+ <p>If you desire permission to use either Needle or the manual in a manner incompatible with these licenses, please contact the copyright holder (<a href="mailto:jgb3@email.byu.edu">Jamis Buck</a>) in order to negotiate a more compatible license.<br />
329
+ </p>
330
+ </div>
331
+
332
+
333
+
334
+ <h2>
335
+ <a name="s4"></a>
336
+ 1.4. Support
337
+ </h2>
338
+
339
+
340
+
341
+ <div class="section">
342
+ <p>Mailing lists, bug trackers, feature requests, and public forums are available (courtesy of <a href="http://rubyforge.org">RubyForge</a>) at Needle&#8217;s RubyForge project page. Just direct your browser to <a href="http://rubyforge.org/projects/needle">http://rubyforge.org/projects/needle</a>.<br />
343
+ </p>
344
+ </div>
345
+
346
+
347
+
348
+
349
+ </div>
350
+
351
+ </td></tr>
352
+ </table>
353
+ </body>
354
+ </html>
@@ -0,0 +1,310 @@
1
+ <html>
2
+ <head>
3
+ <title>Needle Manual :: Chapter 2: Registry</title>
4
+ <link type="text/css" rel="stylesheet" href="manual.css" />
5
+ </head>
6
+
7
+ <body>
8
+ <div id="banner">
9
+ <table border='0' cellpadding='0' cellspacing='0' width='100%'>
10
+ <tr><td valign='top' align='left'>
11
+ <div class="title">
12
+ <span class="product">Needle&mdash;</span><br />
13
+ <span class="tagline">to the point --></span>
14
+ </div>
15
+ </td><td valign='middle' align='right'>
16
+ <div class="info">
17
+ Needle Version: <strong>0.5.0</strong><br />
18
+ Manual Last Updated: <strong>2004-10-15 03:41 GMT</strong>
19
+ </div>
20
+ </td></tr>
21
+ </table>
22
+ </div>
23
+
24
+ <table border='0' width='100%' cellpadding='0' cellspacing='0'>
25
+ <tr><td valign='top'>
26
+
27
+ <div id="navigation">
28
+ <h1>Needle Manual</h1>
29
+
30
+ <h2>Chapters</h2>
31
+ <ol type="I">
32
+
33
+ <li>
34
+ <a href="chapter-1.html">
35
+ Introduction
36
+ </a>
37
+
38
+ <ol type="1">
39
+
40
+ <li><a href="chapter-1.html#s1">What is Needle?</a></li>
41
+
42
+ <li><a href="chapter-1.html#s2">How Can It Help Me?</a></li>
43
+
44
+ <li><a href="chapter-1.html#s3">License Information</a></li>
45
+
46
+ <li><a href="chapter-1.html#s4">Support</a></li>
47
+
48
+ </ol>
49
+ </li>
50
+
51
+ <li><strong>
52
+ <a href="chapter-2.html">
53
+ Registry
54
+ </a>
55
+ </strong> <big>&larr;</big>
56
+ <ol type="1">
57
+
58
+ <li><a href="chapter-2.html#s1">Overview</a></li>
59
+
60
+ <li><a href="chapter-2.html#s2">Creating</a></li>
61
+
62
+ <li><a href="chapter-2.html#s3">Services</a></li>
63
+
64
+ <li><a href="chapter-2.html#s4">Namespaces</a></li>
65
+
66
+ </ol>
67
+ </li>
68
+
69
+ <li>
70
+ <a href="chapter-3.html">
71
+ Dependency Injection
72
+ </a>
73
+
74
+ <ol type="1">
75
+
76
+ </ol>
77
+ </li>
78
+
79
+ <li>
80
+ <a href="chapter-4.html">
81
+ Interceptors
82
+ </a>
83
+
84
+ <ol type="1">
85
+
86
+ </ol>
87
+ </li>
88
+
89
+ <li>
90
+ <a href="chapter-5.html">
91
+ Service Models
92
+ </a>
93
+
94
+ <ol type="1">
95
+
96
+ </ol>
97
+ </li>
98
+
99
+ <li>
100
+ <a href="chapter-6.html">
101
+ Logging
102
+ </a>
103
+
104
+ <ol type="1">
105
+
106
+ </ol>
107
+ </li>
108
+
109
+ <li>
110
+ <a href="chapter-7.html">
111
+ Creating Libraries
112
+ </a>
113
+
114
+ <ol type="1">
115
+
116
+ </ol>
117
+ </li>
118
+
119
+ </ol>
120
+
121
+ <h2>API Reference</h2>
122
+
123
+ <ul>
124
+ <li><a href="http://needle.rubyforge.org/api/index.html">Needle API</a></li>
125
+ </ul>
126
+
127
+ <h2>Tutorials</h2>
128
+ <ol>
129
+
130
+ </ol>
131
+
132
+ <p align="center"><strong>More To Come...</strong></p>
133
+
134
+ <div class="license">
135
+ <a href="http://creativecommons.org/licenses/by-sa/2.0/"><img alt="Creative Commons License" border="0" src="http://creativecommons.org/images/public/somerights" /></a><br />
136
+ This manual is licensed under a <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative Commons License</a>.
137
+ </div>
138
+ </div>
139
+
140
+ </td><td valign='top' width="100%">
141
+
142
+ <div id="content">
143
+
144
+ <h1>2. Registry</h1>
145
+
146
+
147
+
148
+ <h2>
149
+ <a name="s1"></a>
150
+ 2.1. Overview
151
+ </h2>
152
+
153
+
154
+
155
+ <div class="section">
156
+ <p>The registry is at the heart of any dependency-injected application or library. All services are registered with the registry, so that when an application needs an instance of a particular service, it may obtain that service reference by querying the registry.</p>
157
+
158
+ <p>In order to use Needle, you only really <em>need</em> to understand how to create and manipulate registry objects.<br />
159
+ </p>
160
+ </div>
161
+
162
+
163
+
164
+ <h2>
165
+ <a name="s2"></a>
166
+ 2.2. Creating
167
+ </h2>
168
+
169
+
170
+
171
+ <div class="section">
172
+ <p>Creating a registry is as simple as calling <code>Needle::Registry.new</code>. This will give you a new registry object, bootstrapped to contain a few general services.</p>
173
+
174
+ <pre>
175
+ require 'needle'
176
+
177
+ registry = Needle::Registry.new
178
+ </pre>
179
+
180
+ <p>Once you have the reference to the registry, you can register services with it, create new namespaces in it, and so forth.<br />
181
+ </p>
182
+ </div>
183
+
184
+
185
+
186
+ <h2>
187
+ <a name="s3"></a>
188
+ 2.3. Services
189
+ </h2>
190
+
191
+
192
+
193
+ <div class="section">
194
+ <p>Registering services with a Needle registry is very straightforward. The simplest way to do it is:</p>
195
+
196
+ <pre>
197
+ registry.register( :foo ) { Bar.new }
198
+ </pre>
199
+
200
+ <p>The above will register a new service with the registry, naming it <code>:foo</code>. When <code>:foo</code> is requested from the registry, a new instance of <code>Bar</code> will be instantiated and returned.</p>
201
+
202
+ <p>You get services from the registry in either of two ways:</p>
203
+
204
+ <pre>
205
+ # Treating the registry as a Hash
206
+ svc = registry[:foo]
207
+
208
+ # Treating the service as a property of the registry
209
+ svc = registry.foo
210
+ </pre>
211
+
212
+ <h3>Convenience Methods</h3>
213
+
214
+ <p>Because you will often need to register many services with a registry at once, a convenience method has been provided to make this use case lean and mean. Just call <code>registry!</code>, passing a block that accepts no parameters. This block will be evaluated in a new context, with any unrecognized method call being interpreted as a new service registration of that name:</p>
215
+
216
+ <pre>
217
+ registry.register! do
218
+ foo { Bar.new }
219
+ bar { Foo.new }
220
+ ...
221
+ end
222
+ </pre>
223
+
224
+ <p>The above will register two new services with the registry, <code>:foo</code> and <code>:bar</code>.</p>
225
+
226
+ <h3>Default Lifecycle</h3>
227
+
228
+ <p>By default, a service is only instantiated once per registry. This means that (using the above example) if the service <code>:foo</code> were queried twice, the registry would return the same object for both queries:</p>
229
+
230
+ <pre>
231
+ svc1 = registry.foo
232
+ svc2 = registry.foo
233
+
234
+ p svc1.object_id == svc2.object_id #=&gt; true
235
+ </pre>
236
+
237
+ <p>You can change this behavior, with <em>service models</em>. See the chapter on Service Models for more information.<br />
238
+ </p>
239
+ </div>
240
+
241
+
242
+
243
+ <h2>
244
+ <a name="s4"></a>
245
+ 2.4. Namespaces
246
+ </h2>
247
+
248
+
249
+
250
+ <div class="section">
251
+ <p>Namespaces allow you to organize your services. The feature has many different applications, including:</p>
252
+
253
+ <ol>
254
+ <li>Third-parties may distribute Needle-enabled libraries without worrying about their choice of service names conflicting with the service names of their clients.</li>
255
+ <li>Developers may organize complex applications into modules, and the service definitions may be stored in the registry to reflect that organization.</li>
256
+ <li>Services deeper in the hierarchy may override services higher up.</li>
257
+ </ol>
258
+
259
+ <p>Creating a namespace is as easy as invoking the <code>#namespace</code> method of the registry (or of another namespace):</p>
260
+
261
+ <pre>
262
+ registry.namespace :stuff
263
+ </pre>
264
+
265
+ <p>This would create a new namespace in the registry called <code>:stuff</code>. The application may then proceed to register services inside that namespace:</p>
266
+
267
+ <pre>
268
+ registry.stuff.register( :foo ) { Bar.new }
269
+ ...
270
+ svc = registry.stuff.foo
271
+ </pre>
272
+
273
+ <p>Here&#8217;s a tip: <em>namespaces are just a special kind of service.</em> This means that you can access namespaces in the same ways that you can access services:</p>
274
+
275
+ <pre>
276
+ svc = registry[:stuff][:foo]
277
+ </pre>
278
+
279
+ <h3>Convenience Methods</h3>
280
+
281
+ <p>Because it is often the case that you will be creating a namespace and then immediately registering services on it, you can pass a block to <code>namespace</code>. The block will receive a reference to the new namespace:</p>
282
+
283
+ <pre>
284
+ registry.namespace :stuff do |spc|
285
+ spc.register( :foo ) { Bar.new }
286
+ ...
287
+ end
288
+ </pre>
289
+
290
+ <p>And, to mirror the <code>register!</code> method, there is also a <code>namespace!</code> method. This method creates a new namespace and then does a <code>register!</code> call on that namespace.</p>
291
+
292
+ <pre>
293
+ registry.namespace! :stuff do
294
+ foo { Bar.new }
295
+ ...
296
+ end
297
+ </pre>
298
+
299
+ <p>The above code would create a new namespace called <code>:stuff</code> in the registry, and would then proceed to register a service called <code>:foo</code> in the new namespace.</p>
300
+ </div>
301
+
302
+
303
+
304
+
305
+ </div>
306
+
307
+ </td></tr>
308
+ </table>
309
+ </body>
310
+ </html>