Class: ManageIQ::Floe::Workflow::State

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/manageiq/floe/workflow/state.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #logger

Constructor Details

#initialize(workflow, name, payload) ⇒ State

Returns a new instance of State.



25
26
27
28
29
30
31
32
# File 'lib/manageiq/floe/workflow/state.rb', line 25

def initialize(workflow, name, payload)
  @workflow = workflow
  @name     = name
  @payload  = payload
  @end      = !!payload["End"]
  @type     = payload["Type"]
  @comment  = payload["Comment"]
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



23
24
25
# File 'lib/manageiq/floe/workflow/state.rb', line 23

def comment
  @comment
end

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/manageiq/floe/workflow/state.rb', line 23

def name
  @name
end

#payloadObject (readonly)

Returns the value of attribute payload.



23
24
25
# File 'lib/manageiq/floe/workflow/state.rb', line 23

def payload
  @payload
end

#typeObject (readonly)

Returns the value of attribute type.



23
24
25
# File 'lib/manageiq/floe/workflow/state.rb', line 23

def type
  @type
end

#workflowObject (readonly)

Returns the value of attribute workflow.



23
24
25
# File 'lib/manageiq/floe/workflow/state.rb', line 23

def workflow
  @workflow
end

Class Method Details

.build!(workflow, name, payload) ⇒ Object



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

def build!(workflow, name, payload)
  state_type = payload["Type"]

  begin
    klass = ManageIQ::Floe::Workflow::States.const_get(state_type)
  rescue NameError
    raise ManageIQ::Floe::InvalidWorkflowError, "Invalid state type: [#{state_type}]"
  end

  klass.new(workflow, name, payload)
end

Instance Method Details

#contextObject



38
39
40
# File 'lib/manageiq/floe/workflow/state.rb', line 38

def context
  workflow.context
end

#end?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/manageiq/floe/workflow/state.rb', line 34

def end?
  @end
end

#run!(input) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/manageiq/floe/workflow/state.rb', line 42

def run!(input)
  logger.info("Running state: [#{name}] with input [#{input}]")

  input = input_path.value(context, input)

  output, next_state = block_given? ? yield(input) : input
  next_state ||= workflow.states_by_name[payload["Next"]] unless end?

  output ||= input
  output   = output_path&.value(context, output)

  logger.info("Running state: [#{name}] with input [#{input}]...Complete - next state: [#{next_state&.name}] output: [#{output}]")

  [next_state, output]
end

#to_dotObject



58
59
60
61
62
63
64
65
# File 'lib/manageiq/floe/workflow/state.rb', line 58

def to_dot
  String.new.tap do |s|
    s << "  #{name}"

    attributes = to_dot_attributes
    s << " [ #{attributes.to_a.map { |kv| kv.join("=") }.join(" ")} ]" unless attributes.empty?
  end
end

#to_dot_transitionsObject



71
72
73
74
# File 'lib/manageiq/floe/workflow/state.rb', line 71

def to_dot_transitions
  next_state_name = payload["Next"] unless end?
  Array(next_state_name && "  #{name} -> #{next_state_name}")
end