View on GitHub

Blockenspiel

A comprehensive implementation of Ruby DSL blocks

Download this project as a .zip file Download this project as a tar.gz file

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

By default, Blockenspiel uses a mixin technique (proposed by the late Why The Lucky Stiff) to support parameterless blocks without the complications introduced by instance_eval. 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: