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 28
28: def camelize(lower_case_and_underscored_word)
29: lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
30: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 48
48: def classify(table_name)
49: camelize(singularize(table_name))
50: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 57
57: def constantize(camel_cased_word)
58: camel_cased_word.split("::").inject(Object) do |final_type, part|
59: final_type = final_type.const_get(part)
60: end
61: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 40
40: def demodulize(class_name_in_module)
41: class_name_in_module.to_s.gsub(/^.*::/, '')
42: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 52
52: def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
53: Inflector.underscore(Inflector.demodulize(class_name)) +
54: (separate_class_name_and_id_with_underscore ? "_id" : "id")
55: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 36
36: def humanize(lower_case_and_underscored_word)
37: lower_case_and_underscored_word.to_s.gsub(/_/, " ").capitalize
38: 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:
9: if uncountable_words.include?(result.downcase)
10: result
11: else
12: plural_rules.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
13: result
14: end
15: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 17
17: def singularize(word)
18: result = word.to_s.dup
19:
20: if uncountable_words.include?(result.downcase)
21: result
22: else
23: singular_rules.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
24: result
25: end
26: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 44
44: def tableize(class_name)
45: pluralize(underscore(class_name))
46: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 32
32: def underscore(camel_cased_word)
33: camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase
34: end
Private Instance methods
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 68
68: def plural_rules #:doc:
69: [
70: [/^(ox)$/i, '\1\2en'], # ox
71: [/([m|l])ouse$/i, '\1ice'], # mouse, louse
72: [/(matr|vert)ix|ex$/i, '\1ices'], # matrix, vertex, index
73: [/(x|ch|ss|sh)$/i, '\1es'], # search, switch, fix, box, process, address
74: [/([^aeiouy]|qu)ies$/i, '\1y'],
75: [/([^aeiouy]|qu)y$/i, '\1ies'], # query, ability, agency
76: [/(hive)$/i, '\1s'], # archive, hive
77: [/(?:([^f])fe|([lr])f)$/i, '\1\2ves'], # half, safe, wife
78: [/sis$/i, 'ses'], # basis, diagnosis
79: [/([ti])um$/i, '\1a'], # datum, medium
80: [/(p)erson$/i, '\1eople'], # person, salesperson
81: [/(m)an$/i, '\1en'], # man, woman, spokesman
82: [/(c)hild$/i, '\1hildren'], # child
83: [/(buffal|tomat)o$/i, '\1\2oes'], # buffalo, tomato
84: [/(bu)s$/i, '\1\2ses'], # bus
85: [/(alias)/i, '\1es'], # alias
86: [/(octop|vir)us$/i, '\1i'], # octopus, virus - virus has no defined plural (according to Latin/dictionary.com), but viri is better than viruses/viruss
87: [/(ax|cri|test)is$/i, '\1es'], # axis, crisis
88: [/s$/i, 's'], # no change (compatibility)
89: [/$/, 's']
90: ]
91: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 93
93: def singular_rules #:doc:
94: [
95: [/(matr)ices$/i, '\1ix'],
96: [/(vert)ices$/i, '\1ex'],
97: [/^(ox)en/i, '\1'],
98: [/(alias)es$/i, '\1'],
99: [/([octop|vir])i$/i, '\1us'],
100: [/(cris|ax|test)es$/i, '\1is'],
101: [/(shoe)s$/i, '\1'],
102: [/(o)es$/i, '\1'],
103: [/(bus)es$/i, '\1'],
104: [/([m|l])ice$/i, '\1ouse'],
105: [/(x|ch|ss|sh)es$/i, '\1'],
106: [/(m)ovies$/i, '\1\2ovie'],
107: [/(s)eries$/i, '\1\2eries'],
108: [/([^aeiouy]|qu)ies$/i, '\1y'],
109: [/([lr])ves$/i, '\1f'],
110: [/(tive)s$/i, '\1'],
111: [/(hive)s$/i, '\1'],
112: [/([^f])ves$/i, '\1fe'],
113: [/(^analy)ses$/i, '\1sis'],
114: [/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis'],
115: [/([ti])a$/i, '\1um'],
116: [/(p)eople$/i, '\1\2erson'],
117: [/(m)en$/i, '\1an'],
118: [/(s)tatus$/i, '\1\2tatus'],
119: [/(c)hildren$/i, '\1\2hild'],
120: [/(n)ews$/i, '\1\2ews'],
121: [/s$/i, '']
122: ]
123: end