Class: Roqua::Type::StrippedString

Inherits:
ActiveModel::Type::ImmutableString
  • Object
show all
Defined in:
lib/roqua/type/stripped_string.rb

Constant Summary collapse

MULTIBYTE_WHITE =

taken from strip_attributes gem Unicode invisible and whitespace characters. The POSIX character class

:space:

corresponds to the Unicode class Z (“separator”). We also

include the following characters from Unicode class C (“control”), which are spaces or invisible characters that make no sense at the start or end of a string:

U+180E MONGOLIAN VOWEL SEPARATOR
U+200B ZERO WIDTH SPACE
U+200C ZERO WIDTH NON-JOINER
U+200D ZERO WIDTH JOINER
U+2060 WORD JOINER
U+FEFF ZERO WIDTH NO-BREAK SPACE
"\u180E\u200B\u200C\u200D\u2060\uFEFF"
MULTIBYTE_SPACE =
/[[:space:]#{MULTIBYTE_WHITE}]/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(allow_empty: false) ⇒ StrippedString

Returns a new instance of StrippedString.



22
23
24
25
# File 'lib/roqua/type/stripped_string.rb', line 22

def initialize(allow_empty: false)
  super()
  @allow_empty = allow_empty
end

Instance Method Details

#cast(value) ⇒ Object



27
28
29
30
31
# File 'lib/roqua/type/stripped_string.rb', line 27

def cast(value)
  return unless value
  value = super(value).gsub(/\A#{MULTIBYTE_SPACE}+|#{MULTIBYTE_SPACE}+\z/, "").freeze
  value.blank? && !@allow_empty ? nil : value
end