forked from alphagov/publishing-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_graph.rb
45 lines (39 loc) · 1.21 KB
/
link_graph.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# This class is used to represent the graph of links for an edition.
# It is used in [link expansion](../docs/link-expansion.md) and
# [dependency resolution](../docs/depedency-resolution.md)
class LinkGraph
attr_reader :root_content_id, :root_locale, :with_drafts, :link_reference
# link_reference is an object that can be queried to determine the links
# for given inputs. It is the source of information for representing this
# graph.
def initialize(
root_content_id:,
root_locale:,
link_reference:,
with_drafts: false
)
@root_content_id = root_content_id
@root_locale = root_locale
@with_drafts = with_drafts
@link_reference = link_reference
end
def links
@links ||= NodeCollectionFactory.new(self).collection
end
def links_content_ids
links.flat_map { |link| [link.content_id] + link.links_content_ids }.uniq
end
def to_s
"LinkGraph(#{root_content_id}, #{root_locale})"
end
def inspect
"LinkGraph(content_id: #{root_content_id}, locale: #{root_locale}, "\
"with_drafts: #{with_drafts}, links: #{links.map(&:content_id)})"
end
def to_h
links.group_by(&:link_type)
.transform_values do |links|
links.map(&:to_h)
end
end
end