lib/society/formatter/graph/json.rb in society-1.0.0 vs lib/society/formatter/graph/json.rb in society-1.1.0

- old
+ new

@@ -4,12 +4,11 @@ module Formatter module Graph class JSON def initialize(graph) - @nodes = graph.nodes - @edges = graph.edges + @graph = graph end def to_json to_hash.to_json end @@ -20,23 +19,34 @@ edges: named_edges.map do |edge| { from: node_names.index(edge.from), to: node_names.index(edge.to) } - end + end, + clusters: clusters_of_indices } end private - attr_reader :nodes, :edges + attr_reader :graph def node_names - @node_names ||= nodes.map(&:full_name).uniq + @node_names ||= graph.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) } + @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