Module: N::Inflector

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

Overview

The Inflector transforms words from singular to plural, class names to table names, modulized class names to ones without, and class names to foreign keys.

Instance Method Summary collapse

Instance Method Details

#camelize(lower_case_and_underscored_word) ⇒ Object



29
30
31
# File 'lib/glue/inflector.rb', line 29

def camelize(lower_case_and_underscored_word)
  lower_case_and_underscored_word.gsub(/(^|_)(.)/){$2.upcase}
end

#demodulize(class_name_in_module) ⇒ Object



37
38
39
# File 'lib/glue/inflector.rb', line 37

def demodulize(class_name_in_module)
  class_name_in_module.gsub(/^.*::/, '')
end

#foreign_key(class_name, separate_class_name_and_id_with_underscore = true) ⇒ Object



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

def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
  Inflector.underscore(Inflector.demodulize(class_name)) + 
    (separate_class_name_and_id_with_underscore ? "_id" : "id")
end

#name(klass) ⇒ Object

Convert a class to a name.



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

def name(klass)
	Inflector.underscore(Inflector.demodulize(klass.to_s))
end

#plural_name(klass) ⇒ Object

Convert a class to a name in plural



54
55
56
# File 'lib/glue/inflector.rb', line 54

def plural_name(klass)
	Inflector.pluralize(Inflector.underscore(Inflector.demodulize(klass.to_s)))
end

#pluralize(word) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/glue/inflector.rb', line 13

def pluralize(word)
  result = word.dup
  plural_rules.each do |(rule, replacement)|
    break if result.gsub!(rule, replacement)
  end
  return result
end

#singularize(word) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/glue/inflector.rb', line 21

def singularize(word)
  result = word.dup
  singular_rules.each do |(rule, replacement)|
    break if result.gsub!(rule, replacement)
  end
  return result
end

#underscore(camel_cased_word) ⇒ Object



33
34
35
# File 'lib/glue/inflector.rb', line 33

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