gpt4 book ai didi

ruby-on-rails - 如何动态声明这些实例方法?

转载 作者:太空宇宙 更新时间:2023-11-03 18:26:22 26 4
gpt4 key购买 nike

我必须在 ActiveRecord 模型上实现这些方法以支持某些所需的 View 功能。

我认为必须有一种更紧凑的方式来声明这些方法,可能涉及符号数组和 Array#each 调用。

有什么建议吗?

(顺便说一句,这些是遗留的列名。)


class Contract < ActiveRecord::Base

def start_date_displayed
self.start_date.present? ? self.start_date.strftime("%-m/%-d/%Y") : ''
end

def start_date_displayed=(input)
if input.present?
self.start_date = Date.strptime input, '%m/%d/%Y'
end
end

def end_date_displayed
self.end_date.present? ? self.end_date.strftime("%-m/%-d/%Y") : ''
end

def end_date_displayed=(input)
if input.present?
self.end_date = Date.strptime input, '%m/%d/%Y'
end
end

def ArrivalDate_displayed
self.ArrivalDate.present? ? self.ArrivalDate.strftime("%-m/%-d/%Y") : ''
end

def ArrivalDate_displayed=(input)
if input.present?
self.ArrivalDate = Date.strptime input, '%m/%d/%Y'
end
end

def Contract_ReceivedDate_displayed
self.Contract_ReceivedDate.present? ? self.Contract_ReceivedDate.strftime("%-m/%-d/%Y") : ''
end

def Contract_ReceivedDate_displayed=(input)
if input.present?
self.Contract_ReceivedDate = Date.strptime input, '%m/%d/%Y'
end
end

def Contract_FinalizedDate_displayed
self.Contract_FinalizedDate.present? ? self.Contract_FinalizedDate.strftime("%-m/%-d/%Y") : ''
end

def Contract_FinalizedDate_displayed=(input)
if input.present?
self.Contract_FinalizedDate = Date.strptime input, '%m/%d/%Y'
end
end

def Committee_Date_displayed
self.Committee_Date.present? ? self.Committee_Date.strftime("%-m/%-d/%Y") : ''
end

def Committee_Date_displayed=(input)
if input.present?
self.Committee_Date = Date.strptime input, '%m/%d/%Y'
end
end

# ...

end

最佳答案

class Contract < ActiveRecord::Base

%w(start_date end_date).each do |attr_name|
class_eval <<-METHOD_DEF
def #{attr_name}_displayed
self.#{attr_name}.present? ? self.#{attr_name}.strftime("%-m/%-d/%Y") : ''
end

def #{attr_name)_displayed=(input)
if input.present?
self.#{attr_name} = Date.strptime #{attr_name}, '%m/%d/%Y'
end
end


METHOD_DEF
end
end

如果只为那些名称以日期结尾的列定义这些方法,那么

class Contract < ActiveRecord::Base

self.column_names.grep(/date$/i).each do |column_name|

class_eval <<-METHOD_DEF
def #{column_name}_displayed
self.#{column_name}.present? ? self.#{column_name}.strftime("%-m/%-d/%Y") : ''
end

def #{column_name)_displayed=(input)
if input.present?
self.#{column_name} = Date.strptime #{column_name}, '%m/%d/%Y'
end
end
METHOD_DEF

end
end

关于ruby-on-rails - 如何动态声明这些实例方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10424903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com