新手问题 在 JSON 中嵌入 Ruby 代码,应该如何写

u1351384616 · October 03, 2015 · Last by lb563 replied at October 06, 2015 · 2679 hits

如题: 类似代码如下

[{"name":"start", "operator": "self", "next": "record.user.employee.position == 1 and 2 or 1"}
{"name": "一级审批", "operator": "部门经理#Position", "next": "record.days > 1 and 2 or -1"},
{"name": "二级审批", "operator": "部门总监#Position", "next": "-1"}]

想在 next 中加入更加复杂的 ruby 代码如何写?

写个方法不成么?

{"name":"start", "operator": "self", "next": <<EOF
#your code
EOF
}

这样?

#1 楼 @ripple 这个写法可以,但是复杂了,需要多行代码?

用 erb 处理下?

绝对不可以这样写。这个耦合程度就好比上了螺丝钉还生锈了。

JSON 给出足够的信息就行了,比如加个 type 什么的,解码的部分自己根据信息决定用什么代码来处理。

json 裡面嵌代碼會讓我感覺 sick ... next 裡面放方法名行不?

json 中嵌入 ruby 代码真心不是啥好设计,lz 更应该考虑是不是设计的的问题。

找下 JSON builder? 比如 jbuilder ?

@u1351384616 会不会很危险? [{"name":”rm all", "operator": “` rm -rf / `", "next": “game over!”}] 如果没有写好。那么会不会出现这种情况?

You need to Sign in before reply, if you don't have an account, please Sign up first.