-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlink_expansion.rb
54 lines (43 loc) · 1.73 KB
/
link_expansion.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
46
47
48
49
50
51
52
53
54
class ExpansionRules::LinkExpansion
def initialize(rules)
@rules = rules
@multi_level_links = ExpansionRules::MultiLevelLinks.new(
rules::MULTI_LEVEL_LINK_PATHS,
)
end
delegate :allowed_link_types, to: :multi_level_links
def allowed_direct_link_types(link_types_path)
multi_level_links.allowed_link_types(link_types_path).reject do |link_type|
rules.is_reverse_link_type?(link_type)
end
end
def allowed_reverse_link_types(link_types_path)
multi_level_links.allowed_link_types(link_types_path).select do |link_type|
rules.is_reverse_link_type?(link_type)
end
end
def next_allowed_link_types(link_types, link_types_path = [])
if link_types.nil?
link_types = rules::MULTI_LEVEL_LINK_PATHS.flat_map(&:first)
.reject { |link_type| rules.is_reverse_link_type?(link_type) }
end
multi_level_links.next_allowed_link_types(link_types, link_types_path)
end
def next_allowed_direct_link_types(link_types, link_types_path = [], reverse_to_direct: false)
next_allowed = next_allowed_link_types(link_types, link_types_path)
rules.next_allowed_direct_link_types(next_allowed, reverse_to_direct:)
end
def next_allowed_reverse_link_types(link_types, link_types_path = [], reverse_to_direct: false)
next_allowed = next_allowed_link_types(link_types, link_types_path)
rules.next_allowed_reverse_link_types(next_allowed, reverse_to_direct:)
end
def valid_link_types_path?(link_types_path)
valid_paths = multi_level_links.paths(
length: link_types_path.length,
)
valid_paths.include?(link_types_path)
valid_paths.any? { |path| path[0...link_types_path.length] == link_types_path }
end
private
attr_reader :rules, :multi_level_links
end