やりたいこと

  • modelが保持する値を指定の方法で正規化したい。
  • 一律で前後空白削除するのではなく、メールの場合は空白削除、名前の場合は半角を大文字にする。
  • これらの処理を共通化したい。
  • has_one :user のように、modelに記載したい

参考

  def name=(value)
    # do something
    @name = value
  end
インスタンス変数に代入する。
    def attributes
      @attributes ||= superclass.respond_to?(:attributes, true) ? superclass.__send__(:attributes) : Set.new
    end

    def attr_accessor(*names)
      super
      attributes.merge(names.map(&:to_sym))
    end
module Module
  extend ActiveSupport::Concern

  class_methods do
    ...
  end
end
# eval("@#{name} = '#{normalized_val}'")
instance_variable_set("@#{name}".to_sym, normalized_val)