module Volt::Inflector
Public Class Methods
inflections(locale = :en) { |instance| ... }
click to toggle source
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
# File lib/volt/extra_core/inflector/inflections.rb, line 205 def self.inflections(locale = :en) if block_given? yield Inflections.instance(locale) else Inflections.instance(locale) end end
pluralize(word, locale = :en)
click to toggle source
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"
# File lib/volt/extra_core/inflector/methods.rb, line 21 def self.pluralize(word, locale = :en) apply_inflections(word, inflections(locale).plurals) end
singularize(word, locale = :en)
click to toggle source
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"
# File lib/volt/extra_core/inflector/methods.rb, line 38 def self.singularize(word, locale = :en) apply_inflections(word, inflections(locale).singulars) end
Private Class Methods
apply_inflections(word, rules)
click to toggle source
Applies inflection rules for singularize
and pluralize
.
apply_inflections('post', inflections.plurals) # => "posts" apply_inflections('posts', inflections.singulars) # => "post"
# File lib/volt/extra_core/inflector/methods.rb, line 48 def self.apply_inflections(word, rules) result = word.to_s.dup if word.empty? || inflections.uncountables.include?(result.downcase[/\b\w+\Z/]) result else rules.each do |(rule, replacement)| if result.match(rule) result = result.sub(rule, replacement) break end end result end end