As you already know the signatures of the desired methods, it would be better to define them instead of using them method_missing. You can do it like this (inside the class definition):
[:bill_date, :registration_date, :some_other_date].each do |attr|
define_method("#{attr}_human") do
(send(attr) || Date.today).strftime('%b %d, %Y')
end
define_method("#{attr}_human=") do |date_string|
self.send "#{attr}=", Date.strptime(date_string, '%b %d, %Y')
end
end
, , , - method_missing.
, _date, ( ):
column_names.grep(/_date$/)
method_missing ( , ):
def method_missing(method_name, *args, &block)
return super unless /^(.*)_date(=?)/ =~ method_name
if $2.blank?
(send($1) || Date.today).strftime('%b %d, %Y')
else
self.send "#{$1}=", Date.strptime(args[0], '%b %d, %Y')
end
end
, respond_to? true , method_missing ( 1.9 respond_to_missing?).