Skip to content

Instantly share code, notes, and snippets.

@ms-ati
Last active December 5, 2015 23:15
Show Gist options
  • Save ms-ati/faa78ca655b3ff7fc4ec to your computer and use it in GitHub Desktop.
Save ms-ati/faa78ca655b3ff7fc4ec to your computer and use it in GitHub Desktop.
An example of using Docile for @lohqua re https://github.com/ms-ati/docile/issues/19
source 'https://rubygems.org'
gem 'docile'
#
# Here's the code we actually want to run, which uses the Pizza DSL
#
# To run:
# bundle install # so that you have docile
# bundle exec ruby -I. my_order.rb
#
require 'pizza_dsl'
@sauce_level = :extra
my_pizza = pizza do
cheese
pepperoni
sauce @sauce_level
end
require 'pp'
pp my_pizza
Pizza = Struct.new(:cheese, :pepperoni, :bacon, :sauce)
class PizzaBuilder
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
def build
Pizza.new(!!@cheese, !!@pepperoni, !!@bacon, @sauce)
end
end
require 'docile'
require 'pizza_builder'
def pizza(&block)
Docile.dsl_eval(PizzaBuilder.new, &block).build
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment