View on GitHub

Docile

Docile keeps your Ruby DSLs tame and well-behaved

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

Docile

Docile is a small, self-contained Ruby library, that let's you map a DSL (domain specific language) to your Ruby objects in a snap.

Let's take a look

Let's say that we want to make a DSL for modifying Array objects. Wouldn't it be great if we could just treat the methods of Array as a DSL?

with_array([]) do
  push 1
  push 2
  pop
  push 3
end
# => [1, 3]

No problem, just define the method with_array using Docile like this:

def with_array(arr=[], &block)
  Docile.dsl_eval(arr, &block)
end

Big deal, right? Well guess what! It handles the rough edges for you, so you can use functions, instance variables, and local variables in your DSL with no sweat:

def question
  "What is the answer...?"
end

@answer = 42

knowledge = { "towel" => true }

Docile.dsl_eval({}) do
  store question, @answer
  merge! knowledge
end
# => {"What is the answer...?"=>42, "towel"=>true}

Cool

Ok, I want to learn more!