class DocumentHydrator::Inflector::Inflections

Attributes

plurals[R]
uncountables[R]

Public Class Methods

instance() click to toggle source
# File lib/document_hydrator/inflector/inflections.rb, line 5
def self.instance
  @__instance__ ||= new
end
new() click to toggle source
# File lib/document_hydrator/inflector/inflections.rb, line 11
def initialize
  @plurals, @uncountables, @humans = [], [], []
end

Public Instance Methods

irregular(singular, plural) click to toggle source

Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used for strings, not regular expressions. You simply pass the irregular in singular and plural form.

Examples:

irregular 'octopus', 'octopi'
irregular 'person', 'people'
# File lib/document_hydrator/inflector/inflections.rb, line 29
def irregular(singular, plural)
  @uncountables.delete(singular)
  @uncountables.delete(plural)
  if singular[0,1].upcase == plural[0,1].upcase
    plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
    plural(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + plural[1..-1])
  else
    plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
    plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
    plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
    plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
  end
end
plural(rule, replacement) click to toggle source

Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression. The replacement should always be a string that may include references to the matched data from the rule.

# File lib/document_hydrator/inflector/inflections.rb, line 17
def plural(rule, replacement)
  @uncountables.delete(rule) if rule.is_a?(String)
  @uncountables.delete(replacement)
  @plurals.insert(0, [rule, replacement])
end
uncountable(*words) click to toggle source

Add uncountable words that shouldn’t be attempted inflected.

Examples:

uncountable "money"
uncountable "money", "information"
uncountable %w( money information rice )
# File lib/document_hydrator/inflector/inflections.rb, line 49
def uncountable(*words)
  (@uncountables << words).flatten!
end