Class: Mack::Utils::Inflector
- 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
-
.inflections {|Mack::Utils::Inflector.instance| ... } ⇒ Object
Yields up Mack::Utils::Inflector.instance.
Instance Method Summary collapse
-
#initialize ⇒ Inflector
constructor
:nodoc:.
-
#irregular(rule, replacement) ⇒ Object
Adds a irregular rule to the system.
-
#plural(rule, replacement) ⇒ Object
Adds a plural rule to the system.
-
#pluralize(word) ⇒ Object
Returns the singular version of the word, if possible.
-
#singular(rule, replacement) ⇒ Object
Adds a singular rule to the system.
-
#singularize(word) ⇒ Object
Returns the singular version of the word, if possible.
-
#uncountable(*args) ⇒ Object
Adds a uncountable word, or words, to the system.
Constructor Details
#initialize ⇒ Inflector
: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
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
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
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 |