我试图为模型创建一个关注点,但这是一个基本的Ruby方法查找问题,而不是Rails问题本身。从模块中的调度表中查找红宝石方法
我希望能够通过选择基于值的某些特征的格式化方法来提供格式化值的功能。在下面的抽象示例中,如果要格式化的值的值为:attribute
且值为:a_characteristic
,我想调用foo
格式化程序。特别是,我想要一个调度表,我可以从特征映射到格式化函数。
这里是我的尝试:
module FormatConcern
FORMATTERS = {
a_characteristic: ->(v){foo v}
}
def format(v)
FORMATTERS[v[:attribute]].call(v)
end
def foo(v)
"A value with #{v[:foo]}"
end
end
class ModelWithConcern
include FormatConcern
end
test_value = {attribute: :a_characteristic,
foo: "bar"}
m = ModelWithConcern.new
puts "formatted value: #{m.format(test_value)}"
当我执行这段代码中,foo
方法查找失败:
$ ruby concern_test.rb
concern_test.rb:3:in `block in <module:FormatConcern>': undefined method `foo' for FormatConcern:Module (NoMethodError)
from concern_test.rb:7:in `call'
from concern_test.rb:7:in `format'
from concern_test.rb:22:in `<main>'
我想我明白红宝石如何查找方法名在一类,但显然我不太了解它!我如何从我的调度表中的lambda函数引用foo
方法?