2012-11-08 40 views
3

我正在運行到一個磚牆測試類重定義,只是不知道如何處理它。下面是我測試的情況(這不是核心數據):Ruby運動:從對象空間完全刪除一個Ruby類

  • 應用與模型版本上運行1級
  • 熱切的程序員通過添加/刪除/重定義列
  • 應用程序運行時修改模型在第2版

模型在那裏我遇到了問題,在模擬實際刪除從內存的應用程序,並從頭開始吧重建。這很重要,因爲當包含MotionModel::Model模塊時,會建立許多與模型有關的事情,並且只會發生一次:模塊包含在類中時。以下是我覺得可能工作:

it "column removal" do 
     class Removeable 
     include MotionModel::Model 
     columns  :name => :string, :desc => :string 
     end 


     @foo = Removeable.create(:name=> 'Bob', :desc => 'who cares anyway?') 


     Removeable.serialize_to_file('test.dat') 

     @foo.should.respond_to :desc 

     Object.send(:remove_const, :Removeable) # Should remove all traces of Removeable 
     class Removeable 
     include MotionModel::model    # Should include this again instead 
     columns  :name => :string,  # of just reopening the old Removeable 
         :address => :string  # class 
     end 


     Removeable.deserialize_from_file # Deserialize old data into new model 


     Removeable.length.should == 1 
     @bar = Removeable.first 
     @bar.should.respond_to :name 
     @bar.should.respond_to :address  
     @bar.should.not.respond_to :desc 


     @bar.name.should == 'Bob' 
     @bar.address.should == nil 
    end 
    end 

不幸的是,Object.send(:remove_const, :Removeable)沒有做什麼,我所願,和Ruby只是認爲它可以重新打開Removeable,而不是運行MotionModel::Model模塊的self.included()方法。

關於如何在規範示例的上下文中從頭開始模擬創建此類的任何想法?

+0

可能是一個愚蠢的建議,但你試過一個字符串,而不是一個符號? 「可移動」? –

+0

我使用Object.send(:remove_const,:Foo)if defined?(Foo)'在我對MotionModel的提交中很好,也許這不再是問題? RubyMotion緩存修復的1.35版本可能已經解決了這個問題。 – aceofspades

回答

3

我想嘗試使用匿名類(你必須告訴MotionModel的表名)。

虛構的例子:

model_before_update = Class.new do 
    # This tells MotionModel the name of the class (don't know if that actually exists) 
    table_name "SomeTable" 
    include MotionModel::Model 
    columns  :name => :string, :desc => :string 
end 

你完全不刪除類,你剛纔定義具有相同表名其他(匿名)類。它

model_after_update = Class.new do 
    table_name "SomeTable" 
    include MotionModel::model 
    columns  :name => :string, 
       :address => :string 
end 

思考,如果有像上面一個表名二傳手,你甚至不需要使用匿名類,在不與RubyMotion工作情況。

+0

這是一個很好的想法。我認爲它會破壞一些代碼,並且比我計劃的更不直觀,但是table_name setter總是一個好主意。 –