Module: Plaza::Inflector

Extended by:
Inflector
Included in:
Inflector
Defined in:
lib/plaza/inflector.rb

Instance Method Summary collapse

Instance Method Details

#classify(table_name) ⇒ Object



5
6
7
# File 'lib/plaza/inflector.rb', line 5

def classify(table_name)
  singularize(table_name.split('_').map(&:capitalize).join)
end

#pluralize(str) ⇒ Object



9
10
11
12
13
14
# File 'lib/plaza/inflector.rb', line 9

def pluralize(str)
  str.strip!
  str.gsub!(/y$/,'ies')
  str << 's' unless str[-1] == 's'
  str
end

#singularize(str) ⇒ Object



16
17
18
19
20
# File 'lib/plaza/inflector.rb', line 16

def singularize(str)
  str.strip!
  str.gsub!(/ies$/,'y')
  str.chomp('s')
end

#tableize(str) ⇒ Object



22
23
24
# File 'lib/plaza/inflector.rb', line 22

def tableize(str)
  pluralize(underscore(str))
end

#underscore(str) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/plaza/inflector.rb', line 26

def underscore(str)
  return str unless str =~ /[A-Z-]|::/
  word = str.to_s.gsub(/::/, '/')
  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