class Trailer::Utility

Public Class Methods

demodulize(path) click to toggle source

Copied from ActiveSupport::Inflector to avoid introducing an extra dependency.

@param path [String] The path to demodulize.

@see apidock.com/rails/ActiveSupport/Inflector/demodulize

Removes the module part from the expression in the string.

demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections"
demodulize('Inflections')                           # => "Inflections"
demodulize('::Inflections')                         # => "Inflections"
demodulize('')                                      # => ""
# File lib/trailer/utility.rb, line 18
def demodulize(path)
  path = path.to_s
  if (i = path.rindex('::'))
    path[(i + 2)..]
  else
    path
  end
end
resource_name(resource) click to toggle source

Creates a name for the given resource instance, suitable for recording in the trace.

@param resource [Object] The resource instance to derive a name for.

# File lib/trailer/utility.rb, line 53
def resource_name(resource)
  return if resource.nil?

  underscore(demodulize(resource.class.name))
end
underscore(camel_cased_word) click to toggle source

Copied from ActiveSupport::Inflector to avoid introducing an extra dependency.

@param camel_cased_word [String] The word to underscore.

@see apidock.com/rails/v5.2.3/ActiveSupport/Inflector/underscore

Makes an underscored, lowercase form from the expression in the string.

Changes '::' to '/' to convert namespaces to paths.

underscore('ActiveModel')         # => "active_model"
underscore('ActiveModel::Errors') # => "active_model/errors"
# File lib/trailer/utility.rb, line 39
def underscore(camel_cased_word)
  return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)

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