Class: Pug::Action::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/pug/action/controller.rb

Overview

Controls the execution of an Action serially

Instance Method Summary collapse

Constructor Details

#initialize(actions) ⇒ Controller

Returns a new instance of Controller.

Parameters:



8
9
10
11
# File 'lib/pug/action/controller.rb', line 8

def initialize(actions)
  @current_action = nil
  @actions = actions || []
end

Instance Method Details

#action_inputInput

Provides information about the Action’s input requirements

Returns:

  • (Input)

    describing the requirements



27
28
29
30
31
# 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?Boolean

Indicates if there are any actions to manage

Returns:

  • (Boolean)

    If there are any actions



15
16
17
# File 'lib/pug/action/controller.rb', line 15

def actions?
  !@actions.empty?
end

#can_start_action?(index) ⇒ Boolean

Note:

This will return false if there is a currently running action

Determines if an Action can start for a given index

Parameters:

  • index (Integer)

    the index of the Action in actions

Returns:

  • (Boolean)

    indicating if an Action can be started at this index



37
38
39
40
# 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) ⇒ Pug::Types::Result

Runs the Action prepared by #start_action with provided input

Parameters:

  • input (String)

    the input to pass to the Action

Returns:



54
55
56
57
58
59
60
61
62
# 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?Boolean

Indicates if there is a currently running Action

Returns:

  • (Boolean)

    if there is an action currently running



21
22
23
# File 'lib/pug/action/controller.rb', line 21

def running_action?
  !@current_action.nil?
end

#start_action(index) ⇒ Object

Starts up the Action at the given index if possible

Parameters:

  • index (Integer)

    the index of the Action in actions



44
45
46
47
48
49
# 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