0

我可以覆盖 DataMapper 的保存、删除、销毁!使用模块的方法,例如:

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方法,当这些方法找不到时base.class_eval do

所以我可以这样称呼:

User.first_or_create additional_param, name: 'something'
4

1 回答 1

1

您可以通过将其添加到您的class_eval块来覆盖类方法:

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

  # do other things with class methods
end
于 2013-10-17T17:45:07.373 回答