Module: ActiveSupport::Inflector

Extended by:
Inflector
Included in:
Inflector
Defined in:
lib/support/string.rb

Instance Method Summary collapse

Instance Method Details

#camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/support/string.rb', line 31

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(/(?:^|_)(.)/) { $1.upcase }
  else
    lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
  end
end

#constantize(camel_cased_word) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/support/string.rb', line 53

def constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#demodulize(class_name_in_module) ⇒ Object



49
50
51
# File 'lib/support/string.rb', line 49

def demodulize(class_name_in_module)
  class_name_in_module.to_s.gsub(/^.*::/, '')
end

#underscore(camel_cased_word) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/support/string.rb', line 39

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