Module: Inflector
- Extended by:
- Inflector
- Included in:
- Inflector
- Defined in:
- lib/action_controller/support/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
- #camelize(lower_case_and_underscored_word) ⇒ Object
- #classify(table_name) ⇒ Object
- #demodulize(class_name_in_module) ⇒ Object
- #foreign_key(class_name, separate_class_name_and_id_with_underscore = true) ⇒ Object
- #pluralize(word) ⇒ Object
- #singularize(word) ⇒ Object
- #tableize(class_name) ⇒ Object
- #underscore(camel_cased_word) ⇒ Object
Instance Method Details
#camelize(lower_case_and_underscored_word) ⇒ Object
22 23 24 |
# File 'lib/action_controller/support/inflector.rb', line 22 def camelize(lower_case_and_underscored_word) lower_case_and_underscored_word.gsub(/(^|_)(.)/){$2.upcase} end |
#classify(table_name) ⇒ Object
38 39 40 |
# File 'lib/action_controller/support/inflector.rb', line 38 def classify(table_name) camelize(singularize(table_name)) end |
#demodulize(class_name_in_module) ⇒ Object
30 31 32 |
# File 'lib/action_controller/support/inflector.rb', line 30 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
42 43 44 45 |
# File 'lib/action_controller/support/inflector.rb', line 42 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 |
#pluralize(word) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/action_controller/support/inflector.rb', line 6 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
14 15 16 17 18 19 20 |
# File 'lib/action_controller/support/inflector.rb', line 14 def singularize(word) result = word.dup singular_rules.each do |(rule, replacement)| break if result.gsub!(rule, replacement) end return result end |
#tableize(class_name) ⇒ Object
34 35 36 |
# File 'lib/action_controller/support/inflector.rb', line 34 def tableize(class_name) pluralize(underscore(class_name)) end |
#underscore(camel_cased_word) ⇒ Object
26 27 28 |
# File 'lib/action_controller/support/inflector.rb', line 26 def underscore(camel_cased_word) camel_cased_word.gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcase end |