Sha256: d6ae3433cb561eed37c9eabc72a14ff57e5e1a18c7be6574d827165d130036a5

Contents?: true

Size: 872 Bytes

Versions: 1

Compression:

Stored size: 872 Bytes

Contents

require 'json'

module Society
  module Formatter
    module Graph
      class JSON

        def initialize(graph)
          @nodes = graph.nodes
          @edges = graph.edges
        end

        def to_json
          to_hash.to_json
        end

        def to_hash
          {
            nodes: node_names.map { |name| { name: name } },
            edges: named_edges.map do |edge|
              {
                from: node_names.index(edge.from),
                to: node_names.index(edge.to)
              }
            end
          }
        end

        private

        attr_reader :nodes, :edges

        def node_names
          @node_names ||= nodes.map(&:full_name).uniq
        end

        def named_edges
          @named_edges ||= edges.map { |edge| Edge.new(from: edge.from.full_name, to: edge.to.full_name) }
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
society-1.0.0 lib/society/formatter/graph/json.rb