Class: CMDx::Deprecation

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdx/deprecation.rb

Overview

Declared via Task.deprecation. Runs before a task's lifecycle to warn, log, raise, or delegate when a task class has been marked deprecated. Supports conditional :if / :unless gating via Util#satisfied?.

Instance Method Summary collapse

Constructor Details

#initialize(value, options = EMPTY_HASH) ⇒ Deprecation

Returns a new instance of Deprecation.

Parameters:

  • value (:log, :warn, :error, Symbol, Proc, #call, nil)

    action to take; nil disables

  • options (Hash{Symbol => Object}) (defaults to: EMPTY_HASH)

Options Hash (options):

  • :if (Symbol, Proc, #call)
  • :unless (Symbol, Proc, #call)


14
15
16
17
# File 'lib/cmdx/deprecation.rb', line 14

def initialize(value, options = EMPTY_HASH)
  @value   = value
  @options = options.freeze
end

Instance Method Details

#execute(task) { ... } ⇒ void

This method returns an undefined value.

Runs the configured deprecation action, yielding first so Runtime can mark the result as deprecated for telemetry.

Parameters:

Yields:

  • invoked immediately before the action runs, only when conditions pass

Raises:

  • (DeprecationError)

    when value is :error

  • (ArgumentError)

    when value is an unsupported type



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cmdx/deprecation.rb', line 27

def execute(task)
  return if @value.nil?
  return unless Util.satisfied?(@options[:if], @options[:unless], task)

  yield

  case @value
  when Symbol
    registry = deprecators_registry(task)
    if registry.key?(@value)
      registry.lookup(@value).call(task)
    else
      task.send(@value)
    end
  when Proc
    task.instance_exec(task, &@value)
  else
    return @value.call(task) if @value.respond_to?(:call)

    raise ArgumentError, <<~MSG.chomp
      deprecation must be a Symbol, Proc, or respond to #call (got #{@value.class}).
      See https://drexed.github.io/cmdx/deprecation/#declarations
    MSG
  end
end