Module: AttrDeprecated::ClassMethods

Defined in:
lib/attr_deprecated.rb

Instance Method Summary collapse

Instance Method Details

#_notify_deprecated_attribute_call(attribute) ⇒ Object



68
69
70
71
72
# 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) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object

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])



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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!Object



53
54
55
# File 'lib/attr_deprecated.rb', line 53

def clear_deprecated_attributes!
  self._deprecated_attributes = _deprecated_attributes.clear
end

#deprecated_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/attr_deprecated.rb', line 45

def deprecated_attribute?(attribute)
  _deprecated_attributes.include?(attribute)
end

#deprecated_attributesObject



49
50
51
# File 'lib/attr_deprecated.rb', line 49

def deprecated_attributes
  _deprecated_attributes || DeprecatedAttributeSet.new
end