Class: Inform::Inflector::Inflections

Inherits:
Object
  • Object
show all
Defined in:
lib/runtime/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.



47
48
49
# File 'lib/runtime/inflector.rb', line 47

def initialize
  clear_fields
end

Instance Attribute Details

#pluralsObject (readonly)

Returns the value of attribute plurals.



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

def plurals
  @plurals
end

#propersObject (readonly)

Returns the value of attribute propers.



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

def propers
  @propers
end

#singularsObject (readonly)

Returns the value of attribute singulars.



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

def singulars
  @singulars
end

#uncountablesObject (readonly)

Returns the value of attribute uncountables.



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

def uncountables
  @uncountables
end

Class Method Details

.instanceObject



41
42
43
# File 'lib/runtime/inflector.rb', line 41

def self.instance
  @instance ||= new
end

Instance Method Details

#clear(scope = :all) ⇒ Object



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

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



51
52
53
# File 'lib/runtime/inflector.rb', line 51

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

#defective(noun) ⇒ Object



67
68
69
# File 'lib/runtime/inflector.rb', line 67

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

#irregular(singular, plural) ⇒ Object

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



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

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



55
56
57
58
59
# File 'lib/runtime/inflector.rb', line 55

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

#proper(rule, replacement) ⇒ Object



108
109
110
# File 'lib/runtime/inflector.rb', line 108

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

#singular(rule, replacement) ⇒ Object



61
62
63
64
65
# File 'lib/runtime/inflector.rb', line 61

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



104
105
106
# File 'lib/runtime/inflector.rb', line 104

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