Module: Kirico::Helper
- Included in:
- ApplicationRecord, BaseFDManagementRecord
- Defined in:
- lib/kirico/models/helper.rb
Instance Method Summary collapse
-
#define_code_mapper_method(*fields) ⇒ Object
指定したフィールドの値と区分値の値とをマッピングする メソッドを生やす。 対応表は models/mappers.ja.yml 参照.
-
#define_format_date_method(*fields) ⇒ Object
指定した date 型のフィールドを整形するメソッドを生やす.
-
#define_in_k_method(*fields, op: :round, precision: 0) ⇒ Object
10^3 を単位とした値を返すメソッドを生やす.
Instance Method Details
#define_code_mapper_method(*fields) ⇒ Object
指定したフィールドの値と区分値の値とをマッピングするメソッドを生やす。対応表は models/mappers.ja.yml 参照
例:
cause_cd フィールドをマッピングするメソッド
def mapped_birth_at_era
value = object.send('cause_cd').try(:to_sym)
class_name = object.class.underscore
I18n.t("mappers.#{class_name}")[attr_name][value]
end
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/kirico/models/helper.rb', line 101 def define_code_mapper_method(*fields) fields.each do |attr_name| define_method("mapped_#{attr_name}") do value = send(attr_name) return if value.nil? class_name = self.class.name.underscore @mappers ||= YAML.load_file(File.join(__dir__, 'mappers.yml')) @mappers['mappers'][class_name.to_s].try(:[], attr_name.to_s).try(:[], value.to_s) end end end |
#define_format_date_method(*fields) ⇒ Object
指定した date 型のフィールドを整形するメソッドを生やす
例:
created_at メソッドを整形
# 2015/04/09 => 20150409 def fmt_ymd_created_at
return nil if submit_at.nil?
submit_at.try(:to_era, '%Y%m%d')
end
# 2015/04/09 => 270409 def fmt_era_ymd_created_at
return nil if submit_at.nil?
submit_at.try(:to_era, '%E%m%d')
end
# 2015/04/09 => H def submit_at_era_nengo
submit_at.try(:to_era, '%o%E').try(:[], 0)
end
# 2015/04/09 => 平成def submit_at_era_nengo_kanji
submit_at.try(:to_era, '%O%E').try(:gsub, /\d+\z/, '')
end
# 2015/04/09 => 2015 def submit_at_year
submit_at.try(:strftime, '%Y')
end
# 2015/04/09 => 04 def submit_at_month
submit_at.try(:strftime, '%m')
end
2015/04/09 => 09 def submit_at_day
submit_at.try(:strftime, '%d')
end
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/kirico/models/helper.rb', line 57 def define_format_date_method(*fields) fields.each do |attr_name| define_method("fmt_ymd_#{attr_name}") do send(attr_name).try(:strftime, '%Y%m%d') end define_method("fmt_era_ymd_#{attr_name}") do send(attr_name).try(:to_era, '%E%m%d') end define_method("#{attr_name}_era_nengo") do send(attr_name).try(:to_era, '%o%E').try(:[], 0) end define_method("#{attr_name}_era_nengo_kanji") do send(attr_name).try(:to_era, '%O%E').try(:gsub, /\d+\z/, '') end define_method("#{attr_name}_era_year") do send(attr_name).try(:to_era, '%E') end define_method("#{attr_name}_month") do send(attr_name).try(:strftime, '%m') end define_method("#{attr_name}_day") do send(attr_name).try(:strftime, '%d') end end end |
#define_in_k_method(*fields, op: :round, precision: 0) ⇒ Object
10^3 を単位とした値を返すメソッドを生やす
例: def monthly_income_currency_in_k
return nil if object.monthly_income_currency.nil?
return 0 if object.monthly_income_currency == 0
(object.monthly_income_currency.to_f / 1000).round(0)
end
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/kirico/models/helper.rb', line 121 def define_in_k_method(*fields, op: :round, precision: 0) fields.each do |attr_name| define_method("#{attr_name}_in_k") do original = send(attr_name) return nil if original.nil? # 浮動小数点対応 n = BigDecimal(original.to_s) / BigDecimal('1000') x = case op when :round n.send(op, precision) else n.send(op) end format("%.#{precision}f", x) end end end |