2011-12-06 17:58:09 -05:00
|
|
|
# Docile
|
2013-07-04 13:37:28 -04:00
|
|
|
[![Gem Version](https://badge.fury.io/rb/docile.png)](http://badge.fury.io/rb/docile)
|
2012-10-28 08:26:37 -04:00
|
|
|
[![Build Status](https://travis-ci.org/ms-ati/docile.png)](https://travis-ci.org/ms-ati/docile)
|
2012-10-28 08:39:12 -04:00
|
|
|
[![Dependency Status](https://gemnasium.com/ms-ati/docile.png)](https://gemnasium.com/ms-ati/docile)
|
2013-07-04 13:33:08 -04:00
|
|
|
[![Code Climate](https://codeclimate.com/github/ms-ati/docile.png)](https://codeclimate.com/github/ms-ati/docile)
|
2013-07-05 10:46:19 -04:00
|
|
|
[![Coverage Status](https://coveralls.io/repos/ms-ati/docile/badge.png)](https://coveralls.io/r/ms-ati/docile)
|
2012-10-28 08:26:37 -04:00
|
|
|
|
2013-07-28 18:37:47 -04:00
|
|
|
Ruby makes it possible to create very expressive **Domain Specific
|
2013-07-28 18:40:10 -04:00
|
|
|
Languages**, or **DSL**'s for short. However, it requires some deep knowledge and
|
2013-07-28 18:36:21 -04:00
|
|
|
somewhat hairy meta-programming to get the interface just right.
|
|
|
|
|
|
|
|
"Docile" means *Ready to accept control or instruction; submissive* [[1]]
|
|
|
|
|
|
|
|
Instead of each Ruby project reinventing this wheel, let's make our Ruby DSL
|
|
|
|
coding a bit more docile...
|
|
|
|
|
|
|
|
[1]: http://www.google.com/search?q=docile+definition "Google"
|
|
|
|
|
2013-07-29 03:13:02 -04:00
|
|
|
## Usage
|
|
|
|
|
|
|
|
### Basic
|
2011-12-06 17:58:09 -05:00
|
|
|
|
2012-11-26 15:23:32 -05:00
|
|
|
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?
|
2011-12-06 17:58:09 -05:00
|
|
|
|
2012-11-26 15:23:32 -05:00
|
|
|
```ruby
|
|
|
|
with_array([]) do
|
2011-12-06 17:58:09 -05:00
|
|
|
push 1
|
|
|
|
push 2
|
2011-12-06 18:41:43 -05:00
|
|
|
pop
|
|
|
|
push 3
|
2011-12-06 17:58:09 -05:00
|
|
|
end
|
2013-07-29 00:37:03 -04:00
|
|
|
#=> [1, 3]
|
2011-12-06 17:58:09 -05:00
|
|
|
```
|
|
|
|
|
2012-11-27 08:09:44 -05:00
|
|
|
No problem, just define the method `with_array` like this:
|
2013-07-28 10:53:42 -04:00
|
|
|
|
2013-07-29 00:37:03 -04:00
|
|
|
```ruby
|
2012-11-26 15:23:32 -05:00
|
|
|
def with_array(arr=[], &block)
|
|
|
|
Docile.dsl_eval(arr, &block)
|
|
|
|
end
|
2011-12-06 17:58:09 -05:00
|
|
|
```
|
|
|
|
|
2012-11-26 15:23:32 -05:00
|
|
|
Easy!
|
2011-12-06 17:58:09 -05:00
|
|
|
|
2013-07-29 03:13:02 -04:00
|
|
|
### Advanced
|
2012-11-26 15:23:32 -05:00
|
|
|
|
|
|
|
Mutating (changing) an Array instance is fine, but what usually makes a good DSL is a [Builder Pattern][2].
|
|
|
|
|
|
|
|
For example, let's say you want a DSL to specify how you want to build a Pizza:
|
2013-07-28 10:53:42 -04:00
|
|
|
|
2012-11-26 15:23:32 -05:00
|
|
|
```ruby
|
2011-12-06 17:58:09 -05:00
|
|
|
@sauce_level = :extra
|
2012-11-26 15:23:32 -05:00
|
|
|
|
|
|
|
pizza do
|
2011-12-06 17:58:09 -05:00
|
|
|
cheese
|
|
|
|
pepperoni
|
|
|
|
sauce @sauce_level
|
2012-11-26 15:23:32 -05:00
|
|
|
end
|
2013-07-29 00:37:03 -04:00
|
|
|
#=> #<Pizza:0x00001009dc398 @cheese=true, @pepperoni=true, @bacon=false, @sauce=:extra>
|
2012-11-26 15:23:32 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
And let's say we have a PizzaBuilder, which builds a Pizza like this:
|
2013-07-28 10:53:42 -04:00
|
|
|
|
2012-11-26 15:23:32 -05:00
|
|
|
```ruby
|
|
|
|
Pizza = Struct.new(:cheese, :pepperoni, :bacon, :sauce)
|
|
|
|
|
|
|
|
class PizzaBuilder
|
2013-08-01 07:15:53 -04:00
|
|
|
def cheese(v=true); @cheese = v; self; end
|
|
|
|
def pepperoni(v=true); @pepperoni = v; self; end
|
|
|
|
def bacon(v=true); @bacon = v; self; end
|
|
|
|
def sauce(v=nil); @sauce = v; self; end
|
2012-11-26 15:23:32 -05:00
|
|
|
def build
|
|
|
|
Pizza.new(!!@cheese, !!@pepperoni, !!@bacon, @sauce)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-26 15:25:20 -05:00
|
|
|
PizzaBuilder.new.cheese.pepperoni.sauce(:extra).build
|
2011-12-06 18:41:43 -05:00
|
|
|
#=> #<Pizza:0x00001009dc398 @cheese=true, @pepperoni=true, @bacon=false, @sauce=:extra>
|
2011-12-06 17:58:09 -05:00
|
|
|
```
|
|
|
|
|
2012-11-26 15:23:32 -05:00
|
|
|
Then implement your DSL like this:
|
|
|
|
|
|
|
|
``` ruby
|
|
|
|
def pizza(&block)
|
|
|
|
Docile.dsl_eval(PizzaBuilder.new, &block).build
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2013-07-29 00:37:03 -04:00
|
|
|
It's just that easy!
|
2011-12-06 18:41:43 -05:00
|
|
|
|
2011-12-06 17:58:09 -05:00
|
|
|
[2]: http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern "Builder Pattern"
|
|
|
|
|
2013-07-29 03:13:02 -04:00
|
|
|
### Block parameters
|
2013-04-01 01:04:28 -04:00
|
|
|
|
|
|
|
Parameters can be passed to the DSL block.
|
|
|
|
|
|
|
|
Supposing you want to make some sort of cheap [Sinatra][3] knockoff:
|
2013-07-28 10:53:42 -04:00
|
|
|
|
2013-04-01 01:04:28 -04:00
|
|
|
```ruby
|
|
|
|
@last_request = nil
|
|
|
|
respond '/path' do |request|
|
|
|
|
puts "Request received: #{request}"
|
|
|
|
@last_request = request
|
|
|
|
end
|
|
|
|
|
|
|
|
def ride bike
|
|
|
|
# Play with your new bike
|
|
|
|
end
|
|
|
|
|
|
|
|
respond '/new_bike' do |bike|
|
|
|
|
ride(bike)
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
You'd put together a dispatcher something like this:
|
2013-07-28 10:53:42 -04:00
|
|
|
|
2013-04-01 01:04:28 -04:00
|
|
|
```ruby
|
|
|
|
require 'singleton'
|
|
|
|
|
|
|
|
class DispatchScope
|
|
|
|
def a_method_you_can_call_from_inside_the_block
|
|
|
|
:useful_huh?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class MessageDispatch
|
|
|
|
include Singleton
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@responders = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_responder path, &block
|
|
|
|
@responders[path] = block
|
|
|
|
end
|
|
|
|
|
|
|
|
def dispatch path, request
|
|
|
|
Docile.dsl_eval(DispatchScope.new, request, &@responders[path])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond path, &handler
|
|
|
|
MessageDispatch.instance.add_responder path, handler
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_request path, request
|
|
|
|
MessageDispatch.instance.dispatch path, request
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2013-04-01 01:06:13 -04:00
|
|
|
[3]: http://www.sinatrarb.com "Sinatra"
|
|
|
|
|
2013-07-29 03:13:02 -04:00
|
|
|
### Functional-Style DSL Objects
|
2013-07-29 00:37:03 -04:00
|
|
|
|
|
|
|
Sometimes, you want to use an object as a DSL, but it doesn't quite fit the
|
|
|
|
[imperative](http://en.wikipedia.org/wiki/Imperative_programming) pattern shown
|
|
|
|
above.
|
|
|
|
|
|
|
|
Instead of methods like
|
|
|
|
[Array#push](http://www.ruby-doc.org/core-2.0/Array.html#method-i-push), which
|
|
|
|
modifies the object at hand, it has methods like
|
|
|
|
[String#reverse](http://www.ruby-doc.org/core-2.0/String.html#method-i-reverse),
|
|
|
|
which returns a new object without touching the original. Perhaps it's even
|
|
|
|
[frozen](http://www.ruby-doc.org/core-2.0/Object.html#method-i-freeze) in
|
|
|
|
order to enforce [immutability](http://en.wikipedia.org/wiki/Immutable_object).
|
|
|
|
|
|
|
|
Wouldn't it be great if we could just treat these methods as a DSL as well?
|
|
|
|
|
|
|
|
```ruby
|
2013-07-29 03:07:41 -04:00
|
|
|
s = "I'm immutable!".freeze
|
|
|
|
|
|
|
|
with_immutable_string(s) do
|
2013-07-29 00:37:03 -04:00
|
|
|
reverse
|
|
|
|
upcase
|
|
|
|
end
|
|
|
|
#=> "!ELBATUMMI M'I"
|
2013-07-29 03:07:41 -04:00
|
|
|
|
|
|
|
s
|
|
|
|
#=> "I'm immutable!"
|
2013-07-29 00:37:03 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
No problem, just define the method `with_immutable_string` like this:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
def with_immutable_string(str="", &block)
|
|
|
|
Docile.dsl_eval_immutable(str, &block)
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
All set!
|
|
|
|
|
2011-12-06 17:58:09 -05:00
|
|
|
## Features
|
|
|
|
|
2013-07-28 17:07:48 -04:00
|
|
|
1. Method lookup falls back from the DSL object to the block's context
|
|
|
|
2. Local variable lookup falls back from the DSL object to the block's
|
|
|
|
context
|
|
|
|
3. Instance variables are from the block's context only
|
|
|
|
4. Nested DSL evaluation, correctly chaining method and variable handling
|
|
|
|
from the inner to the outer DSL scopes
|
2013-07-29 00:37:03 -04:00
|
|
|
5. Alternatives for both imperative and functional styles of DSL objects
|
2011-12-06 17:58:09 -05:00
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
$ gem install docile
|
|
|
|
```
|
|
|
|
|
2013-07-29 03:13:02 -04:00
|
|
|
## Links
|
|
|
|
* [Source](https://github.com/ms-ati/docile)
|
|
|
|
* [Documentation](http://rubydoc.info/gems/docile)
|
|
|
|
* [Bug Tracker](https://github.com/ms-ati/docile/issues)
|
2011-12-06 17:58:09 -05:00
|
|
|
|
2012-10-28 13:23:46 -04:00
|
|
|
## Status
|
|
|
|
|
2013-09-23 14:46:24 -04:00
|
|
|
Works on [all ruby versions since 1.8.7](https://github.com/ms-ati/docile/blob/master/.travis.yml).
|
2012-10-28 13:23:46 -04:00
|
|
|
|
2011-12-06 17:58:09 -05:00
|
|
|
## Note on Patches/Pull Requests
|
|
|
|
|
|
|
|
* Fork the project.
|
2013-07-28 17:07:48 -04:00
|
|
|
* Setup your development environment with:
|
|
|
|
`gem install bundler; bundle install`
|
2011-12-06 17:58:09 -05:00
|
|
|
* Make your feature addition or bug fix.
|
2013-07-28 17:07:48 -04:00
|
|
|
* Add tests for it. This is important so I don't break it in a future version
|
|
|
|
unintentionally.
|
2011-12-06 17:58:09 -05:00
|
|
|
* Commit, do not mess with rakefile, version, or history.
|
2013-07-28 17:07:48 -04:00
|
|
|
(if you want to have your own version, that is fine but bump version in a
|
|
|
|
commit by itself I can ignore when I pull)
|
2011-12-06 17:58:09 -05:00
|
|
|
* Send me a pull request. Bonus points for topic branches.
|
|
|
|
|
|
|
|
## Copyright
|
|
|
|
|
2013-04-01 17:35:38 -04:00
|
|
|
Copyright (c) 2012-2013 Marc Siegel. See LICENSE for details.
|