0
Eval()用於在Rails 4.2模型payment_request
中評估workflow
塊。下面是使用eval
前workflow
塊:eval中的未定義方法()
class PaymentRequest < ActiveRecord::Base
include Workflow
def self.load_wf
workflow do
state :initial_state do
event :submit, :transitions_to => :ceo_reviewing
end
state :ceo_reviewing do
event :reject, :transitions_to => :rejected
event :approve, :transitions_to => :approved
end
state :rejected
state :approved
end
end
..........
end
字符串變量spec
被定義爲:
spec = "state :initial_state do
event :submit, :transitions_to => :ceo_reviewing
end
state :ceo_reviewing do
event :reject, :transitions_to => :rejected
event :approve, :transitions_to => :approved
end
state :rejected
state :approved"
和重寫:
class PaymentRequest < ActiveRecord::Base
include Workflow
def self.load_wf(spec)
eval("Workflow::ClassMethods.workflow {#{spec}}")
end
...............
end
的eval
生成錯誤:
undefined method 'workflow' for Workflow::ClassMethods:Module.
workflow
是一個寶石的方法:
module Workflow
module ClassMethods
attr_reader :workflow_spec
.....
def workflow(&specification)
assign_workflow Specification.new(Hash.new, &specification)
end
......
end
end
嘗試添加在EVAL約束力和沒有工作(不知道是否綁定正確使用)。如何使eval
找到方法workflow
&在模型payment_request
中執行它?
我接近這個問題的步驟是step1#使用普通代碼運行'load_wf'並且沒有任何'eval'。 step1工作後,step2#用'eval'代替代碼。引入'eval'後,問題發生在第2步。在第1步中,'workflow'被成功調用,並且我沒有看到mixin問題。正如你在另一篇文章中指出的那樣,我覺得它更可能是一個範圍(綁定)與'eval'。但除了一些定義之外,關於「綁定」的網絡並不多。 – user938363
在發佈之前,我嘗試了'instance_eval','class_eval',它們返回相同的錯誤。 – user938363
你可以做一些測試,比如'instance_eval(「raise self.methods.inspect」)'看看它是否在那裏。它應該是! – tadman