Skip to content

Instantly share code, notes, and snippets.

@gorille
Last active January 17, 2016 20:29
Show Gist options
  • Save gorille/fc2261e9de1cb8e2c23a to your computer and use it in GitHub Desktop.
Save gorille/fc2261e9de1cb8e2c23a to your computer and use it in GitHub Desktop.
skeleton for cutom templating dsl
require "docile"
require "highline/import"
require "erubis"
class MenuGenerator
attr_reader :questions, :fragments
def initialize
@questions = {}
@fragments ||= { }
end
# definess hooks
hooks = [:template, :before_all]
hooks.each do | hook |
# define dsl hook
define_method( hook ) do | string |
@fragments[ hook ] = []
@fragments[hook] << string
end
# define render hook
render = ( 'render_' + hook.to_s ).to_sym
define_method( render ) do
puts 'coucou'
end
end
def question(key, text)
@questions[key] = ask(text)
end
end
@test = MenuGenerator.new
@param_list = {}
def parametre(nom,&block)
@param_list[nom] = block
end
parametre "mon param" do
question :clef1, "valeur de clef 1 ? "
question :clef2, "valeur de clef 2 ? "
template "test <%= clef1 %>"
before_all "test <%= clef2 %>"
end
Docile.dsl_eval( @test, &@param_list["mon param"])
puts @test.questions
# b = binding
# b.local_variable_set :clef1, @test.questions[:clef1]
# b.local_variable_set :clef2, @test.questions[:clef2]
# puts ERB.new(@test.fragments[:template].first).result b
# puts ERB.new(@test.fragments[:before_all].first).result b
puts @test.render_template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment