Module: Roda::Project::Helpers::Inflections

Included in:
Bin::Generators::Migration::ActionDetector, Bin::Generators::Migration::FieldParser, Bin::Generators::Model
Defined in:
lib/roda/project/helpers/inflections.rb

Class Method Summary collapse

Class Method Details

.camelize(str) ⇒ Object



9
10
11
12
13
# File 'lib/roda/project/helpers/inflections.rb', line 9

def camelize(str)
  return str if /[A-Z]/.match?(str)

  str.split("_").map(&:capitalize).join
end

.plural?(str) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/roda/project/helpers/inflections.rb', line 50

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

.pluralize(str) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/roda/project/helpers/inflections.rb', line 22

def pluralize(str)
  return str if str.end_with?("s")

  if str.end_with?("y") && !str.end_with?("ay", "ey", "oy", "uy")
    "#{str[0..-2]}ies"
  elsif str.end_with?("s", "x", "z", "ch", "sh")
    "#{str}es"
  else
    "#{str}s"
  end
end

.singular?(str) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/roda/project/helpers/inflections.rb', line 46

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

.singularize(str) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/roda/project/helpers/inflections.rb', line 34

def singularize(str)
  if str.end_with?("ies")
    "#{str[0..-4]}y"
  elsif str.end_with?("es") && !str.end_with?("ques")
    str[0..-3]
  elsif str.end_with?("s") && !str.end_with?("ss")
    str[0..-2]
  else
    str
  end
end

.underscore(str) ⇒ Object



15
16
17
18
19
20
# File 'lib/roda/project/helpers/inflections.rb', line 15

def underscore(str)
  str.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr("-", "_")
    .downcase
end