Designing your DSL
The most important thing to think about when designing a DSL is to focus on how the DSL will be used. Some DSLs are designed to configure a library. Some DSLs are used for making specific changes using the library. Some DSLs exist purely to reduce the verbosity of the code. Sometimes the library exposes a DSL as its only interface, and the library and DSL are basically the same thing. Let's focus first on DSLs designed for configuring a library.
Configuration DSLs
DSLs designed to configure libraries are often referred to as configuration DSLs. They are often initiated from a singleton method on the library's main module or class, often straightforwardly named configure
. RSpec
, a popular Ruby library for testing, uses a configuration DSL like this:
RSpec.configure do |c| c.drb = true c.drb_port = 24601 c.around do |spec| DB.transaction(rollback: :always, &spec) end end...