Sha256: 6e8bb1dd9e6151eba757e257d05312ae36f4a81ef080bfd9257a8943c795d299

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module Lite
  module Command

    STATES = [
      PENDING     = "pending",
      EXECUTING   = "executing",
      COMPLETE    = "complete",
      INTERRUPTED = "interrupted"
    ].freeze

    module Internals
      module Executions

        def state
          @state || PENDING
        end

        def executed?
          complete? || interrupted?
        end

        STATES.each do |s|
          # eg: executing?
          define_method(:"#{s}?") { state == s }

          # eg: interrupted!
          define_method(:"#{s}!") { @state = s }
        end

        private

        def before_execution
          increment_execution_index
          assign_execution_cmd_id
          start_monotonic_time
          Utils.try(self, :on_before_validation)
          validate_context_attributes
          Utils.try(self, :on_before_execution)
          executing!
          Utils.try(self, :on_executing)
        end

        def after_execution
          send(:"#{success? ? COMPLETE : INTERRUPTED}!")
          Utils.try(self, :on_after_execution)
          stop_monotonic_time
          append_execution_result
          freeze_execution_objects
        end

        def around_execution
          before_execution
          yield
          after_execution
        end

        def execute
          around_execution { call }
          Utils.try(self, :on_success)
        rescue StandardError => e
          fault(e, ERROR, metadata) unless e.is_a?(Lite::Command::Fault)
          after_execution
          Utils.try(self, :"on_#{status}", e)
        ensure
          Utils.try(self, :"on_#{state}")
        end

        def execute!
          around_execution { call }
          Utils.try(self, :on_success)
        rescue StandardError => e
          fault(e, ERROR, metadata) unless e.is_a?(Lite::Command::Fault)
          after_execution
          Utils.try(self, :"on_#{status}", e)
          raise(e)
        else
          Utils.try(self, :"on_#{state}")
        end

      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lite-command-3.0.1 lib/lite/command/internals/executions.rb
lite-command-3.0.0 lib/lite/command/internals/executions.rb