Module: Dry::Core::Deprecations::Interface

Defined in:
lib/dry/core/deprecations.rb

Instance Method Summary collapse

Instance Method Details

#deprecate(old_name, new_name = nil, message: nil) ⇒ Object

Mark instance method as deprecated

Parameters:

  • old_name (Symbol)

    deprecated method

  • new_name (Symbol) (defaults to: nil)

    replacement (not required)

  • [String] (Hash)

    a customizable set of options



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/dry/core/deprecations.rb', line 146

def deprecate(old_name, new_name = nil, message: nil)
  full_msg = Deprecations.deprecated_name_message(
    "#{self.name}##{old_name}",
    new_name ? "#{self.name}##{new_name}" : nil,
    message
  )
  mod = self

  if new_name
    undef_method old_name if method_defined?(old_name)

    define_method(old_name) do |*args, &block|
      mod.warn("#{ full_msg }\n#{ STACK.() }")
      __send__(new_name, *args, &block)
    end
  else
    aliased_name = :"#{old_name}_without_deprecation"
    alias_method aliased_name, old_name
    private aliased_name
    undef_method old_name

    define_method(old_name) do |*args, &block|
      mod.warn("#{ full_msg }\n#{ STACK.() }")
      __send__(aliased_name, *args, &block)
    end
  end
end

#deprecate_class_method(old_name, new_name = nil, message: nil) ⇒ Object

Mark class-level method as deprecated

Parameters:

  • old_name (Symbol)

    deprecated method

  • new_name (Symbol) (defaults to: nil)

    replacement (not required)

  • [String] (Hash)

    a customizable set of options



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/dry/core/deprecations.rb', line 179

def deprecate_class_method(old_name, new_name = nil, message: nil)
  full_msg = Deprecations.deprecated_name_message(
    "#{self.name}.#{old_name}",
    new_name ? "#{self.name}.#{new_name}" : nil,
    message
  )

  meth = new_name ? method(new_name) : method(old_name)

  singleton_class.instance_exec do
    undef_method old_name if method_defined?(old_name)

    define_method(old_name) do |*args, &block|
      warn("#{ full_msg }\n#{ STACK.() }")
      meth.call(*args, &block)
    end
  end
end

#deprecate_constant(constant_name, message: nil) ⇒ Object

Mark a constant as deprecated

Parameters:

  • constant_name (Symbol)

    constant name to be deprecated

  • [String] (Hash)

    a customizable set of options



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/dry/core/deprecations.rb', line 201

def deprecate_constant(constant_name, message: nil)
  value = const_get(constant_name)
  remove_const(constant_name)

  full_msg = Deprecations.deprecated_name_message(
    "#{self.name}::#{constant_name}",
    message
  )

  mod = Module.new do
    define_method(:const_missing) do |missing|
      if missing == constant_name
        warn("#{ full_msg }\n#{ STACK.() }")
        value
      else
        super(missing)
      end
    end
  end

  extend(mod)
end

#deprecation_tag(tag = nil) ⇒ Object

Sets/gets deprecation tag

Parameters:

  • [String,Symbol] (Hash)

    a customizable set of options



126
127
128
129
130
131
132
# File 'lib/dry/core/deprecations.rb', line 126

def deprecation_tag(tag = nil)
  if defined?(@deprecation_tag)
    @deprecation_tag
  else
    @deprecation_tag = tag
  end
end

#warn(msg) ⇒ Object

Issue a tagged warning message

Parameters:

  • msg (String)

    warning message



137
138
139
# File 'lib/dry/core/deprecations.rb', line 137

def warn(msg)
  Deprecations.warn(msg, tag: deprecation_tag)
end