Method: Tron.failure

Defined in:
lib/tron.rb

.failure(code, attributes = {}) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tron.rb', line 53

def self.failure(code, attributes = {}) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  code.respond_to?(:to_sym) ||
    raise(ArgumentError, 'Tron.failure must be called with a Symbol as first argument')

  attributes.respond_to?(:keys) ||
    raise(ArgumentError, 'The second argument (metadata) for Tron.failure must respond to #keys')

  attributes.respond_to?(:values) ||
    raise(ArgumentError, 'The second argument (metadata) for Tron.failure must respond to #values')

  Struct.new(:failure, *attributes.keys) do
    undef_method :[]=
    members.each { |member| undef_method :"#{member}=" }

    def success?
      false
    end

    def failure?
      true
    end

    def success
      nil
    end

    def code
      warn 'DEPRECATION WARNING: Calling `#code` on a Tron object is deprecated and will be removed in Tron 2.0.0. ' \
           "Please use `#failure` instead. Called from `#{caller.first}`"

      failure
    end

    def on_success(_ = nil)
      self
    end

    def on_failure(proc = nil, &block)
      (proc || block).call self
    end
  end.new code.to_sym, *attributes.values
end