Module: SanitizeEmail::Deprecation

Included in:
SanitizeEmail, Bleach, Config
Defined in:
lib/sanitize_email/deprecation.rb

Overview

Provides tools that allow methods to be deprecated with new releases of the gem. See www.seejohncode.com/2012/01/09/deprecating-methods-in-ruby/

Constant Summary collapse

DEPRECATE_IN_SILENCE_MUTEX =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deprecate_in_silenceObject



13
14
15
# File 'lib/sanitize_email/deprecation.rb', line 13

def deprecate_in_silence
  DEPRECATE_IN_SILENCE_MUTEX.synchronize { @deprecate_in_silence }
end

.deprecate_in_silence=(value) ⇒ Object



17
18
19
# File 'lib/sanitize_email/deprecation.rb', line 17

def deprecate_in_silence=(value)
  DEPRECATE_IN_SILENCE_MUTEX.synchronize { @deprecate_in_silence = value }
end

Instance Method Details

#deprecated(name, replacement = nil) ⇒ Object

Deprecate a defined method

Parameters:

  • name (Symbol)
    • name of deprecated method

  • replacement (Symbol) (defaults to: nil)
    • name of the desired replacement



38
39
40
41
42
43
44
45
46
47
# File 'lib/sanitize_email/deprecation.rb', line 38

def deprecated(name, replacement = nil)
  # Replace old method
  old_name = :"#{name}_without_deprecation"
  alias_method(old_name, name)
  # And replace it with a wrapped version
  define_method(name) do |*args, &block|
    deprecation(name, " (please use ##{replacement})")
    send(old_name, *args, &block)
  end
end

#deprecated_alias(name, replacement) ⇒ Object

Define a deprecated alias for a method

Parameters:

  • name (Symbol)
    • name of method to define

  • replacement (Symbol)
    • name of method to (alias)



27
28
29
30
31
32
33
# File 'lib/sanitize_email/deprecation.rb', line 27

def deprecated_alias(name, replacement)
  # Create a wrapped version
  define_method(name) do |*args, &block|
    warn("SanitizeEmail: ##{name} deprecated (please use ##{replacement})") unless SanitizeEmail::Deprecation.deprecate_in_silence
    send(replacement, *args, &block)
  end
end

#deprecation(name, replacement = nil) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/sanitize_email/deprecation.rb', line 49

def deprecation(name, replacement = nil)
  if replacement
    deprecation_warning_message("SanitizeEmail: ##{name} deprecated#{replacement}")
  else
    deprecation_warning_message("SanitizeEmail: ##{name} deprecated")
  end
end

#deprecation_warning_message(message) ⇒ Object



57
58
59
# File 'lib/sanitize_email/deprecation.rb', line 57

def deprecation_warning_message(message)
  warn(message) unless SanitizeEmail::Deprecation.deprecate_in_silence
end