Sha256: d0605920c4c92ad9643e7b03935cf7af595aa281c1b28ffa16be9bedc70bbd0b

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require "securerandom" unless defined?(SecureRandom)

module Lite
  module Command
    module Internals
      module Resultable

        def index
          @index ||= context.index ||= 0
        end

        def cid
          @cid ||= context.cid
        end

        def outcome
          return state if pending? || thrown_fault?

          status
        end

        def results
          @results ||= context.results ||= []
        end

        def to_hash
          {
            index:,
            cid:,
            command: self.class.name,
            outcome:,
            state:,
            status:,
            reason:,
            fault: faulter&.index,
            throw: thrower&.index,
            runtime:
          }.compact
        end
        alias to_h to_hash

        private

        def assign_execution_cid
          context.cid ||= SecureRandom.uuid
        end

        def increment_execution_index
          @index = context.index = index.next
        end

        def start_monotonic_time
          @start_monotonic_time ||= Process.clock_gettime(Process::CLOCK_MONOTONIC)
        end

        def stop_monotonic_time
          @stop_monotonic_time ||= Process.clock_gettime(Process::CLOCK_MONOTONIC)
        end

        def runtime
          stop_monotonic_time - start_monotonic_time
        end

        def append_execution_result
          results.push(self).sort_by!(&:index)
        end

        def freeze_execution_objects
          context.freeze if index == 1
          freeze
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lite-command-2.0.1 lib/lite/command/internals/resultable.rb
lite-command-2.0.0 lib/lite/command/internals/resultable.rb