Module: Rigor::ModuleGraph::NodeIO

Defined in:
lib/rigor/module_graph/node.rb

Overview

JSONL reader / writer for Node rows.

Class Method Summary collapse

Class Method Details

.read(io) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rigor/module_graph/node.rb', line 124

def read(io)
  nodes = []
  io.each_line do |line|
    line = line.strip
    next if line.empty?

    row = JSON.parse(line)
    nodes << Node.build(
      kind: row.fetch("kind"),
      name: row.fetch("name"),
      owner: row["owner"],
      path: row["path"],
      line: row["line"],
      column: row["column"],
      visibility: row["visibility"],
      access: row["access"]
    )
  end
  nodes
end

.write(nodes, io) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/rigor/module_graph/node.rb', line 113

def write(nodes, io)
  seen = {}
  nodes.each do |node|
    key = node.dedup_key
    next if seen[key]

    seen[key] = true
    io.puts(JSON.generate(node.to_h))
  end
end