From 155de05e8c45d42f40e7f8c90a47617092b38047 Mon Sep 17 00:00:00 2001 From: alpaca-tc Date: Wed, 3 Apr 2024 23:54:11 +0900 Subject: [PATCH] Render one arrow at between modules if compound=1 --- lib/diver_down/web/definition_to_dot.rb | 30 +++++++++++++------ spec/diver_down/web/definition_to_dot_spec.rb | 9 ++++-- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/diver_down/web/definition_to_dot.rb b/lib/diver_down/web/definition_to_dot.rb index 0e87b31..cb443c0 100644 --- a/lib/diver_down/web/definition_to_dot.rb +++ b/lib/diver_down/web/definition_to_dot.rb @@ -88,6 +88,7 @@ def initialize(definition, compound: false, concentrate: false) @io = DiverDown::IndentedStringIo.new @indent = 0 @compound = compound + @compound_map = Hash.new { |h, k| h[k] = Set.new } # { ltail => Set } @concentrate = concentrate end @@ -122,14 +123,25 @@ def insert_source(source) source.dependencies.each do attributes = {} - - if @compound - ltail = module_label(*source.modules) - lhead = module_label(*definition.source(_1.source_name).modules) - - attributes.merge!(ltail:, lhead:) + ltail = module_label(*source.modules) + lhead = module_label(*definition.source(_1.source_name).modules) + skip_rendering = false + + if @compound && (ltail || lhead) + # Rendering of dependencies between modules is done only once + between_modules = ltail != lhead + skip_rendering ||= @compound_map[ltail].include?(lhead) if between_modules + @compound_map[ltail].add(lhead) + + attributes.merge!( + ltail:, + lhead:, + minlen: (between_modules ? 3 : nil) # Between modules is prominently distanced + ) end + next if skip_rendering + io.write(%("#{source.source_name}" -> "#{_1.source_name}")) io.write(%( #{build_attributes(**attributes)}), indent: false) unless attributes.empty? io.write("\n") @@ -176,10 +188,10 @@ def insert_modules(source) # rubocop:disable Lint/UnderscorePrefixedVariableName # attrsの参考 https://qiita.com/rubytomato@github/items/51779135bc4b77c8c20d def build_attributes(_wrap: '[]', **attrs) - attrs_str = attrs.filter_map { %(#{_1}="#{_2}") if _2 }.join(ATTRIBUTE_DELIMITER) - attrs.merge!(label: 'a-b', headlabel: 'head', taillabel: 'tail') + attrs = attrs.reject { _2.nil? || _2 == '' } + return if attrs.empty? - return if attrs_str.empty? + attrs_str = attrs.map { %(#{_1}="#{_2}") }.join(ATTRIBUTE_DELIMITER) if _wrap "#{_wrap[0]}#{attrs_str}#{_wrap[1]}" diff --git a/spec/diver_down/web/definition_to_dot_spec.rb b/spec/diver_down/web/definition_to_dot_spec.rb index 15dfc46..e9bc3fc 100644 --- a/spec/diver_down/web/definition_to_dot_spec.rb +++ b/spec/diver_down/web/definition_to_dot_spec.rb @@ -54,6 +54,9 @@ def build_definition(title: 'title', sources: []) }, ], }, + { + source_name: 'b.rb', + } ] ) @@ -61,6 +64,7 @@ def build_definition(title: 'title', sources: []) strict digraph "title" { "a.rb" [label="a.rb"] "a.rb" -> "b.rb" + "b.rb" [label="b.rb"] } DOT end @@ -79,7 +83,7 @@ def build_definition(title: 'title', sources: []) module_name: 'B', }, ], - }, + } ] ) @@ -137,8 +141,7 @@ def build_definition(title: 'title', sources: []) subgraph "cluster_A" { label="A" "a.rb" [label="a.rb"] } - "a.rb" -> "b.rb" [ltail="cluster_A" lhead="cluster_B"] - "a.rb" -> "c.rb" [ltail="cluster_A" lhead="cluster_B"] + "a.rb" -> "b.rb" [ltail="cluster_A" lhead="cluster_B" minlen="3"] subgraph "cluster_B" { label="B" "b.rb" [label="b.rb"] }