Monkeybars::Inflector

Public Instance Methods

camelize(first_letter_in_uppercase = true) click to toggle source

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"
# File lib/monkeybars/inflector.rb, line 46
def camelize(first_letter_in_uppercase = true)
  if first_letter_in_uppercase
    self.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
  else
    self.to_s[0..0] + camelize(self.to_s)[1..-1]
  end
end
constantize() click to toggle source

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
# File lib/monkeybars/inflector.rb, line 28
def constantize()
  unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self.to_s
    raise NameError, "#{self.inspect} is not a valid constant name!"
  end

  Object.module_eval("::#{$1}", __FILE__, __LINE__)
end
underscore() click to toggle source

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
# File lib/monkeybars/inflector.rb, line 13
def underscore()
  self.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.