class Pug::Action::Controller

Controls the execution of an Action serially

Public Class Methods

new(actions) click to toggle source

@param actions [Array<Pug::Interfaces::Action>] The actions to manage

# File lib/pug/action/controller.rb, line 8
def initialize(actions)
  @current_action = nil
  @actions = actions || []
end

Public Instance Methods

action_input() click to toggle source

Provides information about the Action's input requirements @return [Input] describing the requirements

# File lib/pug/action/controller.rb, line 27
def action_input
  raise Strings.no_action_running unless running_action?
  input_required = @current_action.requires_input?
  Input.new(@current_action.name, input_required)
end
actions?() click to toggle source

Indicates if there are any actions to manage @return [Boolean] If there are any actions

# File lib/pug/action/controller.rb, line 15
def actions?
  !@actions.empty?
end
can_start_action?(index) click to toggle source

Determines if an Action can start for a given index @param index [Integer] the index of the Action in actions @return [Boolean] indicating if an Action can be started at this index @note This will return false if there is a currently running action

# File lib/pug/action/controller.rb, line 37
def can_start_action?(index)
  return false if index.negative? || index >= @actions.length
  !running_action?
end
run_action(input) click to toggle source

Runs the Action prepared by start_action with provided input @param input [String] the input to pass to the Action @return [Pug::Types::Result] indicating the execution result

# File lib/pug/action/controller.rb, line 54
def run_action(input)
  return Results.no_action_running unless running_action?

  requires_input = @current_action.requires_input?
  result = @current_action.execute(requires_input ? input : nil)
  output = Output.new(@current_action.name, result || '')
  @current_action = nil
  Types::Result.success(output)
end
running_action?() click to toggle source

Indicates if there is a currently running Action @return [Boolean] if there is an action currently running

# File lib/pug/action/controller.rb, line 21
def running_action?
  !@current_action.nil?
end
start_action(index) click to toggle source

Starts up the Action at the given index if possible @param index [Integer] the index of the Action in actions

# File lib/pug/action/controller.rb, line 44
def start_action(index)
  return unless can_start_action?(index)

  action = @actions[index]
  @current_action = action
end