Module: Hanami::Utils::Inflector
- Includes:
- ClassAttribute
- Defined in:
- lib/hanami/utils/inflector.rb
Overview
String inflector
Defined Under Namespace
Classes: IrregularRules
Constant Summary collapse
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- RSE =
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.
"rse"
- RSES =
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.
"rses"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
Class Method Summary collapse
-
.add_to_inflecto(singular, plural) ⇒ Object
private
Since ROM uses Inflecto for it inferences, we need to add an exception to it when one is registered against our Inflector.
-
.exception(singular, plural) ⇒ Object
Adds a custom inflection exception.
-
.inflections(&blk) ⇒ Object
Block for custom inflection rules.
-
.pluralize(string) ⇒ String, NilClass
private
Pluralizes the given string.
-
.singularize(string) ⇒ String, NilClass
private
Singularizes the given string.
-
.uncountable(*words) ⇒ Object
Adds an uncountable word.
Methods included from ClassAttribute
Class Method Details
.add_to_inflecto(singular, plural) ⇒ Object
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.
Since ROM uses Inflecto for it inferences, we need to add an exception to it
when one is registered against our Inflector.
344 345 346 347 348 |
# File 'lib/hanami/utils/inflector.rb', line 344 def self.add_to_inflecto(singular, plural) return unless defined? Inflecto Inflecto.inflections.irregular(singular, plural) end |
.exception(singular, plural) ⇒ Object
Adds a custom inflection exception
335 336 337 338 339 |
# File 'lib/hanami/utils/inflector.rb', line 335 def self.exception(singular, plural) add_to_inflecto(singular, plural) singulars.add(plural, singular) plurals.add(singular, plural) end |
.inflections(&blk) ⇒ Object
Block for custom inflection rules.
315 316 317 |
# File 'lib/hanami/utils/inflector.rb', line 315 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.
Pluralizes the given string
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Style/PerlBackrefs
383 384 385 386 387 388 389 390 391 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 |
# File 'lib/hanami/utils/inflector.rb', line 383 def self.pluralize(string) return string if string.nil? || string =~ Utils::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(buffal|domin|ech|embarg|her|mosquit|potat|tomat)#{O}\z/i $1 + OES when /\A(.*)(fee)\z/ $1 + $2 + S 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.
Singularizes the given string
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity rubocop:disable Style/PerlBackrefs
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/hanami/utils/inflector.rb', line 440 def self.singularize(string) return string if string.nil? || string =~ Utils::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.*#{EAUX}\z/ string.chop when /\A(.*)#{IDES}\z/ $1 + IS when /\A(.*)#{US}\z/ $1 + I when /\A(.*)#{RSES}\z/ $1 + RSE when /\A(.*)#{SES}\z/ $1 + S 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
Adds an uncountable word
365 366 367 368 369 |
# File 'lib/hanami/utils/inflector.rb', line 365 def self.uncountable(*words) Array(words).each do |word| exception(word, word) end end |