我有两个“引擎”处理特定的工作,他们每个人都使用不同的工具/ apis。我有一个Engine
模型,它保存它们之间的通用数据/行为,但每个Engine
都有其他特定的字段/方法。如何动态选择一个Rails模型来实例化?
class Engine
field :name
field :status
end
class DefaultEngine
field :job_id
def process
# default engine process
# ...
end
end
class SpecialEngine
def process
# special engine process
# ...
end
end
class Site
field :engine, type: String, default: '::DefaultEngine'
end
我想要做的就是让Engine
负责继承正确的引擎,这取决于site.engine
值。例如,在控制器中,我想要执行以下操作:
def start
job = Engine.create()
job.process
end
我不想直接引用任何引擎。相反,我想Engine
负责找出哪个是正确的引擎使用。什么是处理这种情况的方法,以便Engine.create
可以返回SpecialEngine
或DefaultEngine
的实例。