Sha256: 257f1d32a3c81a38f210c6eb8dcecb81919b600fa7f46e9b5aa4321c0e7aa998

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module Tataru
  # An execution step
  class ExecutionStep
    def initialize(state, instruction)
      @state = state
      @instruction = instruction
      @instances = {}
    end

    def id
      @instruction.id
    end

    def rf
      @instruction.requirements.resource_finder
    end

    def type_of_id
      @instruction.requirements.type(id)
    end

    def class_of_id
      rf.resource_named(type_of_id)
    end

    def instance_of_id
      @instances[id] ||= class_of_id.new
    end

    def execute
      return execute_begin! if @instruction.action.to_s.start_with?('begin_')

      execute_wait!
    end

    def overall_action
      @instruction.action.to_s.split('_')[1].to_sym
    end

    def execute_begin!
      instance_of_id.send(@instruction.action, @state)
      [send(:"begin_#{overall_action}"), true]
    end

    def begin_create
      new_state = @state.clone
      replacer = true
      replacer = false if new_state[id].nil?

      @instruction.state.each do |name, value|
        new_state.putstate(id, name, value, replacer: replacer)
      end
      new_state.waiting_on(id, overall_action)
      new_state
    end

    def begin_delete
      begin_update
    end

    def begin_update
      new_state = @state.clone
      new_state.waiting_on(id, overall_action)
      new_state
    end

    def execute_wait!
      new_state = @state.clone
      success = instance_of_id.send(:"#{overall_action}_complete?", @state)
      if success
        new_state.no_longer_waiting(id)
        new_state.replace(id) if overall_action == :delete
      end
      [new_state, success]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tataru-0.1.0 lib/tataru/execution_step.rb