The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, and class names to foreign keys.
Methods
- camelize
- classify
- constantize
- demodulize
- foreign_key
- humanize
- plural_rules
- pluralize
- singular_rules
- singularize
- tableize
- underscore
Public Instance methods
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 22
22: def camelize(lower_case_and_underscored_word)
23: lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
24: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 42
42: def classify(table_name)
43: camelize(singularize(table_name))
44: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 51
51: def constantize(camel_cased_word)
52: camel_cased_word.split("::").inject(Object) do |final_type, part|
53: final_type = final_type.const_get(part)
54: end
55: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 34
34: def demodulize(class_name_in_module)
35: class_name_in_module.to_s.gsub(/^.*::/, '')
36: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 46
46: def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
47: Inflector.underscore(Inflector.demodulize(class_name)) +
48: (separate_class_name_and_id_with_underscore ? "_id" : "id")
49: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 30
30: def humanize(lower_case_and_underscored_word)
31: lower_case_and_underscored_word.to_s.gsub(/_/, " ").capitalize
32: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 6
6: def pluralize(word)
7: result = word.to_s.dup
8: plural_rules.each do |(rule, replacement)|
9: break if result.gsub!(rule, replacement)
10: end
11: return result
12: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 14
14: def singularize(word)
15: result = word.to_s.dup
16: singular_rules.each do |(rule, replacement)|
17: break if result.gsub!(rule, replacement)
18: end
19: return result
20: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 38
38: def tableize(class_name)
39: pluralize(underscore(class_name))
40: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 26
26: def underscore(camel_cased_word)
27: camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcase
28: end
Private Instance methods
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 58
58: def plural_rules #:doc:
59: [
60: [/(fish)$/i, '\1\2'], # fish
61: [/(x|ch|ss|sh)$/i, '\1es'], # search, switch, fix, box, process, address
62: [/(series)$/i, '\1\2'],
63: [/([^aeiouy]|qu)ies$/i, '\1y'],
64: [/([^aeiouy]|qu)y$/i, '\1ies'], # query, ability, agency
65: [/(?:([^f])fe|([lr])f)$/i, '\1\2ves'], # half, safe, wife
66: [/sis$/i, 'ses'], # basis, diagnosis
67: [/([ti])um$/i, '\1a'], # datum, medium
68: [/(p)erson$/i, '\1\2eople'], # person, salesperson
69: [/(m)an$/i, '\1\2en'], # man, woman, spokesman
70: [/(c)hild$/i, '\1\2hildren'], # child
71: [/s$/i, 's'], # no change (compatibility)
72: [/$/, 's']
73: ]
74: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 76
76: def singular_rules #:doc:
77: [
78: [/(f)ish$/i, '\1\2ish'],
79: [/(x|ch|ss|sh)es$/i, '\1'],
80: [/(m)ovies$/i, '\1\2ovie'],
81: [/(s)eries$/i, '\1\2eries'],
82: [/([^aeiouy]|qu)ies$/i, '\1y'],
83: [/([lr])ves$/i, '\1f'],
84: [/(tive)s$/i, '\1'],
85: [/([^f])ves$/i, '\1fe'],
86: [/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis'],
87: [/([ti])a$/i, '\1um'],
88: [/(p)eople$/i, '\1\2erson'],
89: [/(m)en$/i, '\1\2an'],
90: [/(s)tatus$/i, '\1\2tatus'],
91: [/(c)hildren$/i, '\1\2hild'],
92: [/(n)ews$/i, '\1\2ews'],
93: [/s$/i, '']
94: ]
95: end