Module: Tron

Defined in:
lib/tron.rb,
lib/tron/version.rb

Overview

Return data objects that can indicate success or failure.

Defined Under Namespace

Modules: VERSION

Class Method Summary collapse

Class Method Details

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

rubocop:disable Metrics/MethodLength



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tron.rb', line 41

def self.failure(code, attributes = {}) # rubocop:disable Metrics/MethodLength
  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')

  Data.define(:failure, *attributes.keys) do
    def success?
      false
    end

    def failure?
      true
    end

    def success
      nil
    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 = {}) ⇒ Object

rubocop:disable Metrics/MethodLength



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tron.rb', line 7

def self.success(code, attributes = {}) # rubocop:disable Metrics/MethodLength
  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')

  Data.define(:success, *attributes.keys) do
    def success?
      true
    end

    def failure?
      false
    end

    def failure
      nil
    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