Class: Mack::Utils::Inflector

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mack-facets/utils/inflector.rb

Overview

This class is used to deal with inflection strings. This means taken a string and make it plural, or singular, etc… Inflection rules can be added very easy, and are checked from the bottom up. This means that the last rule is the first rule to be matched. The exception to this, kind of, is ‘irregular’ and ‘uncountable’ rules. The ‘uncountable’ rules are always checked first, then the ‘irregular’ rules, and finally either the ‘singular’ or ‘plural’ rules, depending on what you’re trying to do. Within each of these sets of rules, the last rule in is the first rule matched.

Example:

Mack::Utils::Inflector.inflections do |inflect|
  inflect.plural(/$/, 's')
  inflect.plural(/^(ox)$/i, '\1en')
  inflect.plural(/(phenomen|criteri)on$/i, '\1a')

  inflect.singular(/s$/i, '')
  inflect.singular(/(n)ews$/i, '\1ews')
  inflect.singular(/^(.*)ookies$/, '\1ookie')

  inflect.irregular('person', 'people')
  inflect.irregular('child', 'children')
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inflectionsObject

Yields up Mack::Utils::Inflector.instance



87
88
89
90
91
92
93
# File 'lib/mack-facets/utils/inflector.rb', line 87

def inflections
  if block_given?
    yield Mack::Utils::Inflector.instance
  else
    Mack::Utils::Inflector.instance
  end
end

Instance Method Details

#irregular(rule, replacement) ⇒ Object

Adds a irregular rule to the system.

Example:

Mack::Utils::Inflector.inflections do |inflect|
  inflect.irregular('person', 'people')
  inflect.irregular('child', 'children')
end


58
59
60
61
# File 'lib/mack-facets/utils/inflector.rb', line 58

def irregular(rule, replacement)
  English::Inflect.rule(rule, replacement)
  English::Inflect.word(rule, replacement)
end

#plural(rule, replacement) ⇒ Object

Adds a plural rule to the system.

Example:

Mack::Utils::Inflector.inflections do |inflect|
  inflect.plural(/$/, 's')
  inflect.plural(/^(ox)$/i, '\1en')
  inflect.plural(/(phenomen|criteri)on$/i, '\1a')
end


35
36
37
# File 'lib/mack-facets/utils/inflector.rb', line 35

def plural(rule, replacement)
  English::Inflect.plural_rule(rule, replacement)
end

#pluralize(word) ⇒ Object

Returns the singular version of the word, if possible.

Examples:

Mack::Utils::Inflector.instance.pluralize("army") # => "armies"
Mack::Utils::Inflector.instance.pluralize("person") # => "people"
Mack::Utils::Inflector.instance.pluralize("boat") # => "boats"


79
80
81
# File 'lib/mack-facets/utils/inflector.rb', line 79

def pluralize(word)
  English::Inflect.plural(word)
end

#singular(rule, replacement) ⇒ Object

Adds a singular rule to the system.

Example:

Mack::Utils::Inflector.inflections do |inflect|
  inflect.singular(/s$/i, '')
  inflect.singular(/(n)ews$/i, '\1ews')
  inflect.singular(/^(.*)ookies$/, '\1ookie')
end


47
48
49
# File 'lib/mack-facets/utils/inflector.rb', line 47

def singular(rule, replacement)
  English::Inflect.singular_rule(rule, replacement)
end

#singularize(word) ⇒ Object

Returns the singular version of the word, if possible.

Examples:

Mack::Utils::Inflector.instance.singularize("armies") # => "army"
Mack::Utils::Inflector.instance.singularize("people") # => "person"
Mack::Utils::Inflector.instance.singularize("boats") # => "boat"


69
70
71
# File 'lib/mack-facets/utils/inflector.rb', line 69

def singularize(word)
  English::Inflect.singular(word)
end