Class: StoryTeller::Inflector::Inflections

Inherits:
Object
  • Object
show all
Defined in:
lib/story_teller/inflector.rb

Overview

The Inflections class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInflections

Returns a new instance of Inflections.



45
46
47
# File 'lib/story_teller/inflector.rb', line 45

def initialize
  clear_fields
end

Instance Attribute Details

#pluralsObject (readonly)

Returns the value of attribute plurals.



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

def plurals
  @plurals
end

#propersObject (readonly)

Returns the value of attribute propers.



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

def propers
  @propers
end

#singularsObject (readonly)

Returns the value of attribute singulars.



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

def singulars
  @singulars
end

#uncountablesObject (readonly)

Returns the value of attribute uncountables.



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

def uncountables
  @uncountables
end

Class Method Details

.instanceObject



39
40
41
# File 'lib/story_teller/inflector.rb', line 39

def self.instance
  @instance ||= new
end

Instance Method Details

#clear(scope = :all) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/story_teller/inflector.rb', line 110

def clear(scope = :all)
  case scope
  when :all
    clear_fields
  else
    clear_fields([scope])
  end
end

#clear_fields(fields = %i[plurals singulars uncountables propers])) ⇒ Object



49
50
51
# File 'lib/story_teller/inflector.rb', line 49

def clear_fields(fields = %i[plurals singulars uncountables propers])
  fields.each { |field| instance_variable_set(:"@#{field}", []) }
end

#defective(noun) ⇒ Object



65
66
67
# File 'lib/story_teller/inflector.rb', line 65

def defective(noun)
  @plurals.unshift([/^#{noun}$/i, noun])
end

#irregular(singular, plural) ⇒ Object

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/story_teller/inflector.rb', line 71

def irregular(singular, plural)
  @uncountables.delete(singular)
  @uncountables.delete(plural)

  s0 = singular[0]
  srest = singular[1..]

  p0 = plural[0]
  prest = plural[1..]

  if s0.upcase == p0.upcase
    plural(/(#{s0})#{srest}$/i, '\1' + prest)
    plural(/(#{p0})#{prest}$/i, '\1' + prest)

    singular(/(#{s0})#{srest}$/i, '\1' + srest)
    singular(/(#{p0})#{prest}$/i, '\1' + srest)
  else
    plural(/#{s0.upcase}(?i)#{srest}$/,   p0.upcase   + prest)
    plural(/#{s0.downcase}(?i)#{srest}$/, p0.downcase + prest)
    plural(/#{p0.upcase}(?i)#{prest}$/,   p0.upcase   + prest)
    plural(/#{p0.downcase}(?i)#{prest}$/, p0.downcase + prest)

    singular(/#{s0.upcase}(?i)#{srest}$/,   s0.upcase   + srest)
    singular(/#{s0.downcase}(?i)#{srest}$/, s0.downcase + srest)
    singular(/#{p0.upcase}(?i)#{prest}$/,   s0.upcase   + srest)
    singular(/#{p0.downcase}(?i)#{prest}$/, s0.downcase + srest)
  end
end

#plural(rule, replacement) ⇒ Object



53
54
55
56
57
# File 'lib/story_teller/inflector.rb', line 53

def plural(rule, replacement)
  @uncountables.delete(rule) if rule.is_a?(String)
  @uncountables.delete(replacement)
  @plurals.prepend([rule, replacement])
end

#proper(rule, replacement) ⇒ Object



106
107
108
# File 'lib/story_teller/inflector.rb', line 106

def proper(rule, replacement)
  @propers.unshift([rule, replacement])
end

#singular(rule, replacement) ⇒ Object



59
60
61
62
63
# File 'lib/story_teller/inflector.rb', line 59

def singular(rule, replacement)
  @uncountables.delete(rule) if rule.is_a?(String)
  @uncountables.delete(replacement)
  @singulars.prepend([rule, replacement])
end

#uncountable(*words) ⇒ Object

rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength



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

def uncountable(*words)
  (@uncountables << words).flatten!
end