Module: Harvest::Model::Utility

Defined in:
lib/harvest/model.rb

Class Method Summary collapse

Class Method Details

.demodulize(class_name_in_module) ⇒ Object

Removes the module part from the expression in the string.

Examples:

"ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
"Inflections".demodulize


105
106
107
# File 'lib/harvest/model.rb', line 105

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

.underscore(camel_cased_word) ⇒ Object

Makes an underscored, lowercase 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

As a rule of thumb you can think of underscore as the inverse of camelize, though there are cases where that does not hold:

"SSLError".underscore.camelize # => "SslError"


121
122
123
124
125
126
127
128
129
# File 'lib/harvest/model.rb', line 121

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