这种内部DSL语法在Ruby中是否可行?

时间:2012-10-15 06:07:55

标签: ruby dsl

我正在尝试为Ruby创建一个内部DSL,我还没有开始编码,我想知道Ruby中是否可以使用这种语法:

IF more_than: 1, :class smells to: "god class", sense: HIGH and has "no temp attributes", sense: 0.5
THEN problem "some problem"
WITH ponderation percent: 10

还有:

IF more_than: 1, :class
{
    smells to: 'god class', sense: 2
    and
    (
        has 'no temp attributes', sense: 0.5 or
        smells to: 'extensive coupling', sense: 1.5 or
        has 'refused parent Bequest', sense: HIGH or
        smells to: 'tradition breaker', sense: LOW
    )
    and
    (
        has :Method
        {
            smells to: 'extensive coupling', sense: 1.5
        }
    )
}
THEN
{ 
    problem "abusive conceptualization"
}
WITH
{
    ponderation percent: 10
}

更新 :D我仍然在定义DSL的要求,这是我的出发点,我正在考虑自定义解析器或Ruby。你会说什么是更好的想法,为它创建一个自定义解析器或使用Ruby?

2 个答案:

答案 0 :(得分:2)

这是不可能的。问题在于这些结构:

:class to

您不能将参数传递给符号,只传递给方法。

答案 1 :(得分:1)

以下是有效的ruby语法的替代版本:

IF CLASS Student {
    smells to: 'god class', sense: 2 and
    has 'no temp attributes', sense: 0.5 or
    smells to: 'extensive coupling', sense: 1.5 or
    has 'refused parent bequest', sense: HIGH or
    smells to: 'tradition breaker', sense: LOW and
    has_method {
        smells to: 'extensive coupling', sense: 1.5
    }
}
THEN {
    problem 'abusive conceptualization'
}
WITH {
    ponderation percent: 10
}

更新:我一直在检查可能性,可以使用大写的IF,THEN,WITH。