Module: QueryStream::Singularize

Defined in:
lib/query_stream/singularize.rb

Overview

英単語の複数形→単数形変換モジュール

Class Method Summary collapse

Class Method Details

.call(word) ⇒ String

複数形の英単語を単数形に変換する

Parameters:

  • word (String)

    変換対象の単語

Returns:

  • (String)

    単数形の単語



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/query_stream/singularize.rb', line 27

def call(word)
  case word.to_s
  in /\Apeople(.*)\z/              then "person#{$1}"    # people → person(不規則)
  in /\A(.+)ies\z/                 then "#{$1}y"         # categories → category
  in /\A(.+)([sxz]|ch|sh)es\z/    then "#{$1}#{$2}"     # branches → branch
  in /\A(.+)ves\z/                 then "#{$1}f"         # shelves → shelf
  in /\A(.+?)s([0-9_].*)\z/        then "#{$1}#{$2}"     # books2 → book2
  in /\A(.+)s\z/                   then $1               # elements → element
  else word                                              # data, sheep(不変)
  end
end