Class: DurableFlow::DefinitionGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/durable_flow/definition_graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workflow_class:, source_file:) ⇒ DefinitionGraph

Returns a new instance of DefinitionGraph.



47
48
49
50
51
52
53
54
# File 'lib/durable_flow/definition_graph.rb', line 47

def initialize(workflow_class:, source_file:)
  @workflow_class = workflow_class.to_s
  @source_file = source_file
  @nodes = []
  @edges = []
  @warnings = []
  @node_ids = Hash.new(0)
end

Instance Attribute Details

#edgesObject (readonly)

Returns the value of attribute edges.



45
46
47
# File 'lib/durable_flow/definition_graph.rb', line 45

def edges
  @edges
end

#nodesObject (readonly)

Returns the value of attribute nodes.



45
46
47
# File 'lib/durable_flow/definition_graph.rb', line 45

def nodes
  @nodes
end

#source_fileObject (readonly)

Returns the value of attribute source_file.



45
46
47
# File 'lib/durable_flow/definition_graph.rb', line 45

def source_file
  @source_file
end

#warningsObject (readonly)

Returns the value of attribute warnings.



45
46
47
# File 'lib/durable_flow/definition_graph.rb', line 45

def warnings
  @warnings
end

#workflow_classObject (readonly)

Returns the value of attribute workflow_class.



45
46
47
# File 'lib/durable_flow/definition_graph.rb', line 45

def workflow_class
  @workflow_class
end

Instance Method Details

#add_edge(from:, to:, type: "sequence", condition: nil, metadata: {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/durable_flow/definition_graph.rb', line 75

def add_edge(from:, to:, type: "sequence", condition: nil, metadata: {})
  return if from.blank? || to.blank?

  edges << DefinitionEdge.new(
    from: from,
    to: to,
    type: type.to_s,
    condition: condition.presence,
    metadata: .compact
  )
end

#add_node(type:, name:, target_workflow_class:, source_line:, metadata: {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/durable_flow/definition_graph.rb', line 56

def add_node(type:, name:, target_workflow_class:, source_line:, metadata: {})
  node_name = name.to_s.presence || "unknown"
  @node_ids[node_name] += 1
  node_id = @node_ids[node_name] == 1 ? node_name : "#{node_name}##{@node_ids[node_name]}"

  warnings << "Duplicate durable step name #{node_name.inspect} at #{source_file}:#{source_line}" if @node_ids[node_name] > 1

  DefinitionNode.new(
    id: node_id,
    type: type.to_s,
    name: node_name,
    workflow_class: workflow_class,
    target_workflow_class: target_workflow_class,
    source_file: source_file,
    source_line: source_line,
    metadata: .compact
  ).tap { |node| nodes << node }
end

#to_hObject Also known as: as_json



87
88
89
90
91
92
93
94
95
# File 'lib/durable_flow/definition_graph.rb', line 87

def to_h
  {
    workflow_class: workflow_class,
    source_file: source_file,
    nodes: nodes.map(&:to_h),
    edges: edges.map(&:to_h),
    warnings: warnings
  }
end