Class | Mack::Utils::Inflector |
In: |
lib/mack-facets/utils/inflector.rb
|
Parent: | Object |
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
Yields up Mack::Utils::Inflector.instance
# File lib/mack-facets/utils/inflector.rb, line 87 87: def inflections 88: if block_given? 89: yield Mack::Utils::Inflector.instance 90: else 91: Mack::Utils::Inflector.instance 92: end 93: end
Adds a irregular rule to the system.
Example:
Mack::Utils::Inflector.inflections do |inflect| inflect.irregular('person', 'people') inflect.irregular('child', 'children') end
# File lib/mack-facets/utils/inflector.rb, line 58 58: def irregular(rule, replacement) 59: English::Inflect.rule(rule, replacement) 60: English::Inflect.word(rule, replacement) 61: end
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
# File lib/mack-facets/utils/inflector.rb, line 35 35: def plural(rule, replacement) 36: English::Inflect.plural_rule(rule, replacement) 37: end
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"
# File lib/mack-facets/utils/inflector.rb, line 79 79: def pluralize(word) 80: English::Inflect.plural(word) 81: end
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
# File lib/mack-facets/utils/inflector.rb, line 47 47: def singular(rule, replacement) 48: English::Inflect.singular_rule(rule, replacement) 49: end
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"
# File lib/mack-facets/utils/inflector.rb, line 69 69: def singularize(word) 70: English::Inflect.singular(word) 71: end