blockenspiel 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{History.txt → History.rdoc} +7 -0
- data/{ImplementingDSLblocks.txt → ImplementingDSLblocks.rdoc} +4 -4
- data/{README.txt → README.rdoc} +8 -7
- data/Rakefile +144 -15
- data/ext/blockenspiel/BlockenspielUnmixerService.java +140 -0
- data/ext/blockenspiel/extconf.rb +2 -0
- data/ext/blockenspiel/unmixer.c +86 -0
- data/lib/blockenspiel/impl.rb +670 -0
- data/lib/blockenspiel/version.rb +42 -0
- data/lib/blockenspiel.rb +7 -636
- data/tests/tc_basic.rb +2 -1
- data/tests/tc_behaviors.rb +4 -3
- data/tests/tc_dsl_methods.rb +2 -1
- data/tests/tc_dynamic.rb +2 -1
- data/tests/tc_mixins.rb +2 -1
- metadata +27 -43
- data/Manifest.txt +0 -11
data/lib/blockenspiel.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# -----------------------------------------------------------------------------
|
2
2
|
#
|
3
|
-
# Blockenspiel
|
3
|
+
# Blockenspiel entry point
|
4
4
|
#
|
5
5
|
# -----------------------------------------------------------------------------
|
6
6
|
# Copyright 2008 Daniel Azuma
|
@@ -34,639 +34,10 @@
|
|
34
34
|
;
|
35
35
|
|
36
36
|
|
37
|
-
|
38
|
-
require
|
39
|
-
|
40
|
-
|
41
|
-
# == Blockenspiel
|
42
|
-
#
|
43
|
-
# The Blockenspiel module provides a namespace for Blockenspiel, as well as
|
44
|
-
# the main entry point method "invoke".
|
45
|
-
|
46
|
-
module Blockenspiel
|
47
|
-
|
48
|
-
# Current gem version
|
49
|
-
VERSION_STRING = '0.1.1'.freeze
|
50
|
-
|
51
|
-
|
52
|
-
# Base exception for all exceptions raised by Blockenspiel
|
53
|
-
|
54
|
-
class BlockenspielError < RuntimeError
|
55
|
-
end
|
56
|
-
|
57
|
-
|
58
|
-
# This exception is rasied when attempting to use the <tt>:proxy</tt> or
|
59
|
-
# <tt>:mixin</tt> parameterless behavior with a target that does not have
|
60
|
-
# the DSL module included. It is an error made by the DSL implementor.
|
61
|
-
|
62
|
-
class DSLMissingError < BlockenspielError
|
63
|
-
end
|
64
|
-
|
65
|
-
|
66
|
-
# This exception is raised when the block provided does not take the
|
67
|
-
# expected number of parameters. It is an error made by the caller.
|
68
|
-
|
69
|
-
class BlockParameterError < BlockenspielError
|
70
|
-
end
|
71
|
-
|
72
|
-
|
73
|
-
# === DSL setup methods
|
74
|
-
#
|
75
|
-
# These class methods are available after you have included the
|
76
|
-
# Blockenspiel::DSL module.
|
77
|
-
#
|
78
|
-
# By default, a class that has DSL capability will automatically make
|
79
|
-
# all public methods available to parameterless blocks, except for the
|
80
|
-
# +initialize+ method, any methods whose names begin with an underscore,
|
81
|
-
# and any methods whose names end with an equals sign.
|
82
|
-
#
|
83
|
-
# If you want to change this behavior, use the directives defined here to
|
84
|
-
# control exactly which methods are available to parameterless blocks.
|
85
|
-
|
86
|
-
module DSLSetupMethods
|
87
|
-
|
88
|
-
# Called when DSLSetupMethods extends a class.
|
89
|
-
# This sets up the current class, and adds a hook that causes
|
90
|
-
# any subclass of the current class to also be set up.
|
91
|
-
|
92
|
-
def self.extended(klass_) # :nodoc:
|
93
|
-
unless klass_.instance_variable_defined?(:@_blockenspiel_module)
|
94
|
-
_setup_class(klass_)
|
95
|
-
def klass_.inherited(subklass_)
|
96
|
-
Blockenspiel::DSLSetupMethods._setup_class(subklass_)
|
97
|
-
super
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
|
103
|
-
# Set up a class.
|
104
|
-
# Creates a DSL module for this class, optionally delegating to the superclass's module.
|
105
|
-
# Also initializes the class's methods hash and active flag.
|
106
|
-
|
107
|
-
def self._setup_class(klass_) # :nodoc:
|
108
|
-
superclass_ = klass_.superclass
|
109
|
-
superclass_ = nil unless superclass_.respond_to?(:_get_blockenspiel_module)
|
110
|
-
mod_ = Module.new
|
111
|
-
if superclass_
|
112
|
-
mod_.module_eval do
|
113
|
-
include superclass_._get_blockenspiel_module
|
114
|
-
end
|
115
|
-
end
|
116
|
-
klass_.instance_variable_set(:@_blockenspiel_superclass, superclass_)
|
117
|
-
klass_.instance_variable_set(:@_blockenspiel_module, mod_)
|
118
|
-
klass_.instance_variable_set(:@_blockenspiel_methods, Hash.new)
|
119
|
-
klass_.instance_variable_set(:@_blockenspiel_active, nil)
|
120
|
-
end
|
121
|
-
|
122
|
-
|
123
|
-
# Hook called when a method is added.
|
124
|
-
# This automatically makes the method a DSL method according to the current setting.
|
125
|
-
|
126
|
-
def method_added(symbol_) # :nodoc:
|
127
|
-
if @_blockenspiel_active
|
128
|
-
dsl_method(symbol_)
|
129
|
-
elsif @_blockenspiel_active.nil?
|
130
|
-
if symbol_ != :initialize && symbol_.to_s !~ /^_/ && symbol_.to_s !~ /=$/
|
131
|
-
dsl_method(symbol_)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
super
|
135
|
-
end
|
136
|
-
|
137
|
-
|
138
|
-
# Get this class's corresponding DSL module
|
139
|
-
|
140
|
-
def _get_blockenspiel_module # :nodoc:
|
141
|
-
@_blockenspiel_module
|
142
|
-
end
|
143
|
-
|
144
|
-
|
145
|
-
# Get information on the given DSL method name.
|
146
|
-
# Possible values are the name of the delegate method, false for method disabled,
|
147
|
-
# or nil for method never defined.
|
148
|
-
|
149
|
-
def _get_blockenspiel_delegate(name_) # :nodoc:
|
150
|
-
delegate_ = @_blockenspiel_methods[name_]
|
151
|
-
if delegate_.nil? && @_blockenspiel_superclass
|
152
|
-
@_blockenspiel_superclass._get_blockenspiel_delegate(name_)
|
153
|
-
else
|
154
|
-
delegate_
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
|
159
|
-
# Make a particular method available to parameterless DSL blocks.
|
160
|
-
#
|
161
|
-
# To explicitly make a method available to parameterless blocks:
|
162
|
-
# dsl_method :my_method
|
163
|
-
#
|
164
|
-
# To explicitly exclude a method from parameterless blocks:
|
165
|
-
# dsl_method :my_method, false
|
166
|
-
#
|
167
|
-
# To explicitly make a method available to parameterless blocks, but
|
168
|
-
# point it to a method of a different name on the target class:
|
169
|
-
# dsl_method :my_method, :target_class_method
|
170
|
-
|
171
|
-
def dsl_method(name_, delegate_=nil)
|
172
|
-
name_ = name_.to_sym
|
173
|
-
if delegate_
|
174
|
-
delegate_ = delegate_.to_sym
|
175
|
-
elsif delegate_.nil?
|
176
|
-
delegate_ = name_
|
177
|
-
end
|
178
|
-
@_blockenspiel_methods[name_] = delegate_
|
179
|
-
unless @_blockenspiel_module.public_method_defined?(name_)
|
180
|
-
@_blockenspiel_module.module_eval("
|
181
|
-
def #{name_}(*params_, &block_)
|
182
|
-
val_ = Blockenspiel._target_dispatch(self, :#{name_}, params_, block_)
|
183
|
-
val_ == Blockenspiel::TARGET_MISMATCH ? super(*params_, &block_) : val_
|
184
|
-
end
|
185
|
-
")
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
|
190
|
-
# Control the behavior of methods with respect to parameterless blocks,
|
191
|
-
# or make a list of methods available to parameterless blocks in bulk.
|
192
|
-
#
|
193
|
-
# To enable automatic exporting of methods to parameterless blocks.
|
194
|
-
# After executing this command, all public methods defined in the class
|
195
|
-
# will be available on parameterless blocks, until
|
196
|
-
# <tt>dsl_methods false</tt> is called:
|
197
|
-
# dsl_methods true
|
198
|
-
#
|
199
|
-
# To disable automatic exporting of methods to parameterless blocks.
|
200
|
-
# After executing this command, methods defined in this class will be
|
201
|
-
# excluded from parameterless blocks, until <tt>dsl_methods true</tt>
|
202
|
-
# is called:
|
203
|
-
# dsl_methods false
|
204
|
-
#
|
205
|
-
# To make a list of methods available to parameterless blocks in bulk:
|
206
|
-
# dsl_methods :my_method1, :my_method2, ...
|
207
|
-
#
|
208
|
-
# You can also point dsl methods to a method of a different name on the
|
209
|
-
# target class, by using a hash syntax, as follows:
|
210
|
-
# dsl_methods :my_method1 => :target_class_method1,
|
211
|
-
# :my_method2 => :target_class_method2
|
212
|
-
#
|
213
|
-
# You can mix non-renamed and renamed method declarations as long as
|
214
|
-
# the renamed (hash) methods are at the end. e.g.:
|
215
|
-
# dsl_methods :my_method1, :my_method2 => :target_class_method2
|
216
|
-
|
217
|
-
def dsl_methods(*names_)
|
218
|
-
if names_.size == 0 || names_ == [true]
|
219
|
-
@_blockenspiel_active = true
|
220
|
-
elsif names_ == [false]
|
221
|
-
@_blockenspiel_active = false
|
222
|
-
else
|
223
|
-
if names_.last.kind_of?(Hash)
|
224
|
-
names_.pop.each do |name_, delegate_|
|
225
|
-
dsl_method(name_, delegate_)
|
226
|
-
end
|
227
|
-
end
|
228
|
-
names_.each do |name_|
|
229
|
-
dsl_method(name_, name_)
|
230
|
-
end
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
end
|
235
|
-
|
236
|
-
|
237
|
-
# === DSL activation module
|
238
|
-
#
|
239
|
-
# Include this module in a class to mark this class as a DSL class and
|
240
|
-
# make it possible for its methods to be called from a block that does not
|
241
|
-
# take a parameter.
|
242
|
-
#
|
243
|
-
# After you include this module, you can use the directives defined in
|
244
|
-
# DSLSetupMethods to control what methods are available to DSL blocks
|
245
|
-
# that do not take parameters.
|
246
|
-
|
247
|
-
module DSL
|
248
|
-
|
249
|
-
def self.included(klass_) # :nodoc:
|
250
|
-
klass_.extend(Blockenspiel::DSLSetupMethods)
|
251
|
-
end
|
252
|
-
|
253
|
-
end
|
254
|
-
|
255
|
-
|
256
|
-
# === DSL activation base class
|
257
|
-
#
|
258
|
-
# Subclasses of this base class are considered DSL classes.
|
259
|
-
# Methods of the class can be made available to be called from a block that
|
260
|
-
# doesn't take an explicit block parameter.
|
261
|
-
# You may use the directives defined in DSLSetupMethods to control how
|
262
|
-
# methods of the class are handled in such blocks.
|
263
|
-
#
|
264
|
-
# Subclassing this base class is functionally equivalent to simply
|
265
|
-
# including Blockenspiel::DSL in the class.
|
266
|
-
|
267
|
-
class Base
|
268
|
-
|
269
|
-
include Blockenspiel::DSL
|
270
|
-
|
271
|
-
end
|
272
|
-
|
273
|
-
|
274
|
-
# Class for proxy delegators.
|
275
|
-
# The proxy behavior creates one of these delegators, mixes in the dsl
|
276
|
-
# methods, and uses instance_eval to invoke the block. This class delegates
|
277
|
-
# non-handled methods to the context object.
|
278
|
-
|
279
|
-
class ProxyDelegator # :nodoc:
|
280
|
-
|
281
|
-
def method_missing(symbol_, *params_, &block_)
|
282
|
-
Blockenspiel._proxy_dispatch(self, symbol_, params_, block_)
|
283
|
-
end
|
284
|
-
|
285
|
-
end
|
286
|
-
|
287
|
-
|
288
|
-
# === Dynamically construct a target
|
289
|
-
#
|
290
|
-
# These methods are available in a block passed to Blockenspiel#invoke and
|
291
|
-
# can be used to dynamically define what methods are available from a block.
|
292
|
-
# See Blockenspiel#invoke for more information.
|
293
|
-
|
294
|
-
class Builder
|
295
|
-
|
296
|
-
include Blockenspiel::DSL
|
297
|
-
|
298
|
-
|
299
|
-
# This is a base class for dynamically constructed targets.
|
300
|
-
# The actual target class is an anonymous subclass of this base class.
|
301
|
-
|
302
|
-
class Target # :nodoc:
|
303
|
-
|
304
|
-
include Blockenspiel::DSL
|
305
|
-
|
306
|
-
|
307
|
-
# Add a method specification to the subclass.
|
308
|
-
|
309
|
-
def self._add_methodinfo(name_, block_, yields_)
|
310
|
-
(@_blockenspiel_methodinfo ||= Hash.new)[name_] = [block_, yields_]
|
311
|
-
module_eval("
|
312
|
-
def #{name_}(*params_, &block_)
|
313
|
-
self.class._invoke_methodinfo(:#{name_}, params_, block_)
|
314
|
-
end
|
315
|
-
")
|
316
|
-
end
|
317
|
-
|
318
|
-
|
319
|
-
# Attempt to invoke the given method on the subclass.
|
320
|
-
|
321
|
-
def self._invoke_methodinfo(name_, params_, block_)
|
322
|
-
info_ = @_blockenspiel_methodinfo[name_]
|
323
|
-
case info_[1]
|
324
|
-
when :first
|
325
|
-
params_.unshift(block_)
|
326
|
-
when :last
|
327
|
-
params_.push(block_)
|
328
|
-
end
|
329
|
-
info_[0].call(*params_)
|
330
|
-
end
|
331
|
-
|
332
|
-
end
|
333
|
-
|
334
|
-
|
335
|
-
# Sets up the dynamic target class.
|
336
|
-
|
337
|
-
def initialize # :nodoc:
|
338
|
-
@target_class = Class.new(Blockenspiel::Builder::Target)
|
339
|
-
@target_class.dsl_methods(false)
|
340
|
-
end
|
341
|
-
|
342
|
-
|
343
|
-
# Creates a new instance of the dynamic target class
|
344
|
-
|
345
|
-
def _create_target # :nodoc:
|
346
|
-
@target_class.new
|
347
|
-
end
|
348
|
-
|
349
|
-
|
350
|
-
# === Declare a DSL method.
|
351
|
-
#
|
352
|
-
# This call creates a method that can be called from the DSL block.
|
353
|
-
# Provide a name for the method, and a block defining the method's
|
354
|
-
# implementation. You may also provided a list of options as follows:
|
355
|
-
#
|
356
|
-
# The <tt>:block</tt> option controls how the generated method reports
|
357
|
-
# any block provided by the caller. If set to +false+ or not given, any
|
358
|
-
# caller-provided block is ignored. If set to <tt>:first</tt>, the
|
359
|
-
# block is _prepended_ (as a +Proc+ object) to the parameters passed to
|
360
|
-
# the method's implementing block. If set to <tt>:last</tt>, the block
|
361
|
-
# is _appended_. In either case, if the caller does not provide a block,
|
362
|
-
# a value of +nil+ is pre- or appended to the parameter list. A value of
|
363
|
-
# +true+ is equivalent to <tt>:first</tt>.
|
364
|
-
# (This is a workaround for the fact that blocks cannot themselves take
|
365
|
-
# block parameters in Ruby 1.8.)
|
366
|
-
#
|
367
|
-
# For example, to create a method named "foo" that takes one parameter
|
368
|
-
# and a block, do this:
|
369
|
-
#
|
370
|
-
# add_method(:foo, :block => :first) do |block, param|
|
371
|
-
# puts "foo called with parameter "+param.inspect
|
372
|
-
# puts "the block returned "+block.call.inspect
|
373
|
-
# end
|
374
|
-
#
|
375
|
-
# In your DSL, you can then call:
|
376
|
-
#
|
377
|
-
# foo("hello"){ "a value" }
|
378
|
-
#
|
379
|
-
# By default, a method of the same name is also made available to
|
380
|
-
# parameterless blocks. To change the name of the parameterless method,
|
381
|
-
# provide its name as the value of the <tt>:dsl_method</tt> option.
|
382
|
-
# To disable this method for parameterless blocks, set the
|
383
|
-
# <tt>:dsl_method</tt> option to +false+.
|
384
|
-
#
|
385
|
-
# For historical reasons, the <tt>:mixin</tt> option is an alias for
|
386
|
-
# <tt>:dsl_method</tt>. However, its use is deprecated.
|
387
|
-
#
|
388
|
-
# The <tt>:receive_block</tt> option is also supported for historical
|
389
|
-
# reasons, but its use is deprecated. Setting <tt>:receive_block</tt> to
|
390
|
-
# +true+ is equivalent to setting <tt>:block</tt> to <tt>:last</tt>.
|
391
|
-
|
392
|
-
def add_method(name_, opts_={}, &block_)
|
393
|
-
receive_block_ = opts_[:receive_block] ? :last : opts_[:block]
|
394
|
-
receive_block_ = :first if receive_block_ && receive_block_ != :last
|
395
|
-
@target_class._add_methodinfo(name_, block_, receive_block_)
|
396
|
-
dsl_method_name_ = opts_[:dsl_method] || opts_[:mixin]
|
397
|
-
if dsl_method_name_ != false
|
398
|
-
dsl_method_name_ = name_ if dsl_method_name_.nil? || dsl_method_name_ == true
|
399
|
-
@target_class.dsl_method(dsl_method_name_, name_)
|
400
|
-
end
|
401
|
-
end
|
402
|
-
|
403
|
-
end
|
404
|
-
|
405
|
-
|
406
|
-
# :stopdoc:
|
407
|
-
TARGET_MISMATCH = Object.new
|
408
|
-
# :startdoc:
|
409
|
-
|
410
|
-
@_target_stacks = Hash.new
|
411
|
-
@_mixin_counts = Hash.new
|
412
|
-
@_proxy_delegators = Hash.new
|
413
|
-
@_mutex = Mutex.new
|
414
|
-
|
415
|
-
|
416
|
-
# === Invoke a given block.
|
417
|
-
#
|
418
|
-
# This is the meat of Blockenspiel. Call this function to invoke a block
|
419
|
-
# provided by the user of your API.
|
420
|
-
#
|
421
|
-
# Normally, this method will check the block's arity to see whether it
|
422
|
-
# takes a parameter. If so, it will pass the given target to the block.
|
423
|
-
# If the block takes no parameter, and the given target is an instance of
|
424
|
-
# a class with DSL capability, the DSL methods are made available on the
|
425
|
-
# caller's self object so they may be called without a block parameter.
|
426
|
-
#
|
427
|
-
# Recognized options include:
|
428
|
-
#
|
429
|
-
# <tt>:parameterless</tt>::
|
430
|
-
# If set to false, disables parameterless blocks and always attempts to
|
431
|
-
# pass a parameter to the block. Otherwise, you may set it to one of
|
432
|
-
# three behaviors for parameterless blocks: <tt>:mixin</tt> (the
|
433
|
-
# default), <tt>:instance</tt>, and <tt>:proxy</tt>. See below for
|
434
|
-
# detailed descriptions of these behaviors.
|
435
|
-
# <tt>:parameter</tt>::
|
436
|
-
# If set to false, disables blocks with parameters, and always attempts
|
437
|
-
# to use parameterless blocks. Default is true, enabling parameter mode.
|
438
|
-
#
|
439
|
-
# The following values control the precise behavior of parameterless
|
440
|
-
# blocks. These are values for the <tt>:parameterless</tt> option.
|
441
|
-
#
|
442
|
-
# <tt>:mixin</tt>::
|
443
|
-
# This is the default behavior. DSL methods from the target are
|
444
|
-
# temporarily overlayed on the caller's +self+ object, but +self+ still
|
445
|
-
# points to the same object, so the helper methods and instance
|
446
|
-
# variables from the caller's closure remain available. The DSL methods
|
447
|
-
# are removed when the block completes.
|
448
|
-
# <tt>:instance</tt>::
|
449
|
-
# This behavior actually changes +self+ to the target object using
|
450
|
-
# <tt>instance_eval</tt>. Thus, the caller loses access to its own
|
451
|
-
# helper methods and instance variables, and instead gains access to the
|
452
|
-
# target object's instance variables. The target object's methods are
|
453
|
-
# not modified: this behavior does not apply any DSL method changes
|
454
|
-
# specified using <tt>dsl_method</tt> directives.
|
455
|
-
# <tt>:proxy</tt>::
|
456
|
-
# This behavior changes +self+ to a proxy object created by applying the
|
457
|
-
# DSL methods to an empty object, whose <tt>method_missing</tt> points
|
458
|
-
# back at the block's context. This behavior is a compromise between
|
459
|
-
# instance and mixin. As with instance, +self+ is changed, so the caller
|
460
|
-
# loses access to its own instance variables. However, the caller's own
|
461
|
-
# methods should still be available since any methods not handled by the
|
462
|
-
# DSL are delegated back to the caller. Also, as with mixin, the target
|
463
|
-
# object's instance variables are not available (and thus cannot be
|
464
|
-
# clobbered) in the block, and the transformations specified by
|
465
|
-
# <tt>dsl_method</tt> directives are honored.
|
466
|
-
#
|
467
|
-
# === Dynamic target generation
|
468
|
-
#
|
469
|
-
# It is also possible to dynamically generate a target object by passing
|
470
|
-
# a block to this method. This is probably best illustrated by example:
|
471
|
-
#
|
472
|
-
# Blockenspiel.invoke(block) do
|
473
|
-
# add_method(:set_foo) do |value|
|
474
|
-
# my_foo = value
|
475
|
-
# end
|
476
|
-
# add_method(:set_things_from_block, :receive_block => true) do |value,blk|
|
477
|
-
# my_foo = value
|
478
|
-
# my_bar = blk.call
|
479
|
-
# end
|
480
|
-
# end
|
481
|
-
#
|
482
|
-
# The above is roughly equivalent to invoking Blockenspiel with an
|
483
|
-
# instance of this target class:
|
484
|
-
#
|
485
|
-
# class MyFooTarget
|
486
|
-
# include Blockenspiel::DSL
|
487
|
-
# def set_foo(value)
|
488
|
-
# set_my_foo_from(value)
|
489
|
-
# end
|
490
|
-
# def set_things_from_block(value)
|
491
|
-
# set_my_foo_from(value)
|
492
|
-
# set_my_bar_from(yield)
|
493
|
-
# end
|
494
|
-
# end
|
495
|
-
#
|
496
|
-
# Blockenspiel.invoke(block, MyFooTarget.new)
|
497
|
-
#
|
498
|
-
# The obvious advantage of using dynamic object generation is that you are
|
499
|
-
# creating methods using closures, which provides the opportunity to, for
|
500
|
-
# example, modify closure variables such as my_foo. This is more difficult
|
501
|
-
# to do when you create a target class since its methods do not have access
|
502
|
-
# to outside data. Hence, in the above example, we hand-waved, assuming the
|
503
|
-
# existence of some method called "set_my_foo_from".
|
504
|
-
#
|
505
|
-
# The disadvantage is performance. If you dynamically generate a target
|
506
|
-
# object, it involves parsing and creating a new class whenever it is
|
507
|
-
# invoked. Thus, it is recommended that you use this technique for calls
|
508
|
-
# that are not used repeatedly, such as one-time configuration.
|
509
|
-
#
|
510
|
-
# See the Blockenspiel::Builder class for more details on add_method.
|
511
|
-
#
|
512
|
-
# (And yes, you guessed it: this API is a DSL block, and is itself
|
513
|
-
# implemented using Blockenspiel.)
|
514
|
-
|
515
|
-
def self.invoke(block_, target_=nil, opts_={}, &builder_block_)
|
516
|
-
|
517
|
-
unless block_
|
518
|
-
raise ArgumentError, "Block expected"
|
519
|
-
end
|
520
|
-
parameter_ = opts_[:parameter]
|
521
|
-
parameterless_ = opts_[:parameterless]
|
522
|
-
|
523
|
-
# Handle no-target behavior
|
524
|
-
if parameter_ == false && parameterless_ == false
|
525
|
-
if block_.arity != 0 && block_.arity != -1
|
526
|
-
raise Blockenspiel::BlockParameterError, "Block should not take parameters"
|
527
|
-
end
|
528
|
-
return block_.call
|
529
|
-
end
|
530
|
-
|
531
|
-
# Perform dynamic target generation if requested
|
532
|
-
if builder_block_
|
533
|
-
opts_ = target_ || opts_
|
534
|
-
builder_ = Blockenspiel::Builder.new
|
535
|
-
invoke(builder_block_, builder_)
|
536
|
-
target_ = builder_._create_target
|
537
|
-
end
|
538
|
-
|
539
|
-
# Handle parametered block case
|
540
|
-
if parameter_ != false && block_.arity == 1 || parameterless_ == false
|
541
|
-
if block_.arity != 1
|
542
|
-
raise Blockenspiel::BlockParameterError, "Block should take exactly one parameter"
|
543
|
-
end
|
544
|
-
return block_.call(target_)
|
545
|
-
end
|
546
|
-
|
547
|
-
# Check arity for parameterless case
|
548
|
-
if block_.arity != 0 && block_.arity != -1
|
549
|
-
raise Blockenspiel::BlockParameterError, "Block should not take parameters"
|
550
|
-
end
|
551
|
-
|
552
|
-
# Handle instance-eval behavior
|
553
|
-
if parameterless_ == :instance
|
554
|
-
return target_.instance_eval(&block_)
|
555
|
-
end
|
556
|
-
|
557
|
-
# Get the module of dsl methods
|
558
|
-
mod_ = target_.class._get_blockenspiel_module rescue nil
|
559
|
-
unless mod_
|
560
|
-
raise Blockenspiel::DSLMissingError
|
561
|
-
end
|
562
|
-
|
563
|
-
# Get the block's calling context object
|
564
|
-
object_ = Kernel.eval('self', block_.binding)
|
565
|
-
|
566
|
-
# Handle proxy behavior
|
567
|
-
if parameterless_ == :proxy
|
568
|
-
|
569
|
-
# Create proxy object
|
570
|
-
proxy_ = Blockenspiel::ProxyDelegator.new
|
571
|
-
proxy_.extend(mod_)
|
572
|
-
|
573
|
-
# Store the target and proxy object so dispatchers can get them
|
574
|
-
proxy_delegator_key_ = proxy_.object_id
|
575
|
-
target_stack_key_ = [Thread.current.object_id, proxy_.object_id]
|
576
|
-
@_proxy_delegators[proxy_delegator_key_] = object_
|
577
|
-
@_target_stacks[target_stack_key_] = [target_]
|
578
|
-
|
579
|
-
begin
|
580
|
-
|
581
|
-
# Call the block with the proxy as self
|
582
|
-
return proxy_.instance_eval(&block_)
|
583
|
-
|
584
|
-
ensure
|
585
|
-
|
586
|
-
# Clean up the dispatcher information
|
587
|
-
@_proxy_delegators.delete(proxy_delegator_key_)
|
588
|
-
@_target_stacks.delete(target_stack_key_)
|
589
|
-
|
590
|
-
end
|
591
|
-
|
592
|
-
end
|
593
|
-
|
594
|
-
# Handle mixin behavior (default)
|
595
|
-
|
596
|
-
# Create hash keys
|
597
|
-
mixin_count_key_ = [object_.object_id, mod_.object_id]
|
598
|
-
target_stack_key_ = [Thread.current.object_id, object_.object_id]
|
599
|
-
|
600
|
-
# Store the target for inheriting.
|
601
|
-
# We maintain a target call stack per thread.
|
602
|
-
target_stack_ = @_target_stacks[target_stack_key_] ||= Array.new
|
603
|
-
target_stack_.push(target_)
|
604
|
-
|
605
|
-
# Mix this module into the object, if required.
|
606
|
-
# This ensures that we keep track of the number of requests to
|
607
|
-
# mix this module in, from nested blocks and possibly multiple threads.
|
608
|
-
@_mutex.synchronize do
|
609
|
-
count_ = @_mixin_counts[mixin_count_key_]
|
610
|
-
if count_
|
611
|
-
@_mixin_counts[mixin_count_key_] = count_ + 1
|
612
|
-
else
|
613
|
-
@_mixin_counts[mixin_count_key_] = 1
|
614
|
-
object_.mixin(mod_)
|
615
|
-
end
|
616
|
-
end
|
617
|
-
|
618
|
-
begin
|
619
|
-
|
620
|
-
# Now call the block
|
621
|
-
return block_.call
|
622
|
-
|
623
|
-
ensure
|
624
|
-
|
625
|
-
# Clean up the target stack
|
626
|
-
target_stack_.pop
|
627
|
-
@_target_stacks.delete(target_stack_key_) if target_stack_.size == 0
|
628
|
-
|
629
|
-
# Remove the mixin from the object, if required.
|
630
|
-
@_mutex.synchronize do
|
631
|
-
count_ = @_mixin_counts[mixin_count_key_]
|
632
|
-
if count_ == 1
|
633
|
-
@_mixin_counts.delete(mixin_count_key_)
|
634
|
-
object_.unmix(mod_)
|
635
|
-
else
|
636
|
-
@_mixin_counts[mixin_count_key_] = count_ - 1
|
637
|
-
end
|
638
|
-
end
|
639
|
-
|
640
|
-
end
|
641
|
-
|
642
|
-
end
|
643
|
-
|
644
|
-
|
645
|
-
# This implements the mapping between DSL module methods and target object methods.
|
646
|
-
# We look up the current target object based on the current thread.
|
647
|
-
# Then we attempt to call the given method on that object.
|
648
|
-
# If we can't find an appropriate method to call, return the special value TARGET_MISMATCH.
|
649
|
-
|
650
|
-
def self._target_dispatch(object_, name_, params_, block_) # :nodoc:
|
651
|
-
target_stack_ = @_target_stacks[[Thread.current.object_id, object_.object_id]]
|
652
|
-
return Blockenspiel::TARGET_MISMATCH unless target_stack_
|
653
|
-
target_stack_.reverse_each do |target_|
|
654
|
-
target_class_ = target_.class
|
655
|
-
delegate_ = target_class_._get_blockenspiel_delegate(name_)
|
656
|
-
if delegate_ && target_class_.public_method_defined?(delegate_)
|
657
|
-
return target_.send(delegate_, *params_, &block_)
|
658
|
-
end
|
659
|
-
end
|
660
|
-
return Blockenspiel::TARGET_MISMATCH
|
661
|
-
end
|
662
|
-
|
663
|
-
|
664
|
-
# This implements the proxy fall-back behavior.
|
665
|
-
# We look up the context object, and call the given method on that object.
|
666
|
-
|
667
|
-
def self._proxy_dispatch(proxy_, name_, params_, block_) # :nodoc:
|
668
|
-
@_proxy_delegators[proxy_.object_id].send(name_, *params_, &block_)
|
669
|
-
end
|
670
|
-
|
671
|
-
|
37
|
+
if RUBY_PLATFORM =~ /java/
|
38
|
+
require "blockenspiel_unmixer"
|
39
|
+
else
|
40
|
+
require "#{File.dirname(__FILE__)}/blockenspiel/unmixer"
|
672
41
|
end
|
42
|
+
require "#{File.dirname(__FILE__)}/blockenspiel/impl"
|
43
|
+
require "#{File.dirname(__FILE__)}/blockenspiel/version"
|
data/tests/tc_basic.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
# This file contains tests for the simple use cases.
|
6
6
|
#
|
7
7
|
# -----------------------------------------------------------------------------
|
8
|
-
# Copyright 2008 Daniel Azuma
|
8
|
+
# Copyright 2008-2009 Daniel Azuma
|
9
9
|
#
|
10
10
|
# All rights reserved.
|
11
11
|
#
|
@@ -36,6 +36,7 @@
|
|
36
36
|
;
|
37
37
|
|
38
38
|
|
39
|
+
require 'test/unit'
|
39
40
|
require File.expand_path("#{File.dirname(__FILE__)}/../lib/blockenspiel.rb")
|
40
41
|
|
41
42
|
|
data/tests/tc_behaviors.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
# This file contains tests for behavior settings.
|
6
6
|
#
|
7
7
|
# -----------------------------------------------------------------------------
|
8
|
-
# Copyright 2008 Daniel Azuma
|
8
|
+
# Copyright 2008-2009 Daniel Azuma
|
9
9
|
#
|
10
10
|
# All rights reserved.
|
11
11
|
#
|
@@ -36,6 +36,7 @@
|
|
36
36
|
;
|
37
37
|
|
38
38
|
|
39
|
+
require 'test/unit'
|
39
40
|
require File.expand_path("#{File.dirname(__FILE__)}/../lib/blockenspiel.rb")
|
40
41
|
|
41
42
|
|
@@ -95,7 +96,7 @@ module Blockenspiel
|
|
95
96
|
context_self_.assert_raise(NoMethodError){ set_value3_dslversion('d', 4) }
|
96
97
|
context_self_.assert_raise(NoMethodError){ helper_method() }
|
97
98
|
context_self_.assert(!instance_variable_defined?(:@my_instance_variable_test))
|
98
|
-
context_self_.assert_instance_of(Target1, self)
|
99
|
+
context_self_.assert_instance_of(Blockenspiel::Tests::TextBehaviors::Target1, self)
|
99
100
|
end
|
100
101
|
Blockenspiel.invoke(block_, Target1.new(hash_), :parameterless => :instance)
|
101
102
|
assert_equal(1, hash_['a1'])
|
@@ -122,7 +123,7 @@ module Blockenspiel
|
|
122
123
|
context_self_.assert_raise(NoMethodError){ set_value3('d', 4) }
|
123
124
|
context_self_.assert(helper_method())
|
124
125
|
context_self_.assert(!instance_variable_defined?(:@my_instance_variable_test))
|
125
|
-
context_self_.assert(!self.kind_of?(Target1))
|
126
|
+
context_self_.assert(!self.kind_of?(Blockenspiel::Tests::TextBehaviors::Target1))
|
126
127
|
context_self_.assert_not_equal(context_self_, self)
|
127
128
|
end
|
128
129
|
Blockenspiel.invoke(block_, Target1.new(hash_), :parameterless => :proxy)
|