Sha256: 5de3bd14d95317545a0322783be262c44d914c92f37a6ce4f189a0f53a780be4

Contents?: true

Size: 1.8 KB

Versions: 7

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

# Copyright (c) 2008 - 2018, 2020, 2022, 2024 Peter H. Boling of RailsBling.com
# Released under the MIT license

module SanitizeEmail
  # Provides tools that allow methods to be deprecated with new releases of the gem.
  # See http://www.seejohncode.com/2012/01/09/deprecating-methods-in-ruby/
  module Deprecation
    class << self
      attr_accessor :deprecate_in_silence
    end

    @deprecate_in_silence = false

    # Define a deprecated alias for a method
    # @param [Symbol] name - name of method to define
    # @param [Symbol] replacement - name of method to (alias)
    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

    # Deprecate a defined method
    # @param [Symbol] name - name of deprecated method
    # @param [Symbol] replacement - name of the desired replacement
    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

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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sanitize_email-2.0.10 lib/sanitize_email/deprecation.rb
sanitize_email-2.0.9 lib/sanitize_email/deprecation.rb
sanitize_email-2.0.8 lib/sanitize_email/deprecation.rb
sanitize_email-2.0.7 lib/sanitize_email/deprecation.rb
sanitize_email-2.0.6 lib/sanitize_email/deprecation.rb
sanitize_email-2.0.5 lib/sanitize_email/deprecation.rb
sanitize_email-2.0.4 lib/sanitize_email/deprecation.rb