質問

I could overwrite DataMapper's save, delete, destroy! method using module, such as:

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 

how to overwrite create and first_or_create method, when those methods are not found on base.class_eval do?

So i could call something like this:

User.first_or_create additional_param, name: 'something'
役に立ちましたか?

解決

You can override class methods by adding this to your class_eval block:

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

  # do other things with class methods
end
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top