Class: Mack::Utils::Inflector

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/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')

  inflect.uncountable(%w(fish sheep deer offspring))
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInflector

:nodoc:



28
29
30
31
32
33
# File 'lib/utils/inflector.rb', line 28

def initialize # :nodoc:
  @plural_rules = []
  @singular_rules = []
  @irregular_rules = []
  @uncountable_rules = []
end

Class Method Details

.inflections {|Mack::Utils::Inflector.instance| ... } ⇒ Object

Yields up Mack::Utils::Inflector.instance

Yields:



121
122
123
# File 'lib/utils/inflector.rb', line 121

def inflections
  yield Mack::Utils::Inflector.instance
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


66
67
68
69
70
71
72
# File 'lib/utils/inflector.rb', line 66

def irregular(rule, replacement)
  @irregular_rules << {:rule => rule, :replacement => replacement}
  # do the reverse so you get:
  # person => people
  # people => person
  @irregular_rules << {:rule => replacement, :replacement => rule}
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


43
44
45
# File 'lib/utils/inflector.rb', line 43

def plural(rule, replacement)
  @plural_rules << {:rule => rule, :replacement => 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"


102
103
104
# File 'lib/utils/inflector.rb', line 102

def pluralize(word)
  do_work(word, @plural_rules)
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


55
56
57
# File 'lib/utils/inflector.rb', line 55

def singular(rule, replacement)
  @singular_rules << {:rule => rule, :replacement => 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"


92
93
94
# File 'lib/utils/inflector.rb', line 92

def singularize(word)
  do_work(word, @singular_rules)
end

#uncountable(*args) ⇒ Object

Adds a uncountable word, or words, to the system.

Example:

Mack::Utils::Inflector.inflections do |inflect|
  inflect.uncountable(%w(fish sheep deer offspring))
end


80
81
82
83
84
# File 'lib/utils/inflector.rb', line 80

def uncountable(*args)
  [args].flatten.each do |word|
    @uncountable_rules << word.downcase
  end
end