Module: Comodule::CustomizeClass::StringCustom
- Defined in:
- lib/comodule/customize_class/string_custom.rb
Instance Method Summary collapse
- #ascii_space ⇒ Object
- #ascii_space! ⇒ Object
- #digitalize ⇒ Object
- #digitalize! ⇒ Object
- #ltrim ⇒ Object
- #ltrim! ⇒ Object
- #rtrim ⇒ Object
- #rtrim! ⇒ Object
-
#single_space ⇒ Object
全角または半角のスペースが連続する場合は一つの半角スペースにする。.
- #single_space! ⇒ Object
-
#standardize(_single_space = single_space) ⇒ Object
NKF.nkfで“½”などの文字が失われることがあるので、文字数が変化してしまったら一文字ずつの変換を行う。 半角カタカナの濁点、半濁点により文字数が変化した場合も一文字ずつの処理にする。.
- #standardize! ⇒ Object
-
#standardize_delicate(_single_space = single_space) ⇒ Object
1文字ずつ変換するので、当然パフォーマンスが低い。.
-
#to_token(hsh = {}) ⇒ Object
空白文字をワイルドカードに置き換えて検索ワードを作る。 ex.
- #to_token!(*args) ⇒ Object
- #trim ⇒ Object
- #trim! ⇒ Object
Instance Method Details
#ascii_space ⇒ Object
39 40 41 |
# File 'lib/comodule/customize_class/string_custom.rb', line 39 def ascii_space gsub(/\p{Z}/u, ?\s) end |
#ascii_space! ⇒ Object
43 44 45 46 47 48 |
# File 'lib/comodule/customize_class/string_custom.rb', line 43 def ascii_space! str = ascii_space return nil if str == self replace str self end |
#digitalize ⇒ Object
118 119 120 |
# File 'lib/comodule/customize_class/string_custom.rb', line 118 def digitalize standardize.gsub(/[^0-9]/,"") end |
#digitalize! ⇒ Object
122 123 124 125 126 127 |
# File 'lib/comodule/customize_class/string_custom.rb', line 122 def digitalize! str = digitalize return nil if str == self replace str self end |
#ltrim ⇒ Object
6 7 8 |
# File 'lib/comodule/customize_class/string_custom.rb', line 6 def ltrim lstrip.sub(/^\p{Z}+/mu, '') end |
#ltrim! ⇒ Object
10 11 12 13 14 15 |
# File 'lib/comodule/customize_class/string_custom.rb', line 10 def ltrim! str = ltrim return nil if str == self replace str self end |
#rtrim ⇒ Object
17 18 19 |
# File 'lib/comodule/customize_class/string_custom.rb', line 17 def rtrim rstrip.sub(/\p{Z}+$/mu, '') end |
#rtrim! ⇒ Object
21 22 23 24 25 26 |
# File 'lib/comodule/customize_class/string_custom.rb', line 21 def rtrim! str = rtrim return nil if str == self replace str self end |
#single_space ⇒ Object
全角または半角のスペースが連続する場合は一つの半角スペースにする。
51 52 53 |
# File 'lib/comodule/customize_class/string_custom.rb', line 51 def single_space trim.gsub(/\p{Z}+/u, ?\s) end |
#single_space! ⇒ Object
55 56 57 58 59 60 |
# File 'lib/comodule/customize_class/string_custom.rb', line 55 def single_space! str = single_space return nil if str == self replace str self end |
#standardize(_single_space = single_space) ⇒ Object
NKF.nkfで“½”などの文字が失われることがあるので、文字数が変化してしまったら一文字ずつの変換を行う。 半角カタカナの濁点、半濁点により文字数が変化した場合も一文字ずつの処理にする。
64 65 66 67 68 |
# File 'lib/comodule/customize_class/string_custom.rb', line 64 def standardize(_single_space=single_space) before = _single_space || trim.ascii_space after = NKF.nkf( '-Wwxm0Z0', NKF.nkf('-WwXm0', before) ) before.size == after.size ? after : standardize_delicate(_single_space) end |
#standardize! ⇒ Object
94 95 96 97 98 99 |
# File 'lib/comodule/customize_class/string_custom.rb', line 94 def standardize! str = standardize return nil if str == self replace str self end |
#standardize_delicate(_single_space = single_space) ⇒ Object
1文字ずつ変換するので、当然パフォーマンスが低い。
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/comodule/customize_class/string_custom.rb', line 71 def standardize_delicate(_single_space=single_space) str = _single_space || trim.ascii_space str_array = [] # 濁点と半濁点が一文字として変換されることを避ける。 str_chars = str.chars.to_enum loop do s = str_chars.next # 濁点、半濁点は直前の文字と組み合わせる。 if !str_array.empty? && s =~ /(゙|゚)/ s = str_array.pop+s end str_array << s end re_str = "" str_array.each do |char| re_char = NKF.nkf( '-Wwxm0Z0', NKF.nkf('-WwXm0', char) ) re_str << (re_char.present? ? re_char : char) end re_str end |
#to_token(hsh = {}) ⇒ Object
空白文字をワイルドカードに置き換えて検索ワードを作る。 ex. “株 山 のり” -> “株%山%のり%” デフォルトは前方一致、部分一致にしたければ:prefixに“%”を渡す。
104 105 106 107 108 109 |
# File 'lib/comodule/customize_class/string_custom.rb', line 104 def to_token(hsh={}) prefix = hsh[:prefix] || "" suffix = hsh[:suffix] || ?% str = sub(/^%+/,'').sub(/%+$/,'') prefix + str.split(/\p{Z}+/u).join(?%) + suffix end |
#to_token!(*args) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/comodule/customize_class/string_custom.rb', line 111 def to_token!(*args) str = to_token(*args) return nil if str == self replace str self end |
#trim ⇒ Object
28 29 30 |
# File 'lib/comodule/customize_class/string_custom.rb', line 28 def trim ltrim.rtrim end |
#trim! ⇒ Object
32 33 34 35 36 37 |
# File 'lib/comodule/customize_class/string_custom.rb', line 32 def trim! str = trim return nil if str == self replace str self end |