Class: Seafoam::JSONWriter
- Inherits:
-
Object
- Object
- Seafoam::JSONWriter
- Defined in:
- lib/seafoam/json_writer.rb
Overview
Write files in a JSON format.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(out) ⇒ JSONWriter
constructor
A new instance of JSONWriter.
- #write(name, graph) ⇒ Object
Constructor Details
#initialize(out) ⇒ JSONWriter
Returns a new instance of JSONWriter.
8 9 10 |
# File 'lib/seafoam/json_writer.rb', line 8 def initialize(out) @out = out end |
Class Method Details
.prepare_json(object) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/seafoam/json_writer.rb', line 42 def prepare_json(object) case object when Float if object.nan? "[NaN]" else object end when Array object.map { |o| prepare_json(o) } when Hash object.transform_values { |v| prepare_json(v) } else object end end |
Instance Method Details
#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 |