Class: ManageIQ::Floe::Workflow::States::Choice

Inherits:
ManageIQ::Floe::Workflow::State show all
Defined in:
lib/manageiq/floe/workflow/states/choice.rb

Instance Attribute Summary collapse

Attributes inherited from ManageIQ::Floe::Workflow::State

#comment, #name, #payload, #type, #workflow

Instance Method Summary collapse

Methods inherited from ManageIQ::Floe::Workflow::State

build!, #context, #end?, #to_dot

Methods included from Logging

included, #logger

Constructor Details

#initialize(workflow, name, payload) ⇒ Choice

Returns a new instance of Choice.



10
11
12
13
14
15
16
17
18
# File 'lib/manageiq/floe/workflow/states/choice.rb', line 10

def initialize(workflow, name, payload)
  super

  @choices = payload["Choices"].map { |choice| ChoiceRule.build(choice) }
  @default = payload["Default"]

  @input_path  = Path.new(payload.fetch("InputPath", "$"))
  @output_path = Path.new(payload.fetch("OutputPath", "$"))
end

Instance Attribute Details

#choicesObject (readonly)

Returns the value of attribute choices.



8
9
10
# File 'lib/manageiq/floe/workflow/states/choice.rb', line 8

def choices
  @choices
end

#defaultObject (readonly)

Returns the value of attribute default.



8
9
10
# File 'lib/manageiq/floe/workflow/states/choice.rb', line 8

def default
  @default
end

#input_pathObject (readonly)

Returns the value of attribute input_path.



8
9
10
# File 'lib/manageiq/floe/workflow/states/choice.rb', line 8

def input_path
  @input_path
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



8
9
10
# File 'lib/manageiq/floe/workflow/states/choice.rb', line 8

def output_path
  @output_path
end

Instance Method Details

#run!Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/manageiq/floe/workflow/states/choice.rb', line 20

def run!(*)
  super do |input|
    next_state_name = choices.detect { |choice| choice.true?(context, input) }&.next || default
    next_state      = workflow.states_by_name[next_state_name]

    output = input

    [output, next_state]
  end
end

#to_dot_transitionsObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/manageiq/floe/workflow/states/choice.rb', line 35

def to_dot_transitions
  [].tap do |a|
    choices.each do |choice|
      choice_label =
        if choice.payload["NumericEquals"]
          "#{choice.variable} == #{choice.payload["NumericEquals"]}"
        else
          "Unknown" # TODO
        end

      a << "  #{name} -> #{choice.next} [ label=#{choice_label.inspect} ]"
    end

    a << "  #{name} -> #{default} [ label=\"Default\" ]" if default
  end
end