Module: Inflector
- Defined in:
- lib/volt/extra_core/inflector/methods.rb,
lib/volt/extra_core/inflector/inflections.rb
Overview
The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept in inflections.rb.
Defined Under Namespace
Classes: Inflections
Class Method Summary collapse
-
.inflections(locale = :en) ⇒ Object
Yields a singleton instance of Inflector::Inflections so you can specify additional inflector rules.
-
.pluralize(word, locale = :en) ⇒ Object
Returns the plural form of the word in the string.
-
.singularize(word, locale = :en) ⇒ Object
The reverse of
pluralize
, returns the singular form of a word in a string.
Class Method Details
.inflections(locale = :en) ⇒ Object
Yields a singleton instance of Inflector::Inflections so you can specify additional inflector rules. If passed an optional locale, rules for other languages can be specified. If not specified, defaults to :en
. Only rules for English are provided.
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.uncountable 'rails'
end
196 197 198 199 200 201 202 |
# File 'lib/volt/extra_core/inflector/inflections.rb', line 196 def self.inflections(locale = :en) if block_given? yield Inflections.instance(locale) else Inflections.instance(locale) end end |
.pluralize(word, locale = :en) ⇒ Object
Returns the plural form of the word in the string.
If passed an optional locale
parameter, the word will be pluralized using rules defined for that language. By default, this parameter is set to :en
.
'post'.pluralize # => "posts"
'octopus'.pluralize # => "octopi"
'sheep'.pluralize # => "sheep"
'words'.pluralize # => "words"
'CamelOctopus'.pluralize # => "CamelOctopi"
'ley'.pluralize(:es) # => "leyes"
20 21 22 |
# File 'lib/volt/extra_core/inflector/methods.rb', line 20 def self.pluralize(word, locale = :en) apply_inflections(word, inflections(locale).plurals) end |
.singularize(word, locale = :en) ⇒ Object
The reverse of pluralize
, returns the singular form of a word in a string.
If passed an optional locale
parameter, the word will be pluralized using rules defined for that language. By default, this parameter is set to :en
.
'posts'.singularize # => "post"
'octopi'.singularize # => "octopus"
'sheep'.singularize # => "sheep"
'word'.singularize # => "word"
'CamelOctopi'.singularize # => "CamelOctopus"
'leyes'.singularize(:es) # => "ley"
37 38 39 |
# File 'lib/volt/extra_core/inflector/methods.rb', line 37 def self.singularize(word, locale = :en) apply_inflections(word, inflections(locale).singulars) end |