Module: Inflector

Extended by:
Inflector
Included in:
Inflector
Defined in:
lib/ext/active_support_subset.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object

By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to “:lower” then camelize produces lowerCamelCase.

camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces

Examples

"active_record".camelize #=> "ActiveRecord"
"active_record".camelize(:lower) #=> "activeRecord"
"active_record/errors".camelize #=> "ActiveRecord::Errors"
"active_record/errors".camelize(:lower) #=> "activeRecord::Errors"


20
21
22
23
24
25
26
27
28
29
# File 'lib/ext/active_support_subset.rb', line 20

def camelize(lower_case_and_underscored_word, 
             first_letter_in_uppercase = true)
  if first_letter_in_uppercase
    lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { 
                        "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
  else
    lower_case_and_underscored_word.first + 
                          camelize(lower_case_and_underscored_word)[1..-1]
  end
end

#constantize(camel_cased_word) ⇒ Object

Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.

Examples

"Module".constantize #=> Module
"Class".constantize #=> Class


54
55
56
57
58
59
60
61
# File 'lib/ext/active_support_subset.rb', line 54

def constantize(camel_cased_word)
  unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
    raise NameError, 
          "#{camel_cased_word.inspect} is not a valid constant name!"
  end

  Object.module_eval("::#{$1}", __FILE__, __LINE__)
end

#underscore(camel_cased_word) ⇒ Object

The reverse of camelize. Makes an underscored form from the expression in the string.

Changes ‘::’ to ‘/’ to convert namespaces to paths.

Examples

"ActiveRecord".underscore #=> "active_record"
"ActiveRecord::Errors".underscore #=> active_record/errors


39
40
41
42
43
44
45
# File 'lib/ext/active_support_subset.rb', line 39

def underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
                        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
                        gsub(/([a-z\d])([A-Z])/,'\1_\2').
                        tr("-", "_").
                        downcase
end