Class: Trailer::Utility

Inherits:
Object
  • Object
show all
Defined in:
lib/trailer/utility.rb

Class Method Summary collapse

Class Method Details

.demodulize(path) ⇒ Object

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

Removes the module part from the expression in the string.

demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections"
demodulize('Inflections')                           # => "Inflections"
demodulize('::Inflections')                         # => "Inflections"
demodulize('')                                      # => ""

Parameters:

  • path (String)

    The path to demodulize.

See Also:



18
19
20
21
22
23
24
25
# 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) ⇒ Object

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

Parameters:

  • resource (Object)

    The resource instance to derive a name for.



53
54
55
56
57
# 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) ⇒ Object

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

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"

Parameters:

  • camel_cased_word (String)

    The word to underscore.

See Also:



39
40
41
42
43
44
45
46
47
48
# 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