私は次のコードを作成し、クラス メソッドを個別のモジュールにリファクタリングして、すべてのモデル クラスと機能を共有するために 1 日を費やしました。
コード ( http://pastie.org/974847 ):
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 )
残念ながら、私の ruby のスキルは低すぎます。私はたくさんのものを読みました(例:ここ)が、今はさらに混乱しています。以下の2点が気になりました。
alias_method
モジュールで動的に定義されるため、失敗しDataMapper::Resource
ます。allowed_properties
モジュールを含むクラスメソッドを取得するには?
ルビーの行方は?
よろしくお願いします。