Sha256: 456d135a34780c07c066d0dc5281e24917ad526e8d6e0e09f4adf433cfae6555

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module Lite
  module Command

    # State represents the state of the executable code. Once `execute`
    # is ran, it will always complete or dnf if a fault is thrown by a
    # child command.
    STATES = [
      PENDING = "pending",
      EXECUTING = "executing",
      COMPLETE = "complete",
      DNF = "dnf"
    ].freeze

    module Internals
      module Executable

        def execute
          around_execution { call }
        rescue StandardError => e
          fn = e.respond_to?(:fault_name) ? e.fault_name : ERROR

          send(:"#{fn}", e)
          after_execution
          send(:"on_#{fn}", e)
        end

        def execute!
          around_execution { call }
        rescue StandardError => e
          after_execution

          raise(e) unless raise_dynamic_faults? && e.is_a?(Lite::Command::Fault)

          raise_dynamic_fault(e)
        end

        def state
          @state || PENDING
        end

        def executed?
          dnf? || complete?
        end

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

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

        private

        def before_execution
          increment_execution_index
          assign_execution_cid
          start_monotonic_time
          executing!
          on_before_execution
        end

        def after_execution
          fault? ? dnf! : complete!
          on_after_execution
          stop_monotonic_time
          append_execution_result
          freeze_execution_objects
        end

        def around_execution
          before_execution
          yield
          after_execution
        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/executable.rb
lite-command-2.0.0 lib/lite/command/internals/executable.rb