Class: Drillbit::Utilities::String

Inherits:
Object
  • Object
show all
Defined in:
lib/drillbit/utilities/string.rb

Constant Summary collapse

SINGULARIZATION_RULES =

rubocop:disable Metrics/LineLength

[
  [/(database)s$/i, '\1'],
  [/(quiz)zes$/i, '\1'],
  [/(matr)ices$/i, '\1ix'],
  [/(vert|ind)ices$/i, '\1ex'],
  [/^(ox)en/i, '\1'],
  [/(alias|status)(es)?$/i, '\1'],
  [/(octop|vir)(us|i)$/i, '\1us'],
  [/^(a)x[ie]s$/i, '\1xis'],
  [/(cris|test)(is|es)$/i, '\1is'],
  [/(shoe)s$/i, '\1'],
  [/(o)es$/i, '\1'],
  [/(bus)(es)?$/i, '\1'],
  [/^(m|l)ice$/i, '\1ouse'],
  [/(x|ch|ss|sh)es$/i, '\1'],
  [/(m)ovies$/i, '\1ovie'],
  [/(child)ren$/i, '\1'],
  [/(s)eries$/i, '\1eries'],
  [/([^aeiouy]|qu)ies$/i, '\1y'],
  [/([lr])ves$/i, '\1f'],
  [/(tive)s$/i, '\1'],
  [/(hive)s$/i, '\1'],
  [/([^f])ves$/i, '\1fe'],
  [/(^analy)(sis|ses)$/i, '\1sis'],
  [/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i, '\1sis'],
  [/([ti])a$/i, '\1um'],
  [/(n)ews$/i, '\1ews'],
  [/(ss)$/i, '\1'],
  [/s$/i, ''],
].freeze

Class Method Summary collapse

Class Method Details

.singularize(word) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/drillbit/utilities/string.rb', line 51

def self.singularize(word)
  result = word.to_s.dup

  SINGULARIZATION_RULES.each do |(rule, replacement)|
    break if result.sub!(rule, replacement)
  end

  result
end

.underscore(other) ⇒ Object

rubocop:enable Metrics/LineLength



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/drillbit/utilities/string.rb', line 39

def self.underscore(other)
  word = other.to_s.gsub('::', '/')
  word.gsub!(/(?:([A-Za-z\d])|^)(?=\b|[^a-z])/) do
    "#{Regexp.last_match(1)}#{Regexp.last_match(1) && ''}"
  end
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end