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.
6 7 8 |
# File 'lib/seafoam/json_writer.rb', line 6 def initialize(out) @out = out end |
Class Method Details
.prepare_json(object) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/seafoam/json_writer.rb', line 39 def self.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
10 11 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 |
# File 'lib/seafoam/json_writer.rb', line 10 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(prepare_json(object)) end |