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.



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

def initialize
  clear_fields
end

Instance Attribute Details

#pluralsObject (readonly)

Returns the value of attribute plurals.



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

def plurals
  @plurals
end

#propersObject (readonly)

Returns the value of attribute propers.



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

def propers
  @propers
end

#singularsObject (readonly)

Returns the value of attribute singulars.



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

def singulars
  @singulars
end

#uncountablesObject (readonly)

Returns the value of attribute uncountables.



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

def uncountables
  @uncountables
end

Class Method Details

.instanceObject



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

def self.instance
  @instance ||= new
end

Instance Method Details

#clear(scope = :all) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/story_teller/inflector.rb', line 113

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



52
53
54
# File 'lib/story_teller/inflector.rb', line 52

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

#defective(noun) ⇒ Object



68
69
70
# File 'lib/story_teller/inflector.rb', line 68

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

#irregular(singular, plural) ⇒ Object

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



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
99
100
101
# File 'lib/story_teller/inflector.rb', line 74

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



56
57
58
59
60
# File 'lib/story_teller/inflector.rb', line 56

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

#proper(rule, replacement) ⇒ Object



109
110
111
# File 'lib/story_teller/inflector.rb', line 109

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

#singular(rule, replacement) ⇒ Object



62
63
64
65
66
# File 'lib/story_teller/inflector.rb', line 62

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



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

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