だから私は管理しなければならない多数の日時に必要ないくつかの日時メソッドを構築するために欠落している方法を使用することを考えています。
私はこのメソッドを Ryan Bates の仮想属性に基づいており、まだ解決していない小さな問題を除いて、このメタプログラミングの課題 (私の 1 つ目!) を解決したと信じています...
私の方法のこの部分では、=
予期しないはずの に関するエラーが発生しています...
define_method(save_method) do
self.send(attribute) = Chronic.parse(args[0], context: :past) if instance_variable_get("@#{method_name}").present?
end
)
メソッドは代わりにa を期待しているはずです...しかし、私はそれを理解することができませんでした。
def method_missing(method_name, *args)
if method_name.to_s.match(/^chronic_(\w+)/)
save_method = "save_#{method_name}"
validate_method = "check_#{method_name}"
attribute = method_name.to_s.gsub(/^chronic_/, '').to_sym
self.class.class_eval do
attr_accessible method_name.to_sym
attr_writer method_name.to_sym
validate validate_method.to_sym
before_save save_method.to_sym
define_method(save_method) do
self.send(attribute) = Chronic.parse(args[0], context: :past) if instance_variable_get("@#{method_name}").present?
end
define_method(validate_method) do
if instance_variable_get("@#{method_name}").present? && Chronic.parse(args[0], context: :past).nil?
errors.add :released_at_text, "cannot be parsed"
end
end
define_method(method_name) do
instance_variable_get("@#{method_name}") || attribute
end
end
puts attribute
send(method_name)
else
super
end
end