Module: Tins::Deprecate

Included in:
Module
Defined in:
lib/tins/deprecate.rb

Overview

A module for deprecating methods with customizable messages and warnings.

Examples:

class MyClass
  extend Tins::Deprecate
  deprecate method: :old_method, new_method: :new_method
end

Instance Method Summary collapse

Instance Method Details

#deprecate(method:, new_method: nil, message: nil) ⇒ Object

Deprecates a method and issues a warning when called.

Parameters:

  • method (Symbol)

    the name of the method to deprecate

  • new_method (Symbol) (defaults to: nil)

    the name of the replacement method

  • message (String) (defaults to: nil)

    the warning message to display



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tins/deprecate.rb', line 15

def deprecate(method:, new_method: nil, message: nil)
  message ||= '[DEPRECATION] `%{method}` is deprecated. Please use `%{new_method}` instead.'
  message = message % { method: method, new_method: new_method }
  m = Module.new do
    define_method(method) do |*a, **kw, &b|
      warn message
      super(*a, **kw, &b)
    end
  end
  prepend m
end