Blockenspiel is a helper library providing several different strategies for implementing DSL blocks. It supports both DSLs that take a block parameter and those that do not.
Here's a simple example:
# Call DSL block with parameter
configure_me do |config|
config.add_foo(1)
config.add_bar(2)
end
# Call DSL block without parameter
configure_me do
add_foo(3)
add_bar(4)
end
To support the above usage, you can do this:
# Implement DSL block methods
class ConfigMethods
include Blockenspiel::DSL
def add_foo(value)
# do something
end
def add_bar(value)
# do something
end
end
# Implement configure_me method
def configure_me(&block)
Blockenspiel.invoke(block, ConfigMethods.new)
end
Blockenspiel uses several techniques to improve on the normal instance_eval method for implementing parameterless blocks. It supports nested blocks and multithreaded access, and provides a variety of tools for handling the typical issues you may encounter when writing DSLs.
Install with "gem install blockenspiel".
For more information:
- Detailed usage and examples
- See the documentation
- Source code on Github
- Report issues here
- An extended analysis of different ways to implement DSL blocks