Module: DocumentHydrator::Inflector

Defined in:
lib/document_hydrator/inflector/inflections.rb

Defined Under Namespace

Classes: Inflections

Class Method Summary collapse

Class Method Details

.inflectionsObject

Yields a singleton instance of Inflector::Inflections so you can specify additional inflector rules.

Example:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable "rails"
end


61
62
63
64
65
66
67
# File 'lib/document_hydrator/inflector/inflections.rb', line 61

def self.inflections
  if block_given?
    yield Inflections.instance
  else
    Inflections.instance
  end
end

.pluralize(word) ⇒ Object

Returns the plural form of the word in the string.

Examples:

"post".pluralize             # => "posts"
"octopus".pluralize          # => "octopi"
"sheep".pluralize            # => "sheep"
"words".pluralize            # => "words"
"CamelOctopus".pluralize     # => "CamelOctopi"


77
78
79
80
81
82
83
84
85
86
# File 'lib/document_hydrator/inflector/inflections.rb', line 77

def self.pluralize(word)
  result = word.to_s.dup

  if word.empty? || inflections.uncountables.include?(result.downcase)
    result
  else
    inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
    result
  end
end