2010-12-10 40 views
2

我正在寻找一个简单的例子,说明如何使用Ruby和正则表达式模式匹配来编写内部DSL。类似西纳特拉如何处理路线如何使用Ruby + Regex编写内部自然语言DSL?

get '/say/*/to/*' do 
    # Some Ruby code here 
end 

类似的还有黄瓜是如何处理的步骤定义:

Given /^I have (\d+) cucumbers in my belly$/ do |cukes| 
    # Some Ruby code here 
end 

我在商不感兴趣,或流利的方法链接。基本上我想要一个Ruby类,它看起来是这样的:

class SpecVocabulary 

    match ‘pattern’ do 
     # Some Ruby code here 
    end 

    # Or, using a different keyword 
    phrase ‘pattern’ do 
     # Some Ruby code here 
    end 
end 

我挣扎什么是配线起来这使得SpecVocabular类自动匹配模式并填写其数据的代码。

我希望有人有一个简单的例子来说明如何做到这一点,我试图避免必须在Sinatra和黄瓜的源代码中潜水。

顺便说一句,我已经有自然语言定义,但我故意省略它。

回答

1

下面是关于在Ruby中创建领域特定语言的综合博客文章:

http://www.daniel-azuma.com/blog/view/z3bqa0t01uugg1/implementing_dsl_blocks

顺便说一句,这是不常见的一类中使用你的DSL,我想它宁愿像:

vocabulary :SpecVocabulary do 
    match 'pattern' do 
    # Some Ruby code here 
    end 

    # Or, using a different keyword 
    phrase 'pattern' do 
    # Some Ruby code here 
    end 
end