2012-11-08 1 views
3

저는 벽돌 벽을 테스트하여 클래스 재 정의를 테스트하고 있으며, 접근 방법을 모른다. 여기에 내가 테스트하고있어 시나리오 (이 코어 데이터되지는)입니다 :Ruby Motion : 객체 공간에서 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) 내가 그것을 것이라고 기대했던 것을하지 않고, 루비 그냥 Removeable을 다시 열고 MotionModel::Model 모듈의 self.included() 방법을 실행할 수 없습니다 생각 : 여기가 작동 할 수 느꼈다 것입니다.

사양 예제의 컨텍스트에서 처음부터이 클래스의 생성을 에뮬레이션하는 방법에 대한 아이디어가 있습니까?

+0

아마도 멍청한 제안 이었지만 심볼 대신 문자열을 사용해 보았습니까? "이동할 수 있는"? –

+0

'ObjectSend (: remove_const, : Foo)가 정의 되었다면 (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 설정자는 항상 좋은 생각이다. –