Last active
December 5, 2015 23:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'docile' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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