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. The default inflections for pluralization, singularization, and uncountable words are kept in inflections.rb.
Methods
- camelize
- classify
- constantize
- dasherize
- demodulize
- foreign_key
- humanize
- inflections
- ordinalize
- pluralize
- singularize
- tableize
- titleize
- underscore
Classes and Modules
Class Inflector::InflectionsPublic Instance methods
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 112 112: def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) 113: if first_letter_in_uppercase 114: lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } 115: else 116: lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1] 117: end 118: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 148 148: def classify(table_name) 149: camelize(singularize(table_name)) 150: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 156 156: def constantize(camel_cased_word) 157: raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" unless 158: /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ camel_cased_word 159: 160: camel_cased_word = "::#{camel_cased_word}" unless $1 161: Object.module_eval(camel_cased_word, __FILE__, __LINE__) 162: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 132 132: def dasherize(underscored_word) 133: underscored_word.gsub(/_/, '-') 134: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 140 140: def demodulize(class_name_in_module) 141: class_name_in_module.to_s.gsub(/^.*::/, '') 142: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 152 152: def foreign_key(class_name, separate_class_name_and_id_with_underscore = true) 153: underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id") 154: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 136 136: def humanize(lower_case_and_underscored_word) 137: lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize 138: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 82 82: def inflections 83: if block_given? 84: yield Inflections.instance 85: else 86: Inflections.instance 87: end 88: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 164 164: def ordinalize(number) 165: if (11..13).include?(number.to_i % 100) 166: "#{number}th" 167: else 168: case number.to_i % 10 169: when 1: "#{number}st" 170: when 2: "#{number}nd" 171: when 3: "#{number}rd" 172: else "#{number}th" 173: end 174: end 175: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 90 90: def pluralize(word) 91: result = word.to_s.dup 92: 93: if inflections.uncountables.include?(result.downcase) 94: result 95: else 96: inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } 97: result 98: end 99: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 101 101: def singularize(word) 102: result = word.to_s.dup 103: 104: if inflections.uncountables.include?(result.downcase) 105: result 106: else 107: inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } 108: result 109: end 110: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 144 144: def tableize(class_name) 145: pluralize(underscore(class_name)) 146: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 120 120: def titleize(word) 121: humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize } 122: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 124 124: def underscore(camel_cased_word) 125: camel_cased_word.to_s.gsub(/::/, '/'). 126: gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). 127: gsub(/([a-z\d])([A-Z])/,'\1_\2'). 128: tr("-", "_"). 129: downcase 130: end