Method: Seafoam::JSONWriter#write

Defined in:
lib/seafoam/json_writer.rb

#write(name, graph) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/seafoam/json_writer.rb', line 12

def write(name, graph)
  nodes = []
  edges = []

  graph.nodes.each_value do |node|
    nodes.push(
      id: node.id,
      props: node.props,
    )

    node.outputs.each do |edge|
      edges.push(
        from: edge.from.id,
        to: edge.to.id,
        props: edge.props,
      )
    end
  end

  object = {
    name: name,
    props: graph.props,
    nodes: nodes,
    edges: edges,
  }

  @out.puts JSON.pretty_generate(self.class.prepare_json(object))
end