2013-10-17 2 views
0

DataMapper의 저장, 삭제, 삭제를 덮어 쓸 수 있습니다! 이러한 방법은 base.class_eval do에없는 경우Ruby DataMapper의 create 및 first_or_create 메소드를 덮어 쓰는 방법은 무엇입니까?

require 'data_mapper' 

module Record 
    def self.included(base) 
    base.class_eval do 
     include DataMapper::Resource 
     property :id, DataMapper::Property::Serial 
     alias :parent_save :save 
     def save bar 
     # do additional thing with bar 
     end 
    end 
    end 
end 

class User 
    include Record 
    property :name,String 
end 

DataMapper.finalize 

# so i could call something like this: 
x = User.new 
x.name = 'something' 
x.save 123 

방법 createfirst_or_create 및 방법을 덮어하려면 다음 방법과 같은 모듈을 사용?

그래서 나는이 같은 것을 호출 할 수

User.first_or_create additional_param, name: 'something' 

답변

1

당신은 당신의 class_eval 블록이를 추가하여 클래스의 메소드를 오버라이드 (override) 할 수 있습니다

class << self 
    alias :parent_first_or_create, :first_or_create 
    def first_or_create 
    # ... 
    end 

    # do other things with class methods 
end 
+0

가 작동! : 3 고마워! – Kokizzu