A DSL implementation based on Bundler

Here is an implementation of a real basic DSL. The implementation was lifted from bundler.

Summary

The main idea is you have a class Dsl that you call instance_eval on. Your domain specific language is still written in ruby, but by evaluating it within the context of an instance of Dsl, you can create the illusion of top level language constructs. These are merely public instance methods of Dsl. This is the way bundler evaluates a Gemfile.

Example

Here is an example, using the implementation in the gist below.

  dsl = Remote::Dsl.new
  dsl.server :app1, "admin@192.168.1.1"
  dsl.server :app2, "admin@192.168.1.2"

By using instance_eval we can turn the above into

  server :app1, "admin@192.168.1.1"
  server :app2, "admin@192.168.1.2"

Examples of this type of DSL:

Implementation

Ben Marini

  • software craftsman
  • san francisco, ca

Rails respond_to gotcha
06 Sep 2011
Rails 3 modularity
30 Dec 2010
Rails 3 routing DSL implementation
29 Dec 2010
A DSL implementation based on Bundler
28 Dec 2010
The case for case statements
18 Apr 2009