Sha256: 8ca39249e747d0c69f97d919afa592b5ae605eba25307d84f9f62fe5ec9e99ac

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Activerecord
  module Transactionable
    class Result
      attr_reader :value, :result, :error, :type, :context, :nested, :attempt

      def initialize(value, context:, transaction_open:, attempt:, error: nil, type: nil)
        @value = value
        @result = fail? ? "fail" : "success"
        @context = context
        @nested = transaction_open ? true : false
        @attempt = attempt
        @error = error
        @type = type
      end

      def fail?
        value == false
      end

      def success?
        value == true
      end

      def to_h(skip_error: nil)
        diagnostic_data = {
          result: result,
          type: type,
          context: context,
          nested: nested,
          attempt: attempt,
        }
        if !skip_error && error
          diagnostic_data[:error] = error.class.to_s
          diagnostic_data[:message] = error.message
        end
        diagnostic_data
      end

      def to_s(skip_error: nil)
        to_h(skip_error: skip_error).to_s
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-transactionable-3.0.3 lib/activerecord/transactionable/result.rb