Skip to content

Commit

Permalink
Render one arrow at between modules if compound=1
Browse files Browse the repository at this point in the history
  • Loading branch information
alpaca-tc committed Apr 3, 2024
1 parent 5c20da0 commit 155de05
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
30 changes: 21 additions & 9 deletions lib/diver_down/web/definition_to_dot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<lhead> }
@concentrate = concentrate
end

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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]}"
Expand Down
9 changes: 6 additions & 3 deletions spec/diver_down/web/definition_to_dot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ def build_definition(title: 'title', sources: [])
},
],
},
{
source_name: 'b.rb',
}
]
)

expect(described_class.new(definition).to_s).to eq(<<~DOT)
strict digraph "title" {
"a.rb" [label="a.rb"]
"a.rb" -> "b.rb"
"b.rb" [label="b.rb"]
}
DOT
end
Expand All @@ -79,7 +83,7 @@ def build_definition(title: 'title', sources: [])
module_name: 'B',
},
],
},
}
]
)

Expand Down Expand Up @@ -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"]
}
Expand Down

0 comments on commit 155de05

Please sign in to comment.