Module: StoryTeller::Plurals

Included in:
Builtins
Defined in:
lib/story_teller/plurals.rb

Overview

The Plurals module

Instance Method Summary collapse

Instance Method Details

#apply_inflections(word, rules) ⇒ Object

Applies inflection rules for singularize and pluralize.



49
50
51
52
53
54
55
56
57
# File 'lib/story_teller/plurals.rb', line 49

def apply_inflections(word, rules)
  return "" if word.empty?
  result = word.to_s.dup
  return result if StoryTeller::Inflector.inflections.uncountables.include?(result.downcase[/\b\w+\Z/])
  for (rule, replacement) in rules
    break if result.sub!(rule, replacement)
  end
  result
end

#plural?(s) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/story_teller/plurals.rb', line 44

def plural?(s)
  pluralize(s) == s
end

#pluralize(word, n = nil) ⇒ Object

Returns the plural form of the word in the string.



29
30
31
32
# File 'lib/story_teller/plurals.rb', line 29

def pluralize(word, n = nil)
  return word if n == 1
  apply_inflections(word, StoryTeller::Inflector.inflections.plurals)
end

#singular?(s) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/story_teller/plurals.rb', line 40

def singular?(s)
  singularize(s) == s
end

#singularize(word) ⇒ Object

The reverse of pluralize, returns the singular form of a word in a string.



36
37
38
# File 'lib/story_teller/plurals.rb', line 36

def singularize(word)
  apply_inflections(word, StoryTeller::Inflector.inflections.singulars)
end