blockenspiel 0.2.0.1-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ === 0.2.0 / 2009-04-15
2
+
3
+ * Now compatible with Ruby 1.9.
4
+ * Now compatible with JRuby 1.2.
5
+ * No longer requires the mixology gem.
6
+ * Building no longer requires hoe.
7
+
8
+ === 0.1.1 / 2008-11-06
9
+
10
+ * Added ability to pass the block as the first parameter in
11
+ the dynamic DSL builder API; cleaned up the API a little
12
+ * Minor fixes to Implementing DSL Blocks paper
13
+ * Some updates to rdocs
14
+ * More test coverage
15
+
16
+ === 0.1.0 / 2008-10-29
17
+
18
+ * Alpha release, opened for public feedback
19
+ * Tightened constraints on block parameters
20
+ * Added some test cases for threads and parameter constraints
21
+ * Revisions to the Implementing DSL Blocks paper
22
+
23
+ === 0.0.4 / 2008-10-24
24
+
25
+ * Improvements to the logic for choosing behaviors
26
+ * Added exception classes and provided better error handling
27
+ * Actually added the behavior test case to the gem manifest...
28
+ * Documentation revisions
29
+ * Revisions to the Implementing DSL Blocks paper
30
+
31
+ === 0.0.3 / 2008-10-23
32
+
33
+ * Added :proxy behavior for parameterless blocks
34
+ * Removed option to turn off inheriting, since the semantics are somewhat
35
+ ill-defined and inconsistent. All parameterless blocks now exhibit the
36
+ inheriting behavior.
37
+ * Added tests for the different behavior settings.
38
+
39
+ === 0.0.2 / 2008-10-21
40
+
41
+ * Cleaned up some of the documentation
42
+ * Revisions to the Implementing DSL Blocks paper
43
+
44
+ === 0.0.1 / 2008-10-20
45
+
46
+ * Initial test release
@@ -0,0 +1,934 @@
1
+ == Implementing DSL Blocks
2
+
3
+ by Daniel Azuma, 29 October 2008
4
+
5
+ A <em>DSL block</em> is a construct commonly used in Ruby APIs, in which a DSL (domain-specific language) is made available inside a block passed to an API call. In this paper I present an overview of different implementation strategies for this important pattern. I will first describe the features of DSL blocks, utilizing illustrations from several well-known Ruby libraries. I will then survey and critique five implementation strategies that have been put forth. Finally, I will present a new library, {Blockenspiel}[http://virtuoso.rubyforge.org/blockenspiel], designed to be a comprehensive implementation of DSL blocks.
6
+
7
+ === An illustrative overview of DSL blocks
8
+
9
+ If you've done much Ruby programming, chances are you've run into mini-DSLs (domain-specific languages) that live inside blocks. Perhaps you've encountered them in Ruby standard library calls, such as <tt>File#open</tt>, a call that lets you interact with a stream while performing automatic setup and cleanup for you:
10
+
11
+ File.open("myfile.txt") do |io|
12
+ io.each_line do |line|
13
+ puts line unless line =~ /^\s*#/
14
+ end
15
+ end
16
+
17
+ Perhaps you've used the XML {builder}[http://builder.rubyforge.org/] library, which uses nested blocks to match the structure of the XML being generated:
18
+
19
+ builder = Builder::XmlMarkup.new
20
+ builder.page do
21
+ builder.element1('hello')
22
+ builder.element2('world')
23
+ builder.collection do
24
+ builder.interior do
25
+ builder.element3('foo')
26
+ end
27
+ end
28
+ end
29
+
30
+ The {Markaby}[http://code.whytheluckystiff.net/markaby/] library also uses nested blocks to generate html, but is able to do so more succinctly without requiring you to explicitly reference a builder object:
31
+
32
+ Markaby::Builder.new.html do
33
+ head { title "Boats.com" }
34
+ body do
35
+ h1 "Boats.com has great deals"
36
+ ul do
37
+ li "$49 for a canoe"
38
+ li "$39 for a raft"
39
+ li "$29 for a huge boot that floats and can fit 5 people"
40
+ end
41
+ end
42
+ end
43
+
44
+ Perhaps you've described testing scenarios using {RSpec}[http://rspec.info/], building and documenting test cases using English-sounding commands such as "describe" and "it_should_behave_like":
45
+
46
+ describe Stack do
47
+
48
+ before(:each) do
49
+ @stack = Stack.new
50
+ end
51
+
52
+ describe "(empty)" do
53
+
54
+ it { @stack.should be_empty }
55
+
56
+ it_should_behave_like "non-full Stack"
57
+
58
+ it "should complain when sent #peek" do
59
+ lambda { @stack.peek }.should raise_error(StackUnderflowError)
60
+ end
61
+
62
+ it "should complain when sent #pop" do
63
+ lambda { @stack.pop }.should raise_error(StackUnderflowError)
64
+ end
65
+
66
+ end
67
+
68
+ # etc...
69
+
70
+ Perhaps you were introduced to Ruby via the {Rails}[http://www.rubyonrails.org/] framework, which sets up configuration via blocks:
71
+
72
+ ActionController::Routing::Routes.draw do |map|
73
+ map.connect ':controller/:action/:id'
74
+ map.connect ':controller/:action/:page/:format'
75
+ # etc...
76
+ end
77
+
78
+ Rails::Initializer.run do |config|
79
+ config.time_zone = 'UTC'
80
+ config.log_level = :debug
81
+ # etc...
82
+ end
83
+
84
+ Blocks are central to Ruby as a language, and it feels natural to Ruby programmers to use them to delimit specialized code. When designing an API for a Ruby library, blocks like these are, in many cases, a natural and effective pattern.
85
+
86
+ === Defining DSL blocks
87
+
88
+ Blocks in Ruby are used for a variety of purposes. In many cases, they are used to provide _callbacks_, specifying functionality to inject into an operation. If you come from a functional programming background, you might see them as lambda expressions; in object-oriented-speak, they implement the Visitor pattern. A simple example is the +each+ method, which iterates over a collection, using the given block as a callback that allows the caller to specify processing to perform on each element.
89
+
90
+ When we speak of DSL blocks, we are describing something conceptually and semanticaly different. Rather than looking for a specification of _functionality_, the method wants to provide the caller with a _language_ to _describe_ something. The block merely serves as a space in which to use that language.
91
+
92
+ Consider the Rails Routing example above. The Rails application needs to specify how URLs should be interpreted as commands sent to controllers, and, conversely, how command descriptions should be expressed as URLs. Rails thus defines a language that can be used to describe these mappings. The language uses the "connect" verb, which interprets a string with embedded codes describing the URL's various parts, and optional parameters that specify further details about the mapping.
93
+
94
+ The Rails Initializer illustrates another common pattern: that of using a DSL block to perform extended configuration of the method call. Again, a language is being defined here: certain property names such as "time_zone" have meanings understood by the Rails framework.
95
+
96
+ Note that in both this case and the Routing case, the information contained in the block is descriptive. It is possible to imagine a syntax in which all the necessary information is passed into the method (<tt>Routes#draw</tt> or <tt>Initializer#run</tt>) as parameters, perhaps as a large hash or other complex data structure. However, in many cases, providing this information via a block-based language makes the code much more readable.
97
+
98
+ The RSpec example illustrates a more sophisticated case with many keywords and multiple levels of blocks, but it shares common features with the Rails examples. Again, a language is being defined to describe things that could conceivably have been passed in as parameters, but are being specified in a block for clarity and readability.
99
+
100
+ Based on this discussion, we can see that DSL blocks have the following properties:
101
+
102
+ * An API requires a caller to communicate complex descriptive information.
103
+ * The API defines a domain-specific language designed to express this information.
104
+ * A method accepts a block from the caller, and executes the block exactly once.
105
+ * The domain-specific language is available to the caller lexically within the block.
106
+
107
+ As far as I have been able to determine, the term "DSL block" originated in 2007 with a {blog post}[http://blog.8thlight.com/articles/2007/05/20/] by Micah Martin. In it, he describes a way to implement certain types of DSL blocks using <tt>instance_eval</tt>, calling the technique the "DSL Block Pattern". We will discuss the nuances of the <tt>instance_eval</tt> implementation in greater detail below. But first, let us ease into the implementation discussion by describing a simple strategy that has worked very well for many libraries, including Rails.
108
+
109
+ === Implementation strategy 1: block parameters
110
+
111
+ In 2006, Jamis Buck, one of the Rails core developers, posted a set of articles describing the Rails routing implementation. Tucked away at the top the {first article}[http://weblog.jamisbuck.org/2006/10/2/under-the-hood-rails-routing-dsl] is a code snippet showing the DSL block implementation for Rails routing. This code, along with some of its context in the file <tt>action_controller/routing/route_set.rb</tt> (from Rails version 2.1.1), is listed below.
112
+
113
+ class RouteSet
114
+
115
+ class Mapper
116
+ def initialize(set)
117
+ @set = set
118
+ end
119
+
120
+ def connect(path, options = {})
121
+ @set.add_route(path, options)
122
+ end
123
+ # ...
124
+ end
125
+
126
+ # ...
127
+
128
+ def draw
129
+ clear!
130
+ yield Mapper.new(self)
131
+ named_routes.install
132
+ end
133
+
134
+ # ...
135
+
136
+ def add_route(path, options = {})
137
+ # ...
138
+
139
+ Recall how we specify routes in Rails: we call the +draw+ method, and pass it a block. The block receives a parameter that we call "+map+". We can then create routes by calling the +connect+ method on the parameter, as follows:
140
+
141
+ ActionController::Routing::Routes.draw do |map|
142
+ map.connect ':controller/:action/:id'
143
+ map.connect ':controller/:action/:page/:format'
144
+ # etc.
145
+ end
146
+
147
+ It should be fairly easy to see how the code above accomplishes this. The +draw+ method creates an object of class +Mapper+. The +Mapper+ class defines the domain-specific language, in particular the +connect+ method that we are familiar with. Note how its implementation is simply to proxy calls into the routing system: it keeps an instance variable called "<tt>@set</tt>" that points back at the +RouteSet+ we are modifying. Then, +draw+ yields the mapper instance back to the block, where we receive it as our +map+ variable.
148
+
149
+ A large number of DSL block implementations are variations on this theme. We define a proxy class (+Mapper+ in this case) that exposes the domain-specific language we want and communicates back to the system we are describing. We then yield an instance of that proxy back to the block, which receives it as a parameter. The block then manipulates the DSL using its parameter.
150
+
151
+ This pattern is extremely powerful and pervasive. It is simple and clean to implement, and straightforward to use by the caller. The caller knows exactly when it is interacting with the DSL: when it calls methods on the block parameter.
152
+
153
+ However, some have argued that it is too verbose. Why, in a DSL, is it necessary to litter the entire block with references to the block variable? If we know that the caller is supposed to be interacting with the DSL in the block, is it really necessary to have the explicit parameter? Perhaps Rails routing, for example, could be specified more succinctly like the following, in which the +map+ variable is implied.
154
+
155
+ ActionController::Routing::Routes.draw do
156
+ connect ':controller/:action/:id'
157
+ connect ':controller/:action/:page/:format'
158
+ # etc.
159
+ end
160
+
161
+ In the next section we will look more closely at the pros and cons of this alternate syntax. But first, let us summarize our discussion of the "block parameter" implementation.
162
+
163
+ *Implementation*:
164
+
165
+ * Create a proxy class defining the DSL.
166
+ * Yield the proxy object to the block as a parameter.
167
+
168
+ *Pros*:
169
+
170
+ * Easy to implement.
171
+ * Clear syntax for the caller.
172
+ * Clear separation between the DSL and surrounding code.
173
+
174
+ *Cons*:
175
+
176
+ * Requires a block parameter, sometimes resulting in verbose or clumsy syntax.
177
+
178
+ <b>Use it when</b>: you want a simple, effective DSL block and don't mind requiring a parameter.
179
+
180
+ === The parameterless block syntax
181
+
182
+ Much of the recent discussion surrounding DSL blocks originates from a desire to eliminate the block parameter. A domain-specific _language_, it is reasoned, should be as natural and concise as possible, and should not be tied down to the syntax of method invocation. In many cases, eliminating the block parameter would have an enormous impact on the readability of a DSL block. One common example is the case of nested blocks, which, because of Ruby 1.8's scoping semantics, require different variable and parameter names. Consider an imaginary DSL block that looks like this:
183
+
184
+ create_container do |container|
185
+ container.create_subcontainer do |subcontainer1|
186
+ subcontainer1.create_subcontainer do |subcontainer2|
187
+ subcontainer2.create_object do |objconfig|
188
+ objconfig.set_value(3)
189
+ end
190
+ end
191
+ subcontainer1.create_subcontainer do |subcontainer3|
192
+ subcontainer3.create_object do |objconfig2|
193
+ objconfig2.set_value(1)
194
+ end
195
+ end
196
+ end
197
+ end
198
+
199
+ That was clunky. Wouldn't it be nice to instead see this?...
200
+
201
+ create_container do
202
+ create_subcontainer do
203
+ create_subcontainer do
204
+ create_object do
205
+ set_value(3)
206
+ end
207
+ end
208
+ create_subcontainer do
209
+ create_object do
210
+ set_value(1)
211
+ end
212
+ end
213
+ end
214
+ end
215
+
216
+ While this appears to be an improvement, it does come at a cost. First, certain method names become syntactically unavailable when you eliminate the method call syntax. Consider, for example, this simple DSL proxy object that uses <tt>attr_writer</tt>...
217
+
218
+ class ConfigMethods
219
+ attr_writer :author
220
+ attr_writer :title
221
+ end
222
+
223
+ You might interact with it in a DSL block that uses parameters, like so:
224
+
225
+ create_paper do |config|
226
+ config.author = "Daniel Azuma"
227
+ config.title = "Implementing DSL Blocks"
228
+ end
229
+
230
+ However, if you try to eliminate the block parameter, you run into this dilemma:
231
+
232
+ create_paper do
233
+ author = "Daniel Azuma" # Whoops! These no longer work because they
234
+ title = "Implementing DSL Blocks" # look like local variable assignments!
235
+ end
236
+
237
+ If you want to retain the <tt>attr_writer</tt> syntax, you must make it clear to the Ruby parser that you are invoking a method call. For example:
238
+
239
+ create_paper do
240
+ self.author = "Daniel Azuma" # These are now clearly method calls
241
+ self.title = "Implementing DSL Blocks"
242
+ end
243
+
244
+ Unfortunately, this negates some of the benefit of removing the block parameter in the first place. A similar syntactic issue occurs with many operators, notably <tt>[]=</tt>.
245
+
246
+ Second, and more importantly, by eliminating the block parameter, we eliminate the primary means of distinguishing which methods belong to the DSL, and which methods do not. For example, in our routing example, if we eliminate the parameter, like so:
247
+
248
+ ActionController::Routing::Routes.draw do
249
+ connect ':controller/:action/:id'
250
+ connect ':controller/:action/:page/:format'
251
+ # etc.
252
+ end
253
+
254
+ ...we now _assume_ that the +connect+ method is part of the DSL, but that is no longer explicit in the syntax. If, +connect+ also happens to be a method of whatever object was +self+ in the context of the block, which method should be called? There is a method lookup ambiguity inherent to the syntax itself, and, as we shall see, different implementations of parameterless blocks will resolve this ambiguity in different, and sometimes confusing, ways.
255
+
256
+ Despite the above caveats inherent to the syntax, the desire to eliminate the block parameter is quite strong. Let's consider how it can be done.
257
+
258
+ === Implementation strategy 2: instance_eval
259
+
260
+ Micah Martin's {blog post}[http://blog.8thlight.com/articles/2007/05/20/] describes an implementation strategy that does not require the block to take a parameter. He suggests using a powerful, if sometimes confusing, Ruby metaprogramming tool called <tt>instance_eval</tt>. This method, defined on the +Object+ class so it is available to every object, has a simple function: it executes a block given it, but does so with the +self+ reference redirected to the receiver. Hence, within the block, calling a method, or accessing an instance variable or class variable, (or, in Ruby 1.9, accessing a constant), will begin the lookup process at a different place.
261
+
262
+ It is perhaps instructive to see an example. Let's create a simple class
263
+
264
+ Class MyClass
265
+ def initialize
266
+ @instvar = 1
267
+ end
268
+ def foo
269
+ puts "in foo: var=#{@instvar}"
270
+ end
271
+ end
272
+
273
+ Things to note here is that the method +foo+ and the instance variable <tt>@instvar</tt> are defined on instances of +MyClass+. Now let's <tt>instance_eval</tt> an instance of +MyClass+ from another class.
274
+
275
+ class Tester
276
+ def test
277
+ puts @instvar.inspect # prints "nil" since the Tester object has no @instvar
278
+ x = MyClass.new # create a new instance of MyClass
279
+ x.instance_eval do # change self to point to x during the block
280
+ puts @instvar.inspect # prints "1" since self now points at x
281
+ @instvar = 2 # changes x's @instvar to 2
282
+ foo # calls x's foo and prints "in foo: var=2"
283
+ puts x == self # prints "true". The local variable x is still accessible
284
+ end # end of the block. self is now back to the Tester instance
285
+ puts x == self # prints "false"
286
+ puts @instvar.inspect # prints "nil" since Tester still has no @instvar
287
+ foo # NameError since Tester has no foo method.
288
+ end
289
+ end
290
+ Tester.new.test # Runs the above test
291
+
292
+ How does this help us? Notice that within the <tt>instance_eval</tt> block, the methods of +x+ can be called without explicitly naming +x+ because the +self+ reference points to +x+. So in the Rails Routing example, if we used <tt>instance_eval</tt> to get +self+ to point to the +Mapper+ instance in the block, then we wouldn't need to pass it explicitly as a parameter, and the block could call methods on it without explicitly naming it.
293
+
294
+ Here is a revised version of the Rails routing code:
295
+
296
+ class RouteSet
297
+
298
+ class Mapper
299
+ def initialize(set)
300
+ @set = set
301
+ end
302
+
303
+ def connect(path, options = {})
304
+ @set.add_route(path, options)
305
+ end
306
+ # ...
307
+ end
308
+
309
+ # ...
310
+
311
+ # We need to pass the block itself to instance_eval, so get it
312
+ # as a parameter to the draw method.
313
+ def draw(&block)
314
+ clear!
315
+ map = Mapper.new(self) # Create the proxy object as before
316
+ map.instance_eval(&block) # Call the block, setting self to point to map.
317
+ named_routes.install
318
+ end
319
+
320
+ # ...
321
+
322
+ def add_route(path, options = {})
323
+ # ...
324
+
325
+ This modified version of the routing API now no longer requires a block parameter, and the DSL is correspondingly more succinct. Sounds like a win all around, right?
326
+
327
+ Well, not so fast. Our implementation here has a number of subtle and surprising side effects. Suppose, for instance, we were to write a little helper method to help us generate URLs:
328
+
329
+ def makeurl(*params)
330
+ 'mywebsite/:controller/:action/' + params.map{ |e| e.inspect }.join('/')
331
+ end
332
+
333
+ Using the above method, it becomes easy to generate URL strings:
334
+
335
+ makeurl(:id, :style) # --> "mywebsite/:controller/:action/:id/:style"
336
+
337
+ Our <tt>routes.rb</tt> file, utilizing our "improvement" to the routing DSL, might now like this:
338
+
339
+ def makeurl(*params)
340
+ 'mywebsite/:controller/:action/' + params.map{ |e| e.inspect }.join('/')
341
+ end
342
+
343
+ ActionController::Routing::Routes.draw do
344
+ connect makeurl :id
345
+ connect makeurl :page, :format
346
+ # etc.
347
+ end
348
+
349
+ Looks nice, right? Except that when we try to run it, we get:
350
+
351
+ NoMethodError: undefined method `[]' for :id:Symbol
352
+ from /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/routing/builder.rb:168:in `build'
353
+ from /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/routing/route_set.rb:261:in `add_route'
354
+ ...
355
+
356
+ What's up with that cryptic error? After some furious digging into the guts of Rails, we discover to our surprise Ruby is trying to call +makeurl+ on the <em>+Mapper+</em> object, rather than calling our +makeurl+ helper method. And then it dawns on us. We used <tt>instance_eval</tt> to change +self+ to point to the +Mapper+ proxy inside the block, and it did exactly what we asked. It let us call the +connect+ method on the +Mapper+ without having to pass it in as a block parameter. But it similarly also tried to call +makeurl+ on the +Mapper+. The helper method we so cleverly wrote is being bypassed.
357
+
358
+ The problem gets worse. Changing +self+ affects not only how methods are looked up, but also how instance variables are looked up. For example, we are now able to do this:
359
+
360
+ ActionController::Routing::Routes.draw do
361
+ @set = nil
362
+ connect ':controller/:action/:id' # Exception raised here!
363
+ connect ':controller/:action/:page/:format'
364
+ # etc.
365
+ end
366
+
367
+ What happened? If we recall, <tt>@set</tt> is used by the +Mapper+ object to point back to the routing +RouteSet+. It is how the proxy knows what it is proxying for. But since we've used <tt>instance_eval</tt>, we now have free access to the +Mapper+ object's internal instance variables, including the ability to clobber them. And that's precisely what we did here. Furthermore, maybe we were actually expecting to access our own <tt>@set</tt> variable, and we haven't done that. Any instance variables from the caller's closure are in fact no longer accessible inside the block.
368
+
369
+ The problem gets even worse. If we think about the cryptic error message we got when we tried to use our +makeurl+ helper method, we begin to realize that we've run into the method lookup ambiguity discussed in the previous section. If +self+ has changed inside the block, and we tried to call +makeurl+, we might expect a +NoMethodError+ to be raised for +makeurl+ on the +Mapper+ class, rather than for "<tt>[]</tt>" on the +Symbol+ class. However, things change when we recall that Rails's routing DSL supports named routes. You do not have to call the specific +connect+ method to create a route. In fact, you can call _any_ method name. Any name is a valid DSL method name. It is thus ambiguous, when we invoke +makeurl+, whether we mean our helper method or a named route called "makeurl". Rails assumed we meant the named route, but in fact that isn't what we had intended.
370
+
371
+ This all sounds pretty bad. Do we give up on <tt>instance_eval</tt>? Some members of the Ruby community have, and indeed the technique has generally fallen out of favor in many major libraries. Jim Weirich, for instance, {originally}[http://onestepback.org/index.cgi/Tech/Ruby/BuilderObjects.rdoc] utilized <tt>instance_eval</tt> in the XML Builder library illustrated earlier, but later deprecated and removed it because of its surprising behavior. Why's {Markaby}[http://code.whytheluckystiff.net/markaby/] still uses <tt>instance_eval</tt> but includes a caveat in the {documentation}[http://markaby.rubyforge.org/] explaining the issues and recommending caution.
372
+
373
+ There are, however, a few specific cases when <tt>instance_eval</tt> may be uniquely appropriate. RSpec's DSL is intended as a class-constructive language: it constructs ruby classes behind the scenes. In the RSpec example at the beginning of this paper, you may notice the use of the <tt>@stack</tt> instance variable. In fact, this is intended as an instance variable of the RSpec test story being written, and as such, <tt>instance_eval</tt> is required because of the kind of language that RSpec wants to use. But in more common cases, such as specifying configuration, <tt>instance_eval</tt> does not give us the most desirable behavior. The general consensus now, expressed for example in recent articles from {Why}[http://hackety.org/2008/10/06/mixingOurWayOutOfInstanceEval.html] and {Ola Bini}[http://olabini.com/blog/2008/09/dont-overuse-instance_eval-and-instance_exec/], is that it should be avoided.
374
+
375
+ So does this mean we're stuck with block parameters for better or worse? Not quite. Several alternatives have been proposed recently, and we'll take a look at them in the next few sections. But first, let's summarize the discussion of <tt>instance_eval</tt>.
376
+
377
+ *Implementation*:
378
+
379
+ * Create a proxy class defining the DSL.
380
+ * Use <tt>instance_eval</tt> to change +self+ to the proxy in the block.
381
+
382
+ *Pros*:
383
+
384
+ * Easy to implement.
385
+ * Concise: does not require a block parameter.
386
+ * Useful for class-constructive DSLs.
387
+
388
+ *Cons*:
389
+
390
+ * Surprising lookup behavior for helper methods.
391
+ * Surprising lookup behavior for instance variables.
392
+ * Breaks encapuslation of the proxy class.
393
+ * Encounters the helper method vs DSL method ambiguity.
394
+
395
+ <b>Use it when</b>: you are writing a DSL that constructs classes or modifies class internals.
396
+
397
+ === Implementation strategy 3: delegation
398
+
399
+ In our discussion of <tt>instance_eval</tt>, a major problem we identified is that helper methods, and indeed all other methods from the calling context, are not available within the block. One way to improve the situation, perhaps, is by redirecting any methods not defined in the DSL (that is, not defined on the proxy object) back to the original context. That way, we still have access to our helper methods--they'll appear to be part of the DSL. This "delegation" approach was proposed by Dan Manges in his {blog}[http://www.dcmanges.com/blog/ruby-dsls-instance-eval-with-delegation].
400
+
401
+ The basic implementation here is not difficult, if we pull out another tool from Ruby's metaprogramming toolbox, <tt>method_missing</tt>. This method is called whenever you call a method that is not explicitly defined on an object's class. It provides a "last ditch" opportunity to handle the method before Ruby bails with a dreaded +NoMethodError+. Again, an example is probably useful here.
402
+
403
+ class MyClass
404
+ def foo
405
+ puts "in foo"
406
+ end
407
+ def method_missing(name, *params)
408
+ puts "last ditch method #{name.inspect} called with params: #{params.inspect}"
409
+ end
410
+ end
411
+
412
+ x = MyClass.new
413
+ x.foo # prints "in foo"
414
+ x.bar # prints "last ditch method :bar called with params: []"
415
+ x.baz(1,2) # prints "last ditch method :baz called with params: [1,2]"
416
+
417
+ How does this help us? Well, our goal is to redirect any calls that aren't available in the DSL, back to the block's original context. To do that, we simply define <tt>method_missing</tt> on our proxy class. In that method, we delegate the call, using +send+, back to the original +self+ from the block's context.
418
+
419
+ The remaining trick is how to get the block's original +self+. This can be done with a little bit of hackery if we realize that any +Proc+ object lets you access the binding of the context where it came from. We can get the original +self+ reference by eval-ing "self" in that binding.
420
+
421
+ Going back to our modification of the Rails routing code, let's see what this looks like.
422
+
423
+ class RouteSet
424
+
425
+ class Mapper
426
+ # We save the block's original "self" reference also, so that we
427
+ # can redirect unhandled methods back to the original context.
428
+ def initialize(set, original_self)
429
+ @set = set
430
+ @original_self = original_self
431
+ end
432
+
433
+ def connect(path, options = {})
434
+ @set.add_route(path, options)
435
+ end
436
+
437
+ # ...
438
+
439
+ # Redirect all other methods
440
+ def method_missing(name, *params, &blk)
441
+ @original_self.send(name, *params, &blk)
442
+ end
443
+ end
444
+
445
+ # ...
446
+
447
+ def draw(&block)
448
+ clear!
449
+ original_self = Kernel.eval('self', block.binding) # Get block's context self
450
+ map = Mapper.new(self, original_self) # Give it to the proxy
451
+ map.instance_eval(&block)
452
+ named_routes.install
453
+ end
454
+
455
+ # ...
456
+
457
+ def add_route(path, options = {})
458
+ # ...
459
+
460
+ Now people familiar with how Rails is implemented will probably object that +Mapper+ already _has_ a <tt>method_missing</tt> defined. It's used to implement the named routes that caused the ambiguity we described earlier. We have not solved that ambiguity: by replacing Rails's <tt>method_missing</tt> with my own <tt>method_missing</tt>, I effectively disable named routes. Granted, I'm ignoring that issue right now, and just trying to illustrate how method delegation works. As long as we don't use named routes, our +makeurl+ example will now work as we expect:
461
+
462
+ def makeurl(*params)
463
+ 'mywebsite/:controller/:action/' + params.map{ |e| e.inspect }.join('/')
464
+ end
465
+
466
+ ActionController::Routing::Routes.draw do
467
+ connect makeurl :id
468
+ connect makeurl :page, :format
469
+ # etc.
470
+ end
471
+
472
+ While this would appear to have solved the helper method issue, so far it does nothing to address the other issues we encountered. For example, invoking instance variables inside the block will still reference the instance variables of the +Mapper+ proxy object. By using <tt>instance_eval</tt>, we still break encapsulation of the proxy class, and lose access to any instance variables from the block's context.
473
+
474
+ Addressing the instance variable issue is not as straightforward as delegating method calls. There is, as far as I know, no direct way to delegate instance variable lookup, and Manges's blog posting does not attempt to provide a solution either. However, we can imagine a few techniques to mitigate the problem. First, we could eliminate the proxy object's dependence on instance variables altogether, by replacing them with a global hash. In our example, instead of keeping a reference to the +RouteSet+ as an instance variable of +Mapper+, we can maintain a global hash that looks up the +RouteSet+ using the +Mapper+ instance as the key. In this way, we eliminate the risk of the block clobbering the proxy's state, and minimize the problem of breaking encapsulation of the proxy object.
475
+
476
+ Second, we could make instance variables from the block's context partially available through a "pull-push" technique using <tt>instance_variable_set</tt> and <tt>instance_variable_get</tt> calls. Before calling the block, we "pull" in the block context object's instance variables, by iterating over them and setting the same instance variables on the proxy object. Then those instance variables will appear to be still available during the block. On completing the block, we then "push" any changes back to the block context object, by iterating over the proxy's instance variables and setting them on the block context object.
477
+
478
+ Here is a sample implementation of these two techniques for handling instance variables:
479
+
480
+ class RouteSet
481
+
482
+ class Mapper
483
+
484
+ @@routeset_map = Hash.new # Global hashes to replace
485
+ @@original_self_map = Hash.new # Mapper's instance variables
486
+
487
+ def initialize(set, original_self)
488
+ @@routeset_map[self] = set # Add me to global hashes
489
+ @@original_self_map[self] = original_self
490
+ original_self.instance_variables.each do |name| # "pull" instance variables
491
+ instance_variable_set(name, original_self.instance_variable_get(name))
492
+ end
493
+ end
494
+
495
+ def cleanup
496
+ @@routeset_map.delete(self) # Remove from global hashes
497
+ original_self = @@original_self_map.delete(self)
498
+ instance_variables.each do |name| # "push" instance variables
499
+ original_self.instance_variable_set(name, instance_variable_get(name))
500
+ end
501
+ end
502
+
503
+ def connect(path, options = {})
504
+ @@routeset_map[self].add_route(path, options) # Lookup set from global hash
505
+ end
506
+
507
+ # ...
508
+
509
+ def method_missing(name, *params, &blk) # Lookup original self
510
+ @@original_self_map[self].send(name, *params, &blk) # from global hash
511
+ end
512
+ end
513
+
514
+ # ...
515
+
516
+ def draw(&block)
517
+ clear!
518
+ original_self = Kernel.eval('self', block.binding)
519
+ map = Mapper.new(self, original_self)
520
+ begin
521
+ map.instance_eval(&block)
522
+ ensure # Ensure the hashes are cleaned up and instance
523
+ map.cleanup # variables are pushed back to original_self,
524
+ end # even if the block threw an exception
525
+ named_routes.install
526
+ end
527
+
528
+ # ...
529
+
530
+ def add_route(path, options = {})
531
+ # ...
532
+
533
+ While these measures seem to handle most of the cases, the implementation is getting more complex, and includes the additional overhead of hash lookups and copying of instance variables. More significantly, the "pull-push" technique does not quite preserve the expected semantics of instance variables. For instance, if you change an instance variable's value inside the block, it will get "pushed" back to the context object after the block is completed, but until then, the context object will not know about the change. So if, in the meantime, you called a helper method that relies on that instance variable, you will get the old value, and this can result in confusion. Using global hashes might be an effective means of protecting the proxy object's internals from the block. However, I find the "pull-push" technique to delegate instance variables to be of questionable value.
534
+
535
+ Several variations on the delegation theme have been proposed. One such variation uses a technique proposed by Jim Weirich called {MethodDirector}[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/19153]. In this variation, we create a small object whose sole purpose is to receive methods and delegate them to whatever object it thinks should handle them. Utilizing Jim's +MethodDirector+ implementation rather than adding a <tt>method_missing</tt> to our +Mapper+ proxy, we could rewrite the +draw+ method as follows:
536
+
537
+ def draw(&block)
538
+ clear!
539
+ original_self = Kernel.eval('self', block.binding) # Get the block's context self
540
+ map = Mapper.new(self) # Get the proxy
541
+ director = MethodDirector.new([map, original_self]) # Create a director
542
+ director.instance_eval(&block) # Use the director as self
543
+ named_routes.install
544
+ end
545
+
546
+ The upshot is not much different from Manges's delegation technique. Method calls get delegated in approximately the same way (though Weirich speculates that +MethodDirector+'s dispatch process may be slow). Within the block, +self+ now points to the +MethodDirector+ object rather than the +Mapper+ object. This means that we're no longer breaking encapsulation of the mapper proxy (but we are breaking the encapsulation of the +MethodDirector+ itself.) We still cannot access instance variables from the block's context. We no longer clobber +Mapper+'s instance variables, but now we can clobber +MethodDirector+'s. In short, it might be considered a slight improvement, but not much, at a possible performance cost.
547
+
548
+ Let's wrap up our discussion of delegation and then delve into an entirely different approach.
549
+
550
+ *Implementation*:
551
+
552
+ * Create a proxy class defining the DSL.
553
+ * Use <tt>method_missing</tt> to delegate unhandled methods back to the block's context.
554
+ * Use <tt>instance_eval</tt> to change +self+ to the proxy in the block.
555
+
556
+ *Pros*:
557
+
558
+ * Concise: does not require a block parameter.
559
+ * Better than a straight <tt>instance_eval</tt> in that it handles helper methods.
560
+
561
+ *Cons*:
562
+
563
+ * No complete way to eliminate the surprising lookup behavior for instance variables.
564
+ * Does not solve the helper method vs DSL method ambiguity.
565
+ * Harder to implement than a simple <tt>instance_eval</tt>.
566
+
567
+ <b>Use it when</b>: you have a case where <tt>instance_eval</tt> is appropriate (i.e. if you are writing a DSL that constructs classes or modifies class internals) but you want to retain helper methods.
568
+
569
+ === Implementation strategy 4: arity detection
570
+
571
+ Intrigued by the discussion surrounding <tt>instance_eval</tt> and DSL blocks, James Edward Gray II (of {RubyQuiz}[http://rubyquiz.com/] fame) chimed in with a compromise. In his {blog}[http://blog.grayproductions.net/articles/dsl_block_styles], he argues that the the issue boils down to two basic strategies: block parameters and <tt>instance_eval</tt>, both of which have their own strengths and weaknesses. On one hand, block parameters avoid surprising behavior and ambiguity in exchange for somewhat more verbose syntax. On the other hand, <tt>instance_eval</tt> offers a more concise and perhaps more pleasing syntax in exchange for some ambiguity and surprising side effects. Neither solution is clearly better than the other, and either might be more appropriate in different circumstances. Thus, why not let the _caller_ decide which one to use?
572
+
573
+ This is in fact easier to do than we might think. When you call a method using a DSL block, you've already make the choice to have your block take a parameter or not. The caller does one of the following:
574
+
575
+ ActionController::Routing::Routes.draw do |map|
576
+ map.connect ':controller/:action/:id'
577
+ map.connect ':controller/:action/:page/:format'
578
+ # etc.
579
+ end
580
+
581
+ or
582
+
583
+ ActionController::Routing::Routes.draw do
584
+ connect ':controller/:action/:id'
585
+ connect ':controller/:action/:page/:format'
586
+ # etc.
587
+ end
588
+
589
+ It is possible for the method itself to detect which case it is, just by examining the block. Every +Proc+ object provides a method called +arity+, which returns a notion of how many parameters the block expects. If you receive a block that expects a parameter, use the block parameter strategy; if you receive a block that doesn't expect a parmaeter, use <tt>instance_eval</tt> or one of its modifications. Under this technique, our Routing +draw+ method might look like this:
590
+
591
+ def draw(&block)
592
+ clear!
593
+ map = Mapper.new(self) # Create the proxy object as before
594
+ if block.arity == 1
595
+ block.call(map) # Block takes one parameter: use block parameter technique
596
+ else
597
+ map.instance_eval(&block) # otherwise, use instance_eval technique.
598
+ end
599
+ named_routes.install
600
+ end
601
+
602
+ Gray's proposal has a compelling advantage. The basis for the entire discussion is the suggestion that eliminating block parameters is desirable for the caller, and the objections raised are also, almost without exception, based on the experience of the caller. The basic question is thus whether the _caller_ ought to consider the benefits of eliminating block parameters to outweigh the costs. Therefore, it makes sense to put that choice in the hands of the caller rather than letting the library API designer dictate one choice or the other.
603
+
604
+ For example, one apparently inherent issue with a DSL block style that eliminates block parameters is the ambiguity between DSL methods and helper methods. By giving the caller the choice, we at once solve the ambiguity by providing a language for it. If the caller does not need to distinguish between the two, because she is not using helper methods or named routes, then she can choose to omit the block parameter and use <tt>instance_eval</tt> without harm. If, on the other hand, she _does_ need to distinguish between the two, as in the case of Rails routing where any method name could be a DSL method because of the named routes feature, then she can choose to make the block parameter explicit.
605
+
606
+ There is, however, a subtle disadvantage to providing the choice. By effectively allowing two DSL styles, a library that offers Gray's choice dilutes the identity and "branding" of its DSL. If there are two "dialects" of the DSL, one that uses a block parameter and one that does not, it becomes harder for programmers to recognize the language. The two dialects might develop separate followings and distinct "best-practices" on account of their syntactic differences, and the schism would diminish the overall power of the DSL. While the actual cost of this diluting effect can be difficult to measure, it cannot be ignored, because the whole point of defining a DSL is to make code more understandable and recognizable.
607
+
608
+ Finally, there are some cases when one choice is specifically called for by the nature of the DSL being implemented. RSpec is a good example: it requires <tt>instance_eval</tt> in order to support access to the test story's instance variables. Allowing the caller to choose would not make sense in this case.
609
+
610
+ Let us summarize Gray's arity detection technique, and then proceed to an interesting new idea recently proposed by Why The Lucky Stiff.
611
+
612
+ *Implementation*:
613
+
614
+ * Create a proxy class defining the DSL.
615
+ * Detect the choice of the caller based on block arity.
616
+ * Use either a block parameter or <tt>instance_eval</tt> to invoke the block.
617
+
618
+ *Pros*:
619
+
620
+ * Gives the caller the ability to choose which syntax works best.
621
+ * Solves method lookup ambiguity.
622
+ * Implementation cost is not significant.
623
+
624
+ *Cons*:
625
+
626
+ * Not an all-encompassing solution-- either choice still has its own pros and cons.
627
+ * Possibility of dilution of DSL branding.
628
+
629
+ <b>Use it when</b>: it is not clear whether block parameters or <tt>instance_eval</tt> is better, or if you need a way to mitigate the method lookup ambiguity.
630
+
631
+ === Implementation strategy 5: mixins
632
+
633
+ One of the most interesting entries into the DSL blocks discussion was proposed by Why The Lucky Stiff in his {blog}[http://hackety.org/2008/10/06/mixingOurWayOutOfInstanceEval.html]. Why observes that the problem with <tt>instance_eval</tt> is that it does too much. Most DSL blocks merely want to be able to intercept and respond to certain method calls, whereas <tt>instance_eval</tt> actually changes +self+, which has the additional side effects of blocking access to other methods and instance variables, and breaking encapsulation. A better solution, he maintains, is not to change +self+, but instead temporarily to add the DSL's methods to the block's context for the duration of the block. That is, instead of having the DSL proxy object delegate back to the block's context object, do the opposite: cause the block's context object to delegate to the DSL proxy object.
634
+
635
+ Implementing this is actually harder than it sounds. We need to take the block context object, dynamically add methods to it before calling the block, and then dynamically remove them afterward. We already know how to get the block context object, but adding and removing methods requires some more Ruby metaprogramming wizardry. And now we're stretching our toolbox to the breaking point.
636
+
637
+ Ruby provides tools for dynamically defining methods on and removing methods from an existing module. We might be tempted to try something like this:
638
+
639
+ def draw(&block)
640
+ clear!
641
+ save_self = self
642
+ original_self = Kernel.eval('self', block.binding)
643
+ original_self.class.module_eval do
644
+ define_method(:connect) do |path,options|
645
+ save_self.add_route(path,options)
646
+ end
647
+ end
648
+ yield
649
+ original_self.class.module_eval do
650
+ remove_method(:connect)
651
+ end
652
+ named_routes.install
653
+ end
654
+
655
+ This implementation, however, is fraught with problems. Notably, we are modifying the entire class of objects, including instances other than <tt>original_self</tt>, which is probably not what we intended. In addition, we could be unknowingly clobbering another +connect+ method defined on <tt>original_self</tt>'s class. (There are, of course, many other problems that I'm just ignoring for the sake of clarity, such as exception safety, and the fact that the +options+ parameter cannot take a default value when using <tt>define_method</tt>. Suffice to say that the above implementation is quite broken.)
656
+
657
+ What we would really like is a way to add methods to just one object temporarily, and then remove them, restoring the original state (including any methods we may have overridden when we added ours.) Ruby _almost_ provides a reasonable way to do this, using the +extend+ method. This method lets you add a module's methods to a single specific object, like this:
658
+
659
+ module MyExtension
660
+ def foo
661
+ puts "foo called"
662
+ end
663
+ end
664
+
665
+ s1 = 'hello'
666
+ s2 = 'world'
667
+ s1.extend(MyExtension) # adds the "foo" method only to object s1,
668
+ # not to the entire string class.
669
+ s1.foo # prints "foo called"
670
+ s2.foo # NameError: s2 is unchanged
671
+
672
+ Unfortunately, there is no way to remove the module from the object. Ruby has no "unextend" capability. This omission led Why to implement it himself as a Ruby language extension called {Mixico}[http://github.com/why/mixico/tree/master]. The name comes from the library's ability to add and remove "mixins" at will. A similar library exists as a gem called {Mixology}[http://www.somethingnimble.com/bliki/mixology]. The two libraries use different APIs but perform the same basic function. For the discussion below, I will assume Mixico is installed. However, the library I describe in the next section uses a custom implementation that is compatible with MRI 1.9 and JRuby.
673
+
674
+ Using Mixico, we can now write the +draw+ method like this:
675
+
676
+ def draw(&block)
677
+ clear!
678
+ Module.mix_eval(MapperModule, &block)
679
+ named_routes.install
680
+ end
681
+
682
+ Wow! That was simple. Mixico even handles all the eval-block-binding hackery for us. But the simplicity is a little deceptive: when we want to do a robust implementation, we run into two issues. First, we run into a challenge if we want to support multiple DSL blocks being invoked at once: for example in the case of nested blocks or multithreading. It is possible in such cases that a MapperModule is already mixed into the block's context. The <tt>mix_eval</tt> method by itself, as of this writing, doesn't handle this case well: the inner invocation will remove the module prematurely. Additional logic is necessary to track how many nested invocations (or invocations from other threads) want to mix-in each particular module into each object.
683
+
684
+ The other challenge is that of creating the +MapperModule+ module, implementing the +connect+ method and any others we want to mix-in. Because we're adding methods to someone else's object, we need to be as unobtrusive as possible, yet we need to provide the necessary functionality, including invoking the <tt>add_route</tt> method back on the +RouteSet+. This is unfortunately not trivial. In particular, we need to give +MapperModule+ a way to reference the +RouteSet+. I'll describe a full implementation of this in the next section, but for now let's explore some possible approaches.
685
+
686
+ Rails's original +Mapper+ proxy class, we recall from our earlier discussion, used an instance variable, <tt>@set</tt>, which pointed back to the +RouteSet+ instance and thus provided a way to invoke <tt>add_route</tt>. One approach could be to add such an instance variable to the block's context object, so it's available in methods of +MapperModule+. This seems to be the easiest approach, but it is also dangerous because it intrudes on the context object, adding an instance variable and potentially clobbering one used by the caller. Furthermore, in the case of nested blocks that try to add methods to the same object, the two blocks may clobber each other's instance variables.
687
+
688
+ Instead of adding information to the block's context object, we could stash the information away in a global location, such as a class variable, that can be accessed by the +MapperModule+ from within the block. This is of course the same strategy we used to eliminate instance variables in the section on delegation. Again, this seems to work, until you have nested or multithreaded usage. It then becomes neccessary to keep a stack of references to handle nesting, and thread-local variables to handle multithreading-- all feasible to do, but a lot of work.
689
+
690
+ A third approach involves dynamically generating a singleton module, "hard coding" a reference to the +RouteSet+ in the module. For example:
691
+
692
+ def draw(&block)
693
+ clear!
694
+ save_self = self
695
+ mapper_module = Module.new
696
+ mapper_module.module_eval do
697
+ define_method(:connect) do |path,options|
698
+ save_self.add_route(path,options)
699
+ end
700
+ end
701
+ Module.mix_eval(mapper_module, &block)
702
+ named_routes.install
703
+ end
704
+
705
+ This probably can be made to work, and it also has the benefit of solving the nesting and multithreading issue neatly since each mixin is done exactly once. However, it seems to be a fairly heavyweight solution: creating a new module for every DSL block invocation may have performance implications. It is also not clear how to support constructs that are not available to <tt>define_method</tt>, such as blocks and parameter default values. However, such an approach may still be useful in certain cases when you need to generate a DSL dynamically based on the context.
706
+
707
+ One more issue with the mixin strategy is that, like all implementations that drop the block parameter, there remains an ambiguity regarding whether methods should be directed to the DSL or to the surrounding context. In the implementations we've discussed previously, based on <tt>instance_eval</tt>, the actual behavior is fairly straightforward to reason about. A simple <tt>instance_eval</tt> disables method calls to the block's context altogether: you can call _only_ the DSL methods. An <tt>instance_eval</tt> with delegation re-enables method calls to the block's context but gives the DSL priority. If both the DSL and the surrounding block define the same method name, the DSL's method will be take precedence.
708
+
709
+ Mixin's behavior is less straightforward, because of a subtlety in Ruby's method lookup behavior. Under most cases, it behaves similarly to an <tt>instance_eval</tt> with delegation: the DSL's methods take priority. However, if methods have been added directly to the object, they will take precedence over the DSL's methods. Following is an example of this case:
710
+
711
+ # Suppose we have a DSL block available, via "call_my_dsl",
712
+ # that implements the methods "foo" and "bar"...
713
+
714
+ # First, let's implement a simple class
715
+ class MyClass
716
+
717
+ # A test method
718
+ def foo
719
+ puts "in foo"
720
+ end
721
+
722
+ end
723
+
724
+ # Create an instance of MyClass
725
+ obj = MyClass.new
726
+
727
+ # Now, add a new method "bar" to the object.
728
+ def obj.bar
729
+ puts "in bar"
730
+ end
731
+
732
+ # Finally, add a method "run" that runs a DSL block
733
+ def obj.run
734
+ call_my_dsl do
735
+ foo # DSL "foo" method takes precedence over MyClass#foo
736
+ bar # The object's "bar" method takes precedence over DSL "bar"
737
+ end
738
+ end
739
+
740
+ # At this point, obj has methods "foo", "bar", and "run"
741
+ # Run the DSL block to test the behavior
742
+ obj.run
743
+
744
+ In the above example, suppose both +foo+ and +bar+ are methods of the DSL. They are also both defined as methods of +obj+. (+foo+ is available because it is a method of +MyClass+, while +bar+ is available because it is explicitly added to +obj+.) However, if you run the code, it calls the DSL's +foo+ but +obj+'s +bar+. Why?
745
+
746
+ The reason points to a subtlety in how Ruby does method lookup. When you define a method in the way +foo+ is defined, it is just added to the class. However, when you define a method in the way +bar+ is defined, it is defined as a "singleton method", and added to the "singleton class", which is an anonymous class that holds methods defined directly on a particular object. It turns out that the singleton class is always given the highest priority in method lookup. So, for example, the lookup order for methods of +obj+ within the block would look like this:
747
+
748
+ singleton methods of obj -> mixin module from the DSL -> methods of MyClass
749
+ (e.g. bar, run) (e.g. foo, bar) (e.g. foo)
750
+
751
+ So when the +foo+ method is called, it is not found in the singleton class, but it is found in the mixin, so the mixin's version is invoked. However, when +bar+ is called, it is found in the singleton class, so that version is invoked in favor of the mixin's version.
752
+
753
+ Does this esoteric-sounding case actually happen in practice? In fact it does, quite frequently: class methods are singleton methods of the class object, so you should beware of this issue when designing a DSL block that will be called from a class method.
754
+
755
+ Well, that was confusing. It is on account of such behavior that we need to take the method lookup ambiguity seriously when dealing with mixins. In fact, I would go so far as to suggest that the mixin implementation should always go hand-in-hand with a way to mitigate that ambiguity, such as Gray's arity check.
756
+
757
+ As we have seen, the mixin idea seems like it may be a compelling solution, particularly in conjunction with Gray's arity check, but the implementation details present some challenges. It may be viable if a library can be written to hide the implementation complexity. Let's summarize this approach, and then proceed to examine such a library, one that uses some of the best of what we've discussed to make implementing DSL blocks simple.
758
+
759
+ *Implementation*:
760
+
761
+ * Install a mixin library such as mixico or mixology (or roll your own if necessary).
762
+ * Define the DSL methods in a module.
763
+ * Mix the module into the block's context before invoking the block, and remove it afterwards.
764
+ * Carefully handle any issues involving nested blocks and multithreading while remaining unobtrusive.
765
+
766
+ *Pros*:
767
+
768
+ * Allows the concise syntax without a block parameter.
769
+ * Doesn't change +self+, thus preserving the right behavior regarding helper methods and instance variables.
770
+
771
+ *Cons*:
772
+
773
+ * Requires an extension to Ruby to implement mixin removal.
774
+ * Implementation is complicated and error-prone.
775
+ * The helper method vs DSL method ambiguity remains, exhibiting surprising behavior in the presence of singleton methods.
776
+
777
+ <b>Use it when</b>: parameterless blocks are desired and the method lookup ambiguity can be mitigated, as long as a library is available to handle the details of the implementation.
778
+
779
+ === Blockenspiel: a comprehensive implementation
780
+
781
+ Some of the implementations we have covered, especially the mixin implementation, have some compelling qualities, but are hampered by the difficulty of implementing them in a robust way. They could be viable if a library were present to handle the details.
782
+
783
+ {Blockenspiel}[http://virtuoso.rubyforge.org/blockenspiel] was written to be that library. It first provides a comprehensive and robust implementation of the mixin strategy, correctly handling nesting and multithreading. It offers the option to perform an arity check, giving the caller the choice of whether or not to use a block parameter. You can even tell blockenspiel to use an alternate implementation, such as <tt>instance_eval</tt>, instead of a mixin, in those cases when it is appropriate. Finally, blockenspiel also provides an API for dynamic construction of DSLs.
784
+
785
+ But most importantly, it is easy to use. To write a basic DSL, just follow the first and easiest implementation strategy, creating a proxy class that can be passed into the block as a parameter. Then instead of yielding the proxy object, pass it to blockenspiel, and it will do the rest.
786
+
787
+ Our Rails routing example implemented using blockenspiel might look like this:
788
+
789
+ class RouteSet
790
+
791
+ class Mapper
792
+ include Blockenspiel::DSL # tell blockenspiel this is a DSL proxy
793
+
794
+ def initialize(set)
795
+ @set = set
796
+ end
797
+
798
+ def connect(path, options = {})
799
+ @set.add_route(path, options)
800
+ end
801
+ # ...
802
+ end
803
+
804
+ # ...
805
+
806
+ def draw(&block)
807
+ clear!
808
+ Blockenspiel.invoke(block, Mapper.new(self)) # blockenspiel does the rest
809
+ named_routes.install
810
+ end
811
+
812
+ # ...
813
+
814
+ def add_route(path, options = {})
815
+ # ...
816
+
817
+ The code above is as simple as a block parameter or <tt>instance_eval</tt> implementation. However, it performs a full-fledged mixin implementation, and even throws in the arity check. We recall from the previous section that one of the chief challenges is to mediate communication between the mixin and proxy in a re-entrant and thread-safe way. The blockenspiel library implements this mediation using a global hash, avoiding the compatibility risk of adding instance variables to the block's context object, and avoiding the performance hit of dynamically generating proxies. All the implementation details are carefully handled behind the scenes.
818
+
819
+ Atop this basic usage, blockenspiel provides two types of customization. First, you can customize the DSL, using a few simple directives to specify which methods on your proxy should be available in the mixin implementation. You can also cause methods to be available in the mixin under different names, thus sidestepping the <tt>attr_writer</tt> issue we discussed earlier. If you want methods of the form "attribute=" on your proxy object, blockenspiel provides a simple syntax for renaming them:
820
+
821
+ class ConfigMethods
822
+ include Blockenspiel::DSL
823
+ attr_writer :author
824
+ attr_writer :title
825
+ dsl_method :set_author, :author= # Make the methods available in parameterless
826
+ dsl_method :set_title, :title= # blocks under these alternate names.
827
+ end
828
+
829
+ Now, when we use block parameters, we use the methods of the original +ConfigMethods+ class:
830
+
831
+ create_paper do |config|
832
+ config.author = "Daniel Azuma"
833
+ config.title = "Implementing DSL Blocks"
834
+ end
835
+
836
+ And, when we omit the parameter, the alternate method names are mixed in:
837
+
838
+ create_paper do
839
+ set_author "Daniel Azuma"
840
+ set_title "Implementing DSL Blocks"
841
+ end
842
+
843
+ Second, you can customize the invocation-- for example specifying whether to perform an arity check, whether to use <tt>instance_eval</tt> instead of mixins, and various other minor behavioral adjustments-- simply by providing parameters to the <tt>Blockenspiel#invoke</tt> method. All the implementation details are handled by the blockenspiel library, leaving you free to focus on your API.
844
+
845
+ Third, blockenspiel provides an API, itself a DSL block, letting you dynamically construct DSLs. Suppose, for the sake of argument, we wanted to let the caller optionally rename the +connect+ method. (Maybe we want to make the name "connect" available for named routes.) That is, suppose we wanted to provide this behavior:
846
+
847
+ ActionController::Routing::Routes.draw(:method => :myconnect) do |map|
848
+ map.myconnect ':controller/:action/:id'
849
+ map.myconnect ':controller/:action/:page/:format'
850
+ # etc.
851
+ end
852
+
853
+ This requires dynamic generation of the proxy class. We could implement it using blockenspiel as follows:
854
+
855
+ class RouteSet
856
+
857
+ # We don't define a static Mapper class anymore. Now it's dynamically generated.
858
+
859
+ def draw(options={}, &block)
860
+ clear!
861
+ method_name = options[:method] || :connect # The method name for the DSL to use
862
+ save_self = self # Save a reference to the RouteSet
863
+ Blockenspiel.invoke(block) do # Dynamically create a "mapper" object
864
+ add_method(method_name) do |path, *args| # Dynamically add the method
865
+ save_self.add_route(path, *args) # Call back to the RouteSet
866
+ end
867
+ end
868
+ named_routes.install
869
+ end
870
+
871
+ # ...
872
+
873
+ def add_route(path, options = {})
874
+ # ...
875
+
876
+ You can install blockenspiel as a gem. It is compatible with MRI 1.8.7 or later, MRI 1.9.1 or later, and JRuby 1.2 or later.
877
+
878
+ gem install blockenspiel
879
+
880
+ More information is available on blockenspiel's Rubyforge page at http://virtuoso.rubyforge.org/blockenspiel
881
+
882
+ Source code is available on Github at http://github.com/dazuma/blockenspiel
883
+
884
+ === Summary
885
+
886
+ DSL blocks are a valuable and ubiquitous pattern for designing Ruby APIs. A flurry of discussion has recently surrounded the implementation of DSL blocks, particularly addressing the desire to eliminate block parameters. We have discussed several different strategies for DSL block implementation, each with its own advantages and disadvantages.
887
+
888
+ The simplest strategy, creating a proxy object and passing a reference to the block as a parameter, is straightforward, safe, and widely used. However, sometimes we might want to provide a cleaner API by eliminating the block parameter.
889
+
890
+ Parameterless blocks inherently pose some syntactic issues. First, it may be ambiguous whether a method is meant to be directed to the DSL or to the block's surrounding context. Second, certain constructions, such as those created by <tt>attr_writer</tt>, are syntactically not allowed and must be renamed.
891
+
892
+ The simplest way to eliminate the block parameter is to change +self+ inside the block using <tt>instance_eval</tt>. This has the side effects of opening the implementation of the proxy object, and cutting off access to the context's helper methods and instance variables.
893
+
894
+ It is possible to mitigate these side effects by delegating methods, and partially delegating instance variables, back to the context object. These are not foolproof mechanisms and are subject to a few cases of surprising behavior.
895
+
896
+ The mixin strategy takes a different approach to parameterless blocks by temporarily "mixing" the DSL methods into the context object itself. This eliminates the side effects of changing the +self+ reference, but requires a more complex implementation, and somewhat exacerbates the method lookup ambiguity.
897
+
898
+ Since the question of whether or not to take a block parameter may be best answered by the caller, it is often useful for an implementation to check the block's arity to determine whether to use a block parameter or a parameterless implementation. However, it is possible for this step to lead to dilution of the DSL's branding.
899
+
900
+ The Blockenspiel library provides a concrete and robust implementation of DSL blocks, based on the best of these ideas. It hides the implementation complexity while providing a number of features useful for writing DSL blocks.
901
+
902
+ === References
903
+
904
+ {Daniel Azuma}[http://www.daniel-azuma.com/], <em>{Blockenspiel}[http://virtuoso.rubyforge.org/blockenspiel]</em> (Ruby library), 2008.
905
+
906
+ {Ola Bini}[http://olabini.com/], <em>{Don't overuse instance_eval and instance_exec}[http://olabini.com/blog/2008/09/dont-overuse-instance_eval-and-instance_exec]</em>, 2008.09.18
907
+
908
+ {Jamis Buck}[http://jamisbuck.org], <em>{Under the hood: Rails' routing DSL}[http://weblog.jamisbuck.org/2006/10/2/under-the-hood-rails-routing-dsl]</em>, 2006.10.02.
909
+
910
+ {James Edward Gray II}[http://blog.grayproductions.net/], <em>{DSL Block Styles}[http://blog.grayproductions.net/articles/dsl_block_styles]</em>, 2008.10.07
911
+
912
+ {Dan Manges}[http://www.dcmanges.com], <em>{Ruby DSLs: instance_eval with delegation}[http://www.dcmanges.com/blog/ruby-dsls-instance-eval-with-delegation]</em>, 2008.10.07
913
+
914
+ {Micah Martin}[http://www.8thlight.com/main/bios/micah], <em>{Ruby DSL Blocks}[http://blog.8thlight.com/articles/2007/05/20/]</em>, 2007.05.20.
915
+
916
+ <em>{Mixology}[http://www.somethingnimble.com/bliki/mixology]</em> (Ruby library), 2007.
917
+
918
+ <em>{RSpec}[http://rspec.info/]</em> (Ruby library), 2005-2008.
919
+
920
+ {Jim Weirich}[http://onestepback.org/], <em>{Builder}[http://builder.rubyforge.org]</em> (Ruby library), 2004-2008.
921
+
922
+ {Jim Weirich}[http://onestepback.org/], <em>{Builder Objects}[http://onestepback.org/index.cgi/Tech/Ruby/BuilderObjects.rdoc]</em> 2004.08.24.
923
+
924
+ {Jim Weirich}[http://onestepback.org/], <em>{ruby-core:19153}[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/19153]</em>, 2008.10.07
925
+
926
+ {Why The Lucky Stiff}[http://whytheluckystiff.net/], <em>{Markaby}[http://code.whytheluckystiff.net/markaby/]</em> (Ruby library), 2006.
927
+
928
+ {Why The Lucky Stiff}[http://whytheluckystiff.net/], <em>{Mixico}[http://github.com/why/mixico/tree/master]</em> (Ruby library), 2008.
929
+
930
+ {Why The Lucky Stiff}[http://whytheluckystiff.net/], <em>{Mixing Our Way Out Of Instance Eval?}[http://hackety.org/2008/10/06/mixingOurWayOutOfInstanceEval.html]</em>, 2008.10.06.
931
+
932
+ === About the author
933
+
934
+ Daniel Azuma is Chief Software Architect at Zoodango. He has been working with Ruby for about three years, and finds the language generally pleasant to work with, though he thinks the scoping rules could use some improvement. His home page is at http://www.daniel-azuma.com/