module AttrDeprecated::ClassMethods

Public Instance Methods

_notify_deprecated_attribute_call(attribute) click to toggle source
# File lib/attr_deprecated.rb, line 68
def _notify_deprecated_attribute_call(attribute)
  @_deprecation_logger ||= AttrDeprecated::DeprecatedAttributeLogger.new(self)

  @_deprecation_logger.log_deprecated_attribute_usage(self, attribute)
end
_set_attribute_as_deprecated(attribute) click to toggle source
# File lib/attr_deprecated.rb, line 57
def _set_attribute_as_deprecated(attribute)
  original_method = instance_method(attribute.to_sym)

  klass = self
  define_method attribute.to_sym do |*args|
    klass._notify_deprecated_attribute_call(attribute)

    original_method.bind(self).call(*args)
  end
end
attr_deprecated(*attributes) click to toggle source

attr_deprecated

class macro definition to non-destructively mark an attribute as deprecated.

The original method (i.e. the one marked as deprecated) is renamed and wrapped in an alias that dispatches the notification. (See the ‘around_alias` pattern. [Paolo Perotta. Metaprogramming Ruby, p. 121])

# File lib/attr_deprecated.rb, line 28
def attr_deprecated(*attributes)
  attributes                  = DeprecatedAttributeSet.new(attributes.compact)
  self._deprecated_attributes ||= DeprecatedAttributeSet.new

  # Rails uses lazy initialization to wrap methods, so make sure we pre-initialize any deprecated attributes
  if defined?(ActiveRecord) && ancestors.include?(ActiveRecord::Base)
    new(Hash[attributes.zip(attributes.map {})], without_protection: true)
  end

  # Taking the difference of the two sets ensures we don't deprecate the same attribute more than once
  (attributes - _deprecated_attributes).each do |attribute|
    _set_attribute_as_deprecated attribute
  end

  self._deprecated_attributes += attributes
end
clear_deprecated_attributes!() click to toggle source
# File lib/attr_deprecated.rb, line 53
def clear_deprecated_attributes!
  self._deprecated_attributes = _deprecated_attributes.clear
end
deprecated_attribute?(attribute) click to toggle source
# File lib/attr_deprecated.rb, line 45
def deprecated_attribute?(attribute)
  _deprecated_attributes.include?(attribute)
end
deprecated_attributes() click to toggle source
# File lib/attr_deprecated.rb, line 49
def deprecated_attributes
  _deprecated_attributes || DeprecatedAttributeSet.new
end