Module Yapra::Inflector
In: lib/yapra/inflector.rb

Methods

Public Instance methods

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"

[Source]

    # File lib/yapra/inflector.rb, line 18
18:   def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
19:     if first_letter_in_uppercase
20:       lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
21:     else
22:       lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
23:     end
24:   end

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

[Source]

    # File lib/yapra/inflector.rb, line 48
48:   def constantize(camel_cased_word)
49:     unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
50:       raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
51:     end
52: 
53:     Object.module_eval("::#{$1}", __FILE__, __LINE__)
54:   end

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

[Source]

    # File lib/yapra/inflector.rb, line 33
33:   def underscore(camel_cased_word)
34:     camel_cased_word.to_s.gsub(/::/, '/').
35:       gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
36:       gsub(/([a-z\d])([A-Z])/,'\1_\2').
37:       tr("-", "_").
38:       downcase
39:   end

[Validate]