class Sprockets::Deprecation

Constants

DEFAULT_BEHAVIORS
SPROCKETS_GEM_ROOT
THREAD_LOCAL__SILENCE_KEY

Attributes

callstack[R]

Public Class Methods

new(callstack = nil) click to toggle source
# File lib/sprockets/deprecation.rb, line 25
def initialize(callstack = nil)
  @callstack = callstack || caller(2)
end
silence(&block) click to toggle source
# File lib/sprockets/deprecation.rb, line 18
def self.silence(&block)
  Thread.current[THREAD_LOCAL__SILENCE_KEY] = true
  block.call
ensure
  Thread.current[THREAD_LOCAL__SILENCE_KEY] = false
end

Public Instance Methods

warn(message) click to toggle source
# File lib/sprockets/deprecation.rb, line 29
def warn(message)
  return if Thread.current[THREAD_LOCAL__SILENCE_KEY]
  deprecation_message(message).tap do |m|
    behavior.each { |b| b.call(m, callstack) }
  end
end

Private Instance Methods

_extract_callstack() click to toggle source
# File lib/sprockets/deprecation.rb, line 77
def _extract_callstack
  offending_line = callstack.find { |line| !ignored_callstack(line) } || callstack.first

  if offending_line
    if md = offending_line.match(/^(.+?):(\d+)(?::in `(.*?)')?/)
      md.captures
    else
      offending_line
    end
  end
end
behavior() click to toggle source
# File lib/sprockets/deprecation.rb, line 37
def behavior
  @behavior ||= [DEFAULT_BEHAVIORS[:stderr]]
end
behavior=(behavior) click to toggle source
# File lib/sprockets/deprecation.rb, line 41
def behavior=(behavior)
  @behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
end
deprecation_caller_message() click to toggle source
# File lib/sprockets/deprecation.rb, line 50
def deprecation_caller_message
  file, line, method = extract_callstack
  if file
    if line && method
      "(called from #{method} at #{file}:#{line})"
    else
      "(called from #{file}:#{line})"
    end
  end
end
deprecation_message(message = nil) click to toggle source
# File lib/sprockets/deprecation.rb, line 45
def deprecation_message(message = nil)
  message ||= "You are using deprecated behavior which will be removed from the next major or minor release."
  "DEPRECATION WARNING: #{message} #{ deprecation_caller_message }"
end
extract_callstack() click to toggle source
# File lib/sprockets/deprecation.rb, line 67
def extract_callstack
  return _extract_callstack if callstack.first.is_a? String

  offending_line = callstack.find { |frame|
    frame.absolute_path && !ignored_callstack(frame.absolute_path)
  } || callstack.first

  [offending_line.path, offending_line.lineno, offending_line.label]
end
ignored_callstack(path) click to toggle source
# File lib/sprockets/deprecation.rb, line 63
def ignored_callstack(path)
  path.start_with?(SPROCKETS_GEM_ROOT) || path.start_with?(RbConfig::CONFIG['rubylibdir'])
end