module Tron

Public Class Methods

failure(code, attributes = {}) click to toggle source
# 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
success(code, attributes = {}) click to toggle source
# File lib/tron.rb, line 10
def self.success(code, attributes = {}) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  code.respond_to?(:to_sym) ||
    raise(ArgumentError, 'Tron.success must be called with a Symbol as first argument')

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

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

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

    def success?
      true
    end

    def failure?
      false
    end

    def failure
      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 `#success` instead. Called from `#{caller.first}`"

      success
    end

    def on_success(proc = nil, &block)
      (proc || block).call self
    end

    def on_failure(_ = nil)
      self
    end
  end.new code.to_sym, *attributes.values
end

Public Instance Methods

code() click to toggle source
# File lib/tron.rb, line 36
def code
  warn 'DEPRECATION WARNING: Calling `#code` on a Tron object is deprecated and will be removed in Tron 2.0.0. ' \
       "Please use `#success` instead. Called from `#{caller.first}`"

  success
end
failure() click to toggle source
# File lib/tron.rb, line 32
def failure
  nil
end
failure?() click to toggle source
# File lib/tron.rb, line 28
def failure?
  false
end
on_failure(_ = nil) click to toggle source
# File lib/tron.rb, line 47
def on_failure(_ = nil)
  self
end
on_success(proc = nil, &block) click to toggle source
# File lib/tron.rb, line 43
def on_success(proc = nil, &block)
  (proc || block).call self
end
success() click to toggle source
# File lib/tron.rb, line 75
def success
  nil
end
success?() click to toggle source
# File lib/tron.rb, line 24
def success?
  true
end