| Module | Inflector |
| In: |
vendor/activesupport/lib/active_support/inflector.rb
|
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.
# File vendor/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
# File vendor/activesupport/lib/active_support/inflector.rb, line 42
42: def classify(table_name)
43: camelize(singularize(table_name))
44: end
# File vendor/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
# File vendor/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
# File vendor/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
# File vendor/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
# File vendor/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
# File vendor/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
# File vendor/activesupport/lib/active_support/inflector.rb, line 38
38: def tableize(class_name)
39: pluralize(underscore(class_name))
40: end
# File vendor/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
# File vendor/activesupport/lib/active_support/inflector.rb, line 58
58: def plural_rules #:doc:
59: [
60: [/fish$/, 'fish'], # fish
61: [/(x|ch|ss|sh)$/, '\1es'], # search, switch, fix, box, process, address
62: [/series$/, '\1series'],
63: [/([^aeiouy]|qu)ies$/, '\1y'],
64: [/([^aeiouy]|qu)y$/, '\1ies'], # query, ability, agency
65: [/(?:([^f])fe|([lr])f)$/, '\1\2ves'], # half, safe, wife
66: [/sis$/, 'ses'], # basis, diagnosis
67: [/([ti])um$/, '\1a'], # datum, medium
68: [/person$/, 'people'], # person, salesperson
69: [/man$/, 'men'], # man, woman, spokesman
70: [/child$/, 'children'], # child
71: [/s$/, 's'], # no change (compatibility)
72: [/$/, 's']
73: ]
74: end
# File vendor/activesupport/lib/active_support/inflector.rb, line 76
76: def singular_rules #:doc:
77: [
78: [/fish$/, 'fish'],
79: [/(x|ch|ss|sh)es$/, '\1'],
80: [/movies$/, 'movie'],
81: [/series$/, 'series'],
82: [/([^aeiouy]|qu)ies$/, '\1y'],
83: [/([lr])ves$/, '\1f'],
84: [/([^f])ves$/, '\1fe'],
85: [/(analy|ba|diagno|parenthe|progno|synop|the)ses$/, '\1sis'],
86: [/([ti])a$/, '\1um'],
87: [/people$/, 'person'],
88: [/men$/, 'man'],
89: [/status$/, 'status'],
90: [/children$/, 'child'],
91: [/news$/, 'news'],
92: [/s$/, '']
93: ]
94: end