你好
我尝试构建一些动态定义的方法并链接一些范围方法,例如:
define_method "#{instance_name_method}" do
Kernel.const_get(model_name).___some_chaining methods basd on condition
end
一个想法是这样的:
method_action = model_name #ex Post
['latest', 'old', 'deleted','latest_deleted','archived'].each do |prefix|
method_action << ".deleted" if prefix.match('deleted')
method_action << ".latest" if prefix.match('latest')
method_action << ".old" if prefix.match('old')
define_method "#{prefix}_#{instance_name_method}" do
eval( method_action)
end
end
在帖子中,我们有最新的、旧的...
现在我们可以调用如下方法:
Post.latest or Post.old_archived etc...
我的问题是:
有没有更好的方法来做到这一点?(类似于活动记录查找但没有method_missing)这有点难看......
如何动态链接方法?
我已经知道 send('method',var) 但我不知道如何根据条件从字符串中加入这些方法......
谢谢