Last active
May 31, 2021 07:29
-
-
Save alpaca-tc/8b65ca576afafb70a19d0a5581fa0632 to your computer and use it in GitHub Desktop.
空白を除去するString型
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StrippedString < ActiveModel::Type::String | |
# 左右の空白を除去した文字列クラス | |
# | |
# @param [Object] value | |
# | |
# @return [String, nil] | |
def cast(value) | |
super(value)&.strip&.presence | |
end | |
# 与えられた値をDBのクエリ用に変換する | |
# | |
# @param value [Object] | |
# | |
# @return [String, NilClass] | |
def serialize(value) | |
cast(value) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment