1
ユーザ入力パラメータをTimeオブジェクトに変換したいだけなので、そのための関心事を定義しようとしています。Raster上でConcernのインスタンスメソッドを動的に定義する
class Foo < ActiveRecord::Base
include DateAttribute
attr_date :date_column_1, :date_column_2
end
しかし、私はその懸念を書くとき、私はモジュールのclass_methodsブロックのインスタンスメソッドを定義する方法という問題を抱えて。
module DateAttribute
extend ActiveSupport::Concern
included do
class_attribute :_attr_date, instance_accessor: false
self._attr_date = []
end
class_methods do
def attr_date(*attributes)
self._attr_date = Set.new(attributes.map(&:to_s))
# how to dynamic define setter methods by loop attributes here
end
def date_attributes
self._attr_date
end
end
end
ありがとうございます。
おかげから動的メソッドの作成ガイドを見つけることができますです!私はこれを試しました。 'attr_date'が' attr_accessor'の後に呼び出されなければならないことを知っています。あるいは、定義されたメソッドが再びオーバーライドされます。 –