Module: Hanami::Utils::Inflector
- Includes:
- ClassAttribute
- Defined in:
- lib/hanami/utils/inflector.rb
Overview
String inflector
Defined Under Namespace
Classes: IrregularRules
Constant Summary collapse
- BLANK_STRING_MATCHER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Matcher for blank strings
/\A[[:space:]]*\z/.freeze
- A =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'a'.freeze
- CH =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ch'.freeze
- CHES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ches'.freeze
- EAUX =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'eaux'.freeze
- ES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'es'.freeze
- F =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'f'.freeze
- I =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'i'.freeze
- ICE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ice'.freeze
- ICES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ices'.freeze
- IDES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ides'.freeze
- IES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ies'.freeze
- IFE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ife'.freeze
- INA =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ina'.freeze
- IS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'is'.freeze
- IVES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ives'.freeze
- MA =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ma'.freeze
- MATA =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'mata'.freeze
- MEN =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'men'.freeze
- MINA =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'mina'.freeze
- NA =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'na'.freeze
- NON =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'non'.freeze
- O =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'o'.freeze
- OES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'oes'.freeze
- OUSE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ouse'.freeze
- S =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
's'.freeze
- SES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ses'.freeze
- SSES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'sses'.freeze
- TA =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ta'.freeze
- UM =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'um'.freeze
- US =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'us'.freeze
- USES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'uses'.freeze
- VES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'ves'.freeze
- X =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'x'.freeze
- XES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'xes'.freeze
- Y =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'y'.freeze
Class Method Summary collapse
-
.exception(singular, plural) ⇒ Object
Add a custom inflection exception.
-
.inflections(&blk) ⇒ Object
Block for custom inflection rules.
-
.pluralize(string) ⇒ String, NilClass
private
Pluralize the given string.
-
.singularize(string) ⇒ String, NilClass
private
Singularize the given string.
-
.uncountable(*words) ⇒ Object
Add an uncountable word.
Methods included from ClassAttribute
Class Method Details
.exception(singular, plural) ⇒ Object
Add a custom inflection exception
307 308 309 310 |
# File 'lib/hanami/utils/inflector.rb', line 307 def self.exception(singular, plural) singulars.add(plural, singular) plurals.add(singular, plural) end |
.inflections(&blk) ⇒ Object
Block for custom inflection rules.
287 288 289 |
# File 'lib/hanami/utils/inflector.rb', line 287 def self.inflections(&blk) class_eval(&blk) end |
.pluralize(string) ⇒ String, NilClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Pluralize the given string
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/hanami/utils/inflector.rb', line 341 def self.pluralize(string) return string if string.nil? || string.match(BLANK_STRING_MATCHER) case string when plurals plurals.apply(string) when /\A((.*)[^aeiou])ch\z/ $1 + CHES when /\A((.*)[^aeiou])y\z/ $1 + IES when /\A(.*)(ex|ix)\z/ $1 + ICES when /\A(.*)(eau|#{ EAUX })\z/ $1 + EAUX when /\A(.*)x\z/ $1 + XES when /\A(.*)ma\z/ string + TA when /\A(.*)(um|#{ A })\z/ $1 + A when /\A(.*)(ouse|#{ ICE })\z/ $1 + ICE when /\A(buffal|domin|ech|embarg|her|mosquit|potat|tomat)#{ O }\z/i $1 + OES when /\A(.*)(en|#{ INA })\z/ $1 + INA when /\A(.*)(?:([^f]))f[e]*\z/ $1 + $2 + VES when /\A(.*)us\z/ $1 + USES when /\A(.*)non\z/ $1 + NA when /\A((.*)[^aeiou])is\z/ $1 + ES when /\A(.*)ss\z/ $1 + SSES when /s\z/ string else string + S end end |
.singularize(string) ⇒ String, NilClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Singularize the given string
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/hanami/utils/inflector.rb', line 392 def self.singularize(string) return string if string.nil? || string.match(BLANK_STRING_MATCHER) case string when singulars singulars.apply(string) when /\A.*[^aeiou]#{CHES}\z/ string.sub(CHES, CH) when /\A.*[^aeiou]#{IES}\z/ string.sub(IES, Y) when /\A(.*)#{ICE}\z/ $1 + OUSE when /\A.*#{EAUX}\z/ string.chop when /\A(.*)#{IDES}\z/ $1 + IS when /\A(.*)#{US}\z/ $1 + I when /\A(.*)#{SES}\z/ $1 + S when /\A(.*)#{OUSE}\z/ $1 + ICE when /\A(.*)#{MATA}\z/ $1 + MA when /\A(.*)#{OES}\z/ $1 + O when /\A(.*)#{MINA}\z/ $1 + MEN when /\A(.*)#{XES}\z/ $1 + X when /\A(.*)#{IVES}\z/ $1 + IFE when /\A(.*)#{VES}\z/ $1 + F when /\A(.*)#{I}\z/ $1 + US when /\A(.*)ae\z/ $1 + A when /\A(.*)na\z/ $1 + NON when /\A(.*)#{A}\z/ $1 + UM when /[^s]\z/ string else string.chop end end |
.uncountable(*words) ⇒ Object
Add an uncountable word
327 328 329 330 331 |
# File 'lib/hanami/utils/inflector.rb', line 327 def self.uncountable(*words) Array(words).each do |word| exception(word, word) end end |