Sha256: f41f359e9a15559a850c073f5005a59ee01f3715b40087ede62d8de07c496051

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

require 'json'

module Society
  module Formatter
    module Graph
      class JSON

        def initialize(graph)
          @graph = graph
        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,
            clusters: clusters_of_indices
          }
        end

        private

        attr_reader :graph

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

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

        def clusters_of_indices
          Society::Clusterer.new.cluster(graph_of_names).map do |cluster|
            cluster.map { |name| node_names.index(name) }
          end
        end

        def graph_of_names
          ObjectGraph.new(nodes: node_names, edges: named_edges)
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
society-1.1.1 lib/society/formatter/graph/json.rb
society-1.1.0 lib/society/formatter/graph/json.rb