我有以下代码,并尝试了一整天将类方法重构为一个单独的模块,以与我的所有模型类共享功能。
class Merchant
include DataMapper::Resource
property :id, Serial
[...]
class << self
@allowed_properties = [:id,:vendor_id, :identifier]
alias_method :old_get, :get
def get *args
[...]
end
def first_or_create_or_update attr_hash
[...]
end
end
end
我想归档类似的内容:
class Merchant
include DataMapper::Resource
include MyClassFunctions
[...]
end
module MyClassFunctions
def get [...]
def first_or_create_or_update[...]
end
=> Merchant.allowed_properties = [:id]
=> Merchant.get( :id=> 1 )
但不幸的是,我的红宝石技能太差了。我读了很多东西(例如这里),现在我更加困惑了。我偶然发现了以下两点:
alias_method
将失败,因为它将在DataMapper::Resource
模块中动态定义。- 如何获得
allowed_properties
包含模块的类方法?
红宝石的方法是什么?
提前谢谢了。