我有兩個「引擎」處理特定的工作,他們每個人都使用不同的工具/ 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
的實例。