2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-04-11 13:53:59 +02:00
|
|
|
module TreeableConcern
|
2023-03-24 11:08:31 +01:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
MAX_DEPTH = 6 # deepest level for header_sections is 3.
|
|
|
|
# but a repetition can be nested an header_section, so 3+3=6=MAX_DEPTH
|
|
|
|
|
|
|
|
included do
|
2023-11-28 17:27:09 +01:00
|
|
|
# as we progress in the list of ordered types_de_champ
|
2023-04-14 10:55:36 +02:00
|
|
|
# we keep a reference to each level of nesting (walk)
|
2023-03-24 11:08:31 +01:00
|
|
|
# when we encounter an header_section, it depends of its own depth of nesting minus 1, ie:
|
2023-04-14 10:55:36 +02:00
|
|
|
# h1 belongs to prior (rooted_tree)
|
2023-03-24 11:08:31 +01:00
|
|
|
# h2 belongs to prior h1
|
|
|
|
# h3 belongs to prior h2
|
2023-04-14 10:55:36 +02:00
|
|
|
# h1 belongs to prior (rooted_tree)
|
2023-11-28 17:27:09 +01:00
|
|
|
# then, each and every types_de_champ which are not an header_section
|
2023-04-14 10:55:36 +02:00
|
|
|
# are added to the current_tree
|
2023-03-24 11:08:31 +01:00
|
|
|
# given a root_depth at 0, we build a full tree
|
|
|
|
# given a root_depth > 0, we build a partial tree (aka, a repetition)
|
2023-11-28 17:27:09 +01:00
|
|
|
def to_tree(types_de_champ:)
|
2023-04-14 10:55:36 +02:00
|
|
|
rooted_tree = []
|
|
|
|
walk = Array.new(MAX_DEPTH)
|
|
|
|
walk[0] = rooted_tree
|
|
|
|
current_tree = rooted_tree
|
2023-03-24 11:08:31 +01:00
|
|
|
|
2023-11-28 17:27:09 +01:00
|
|
|
types_de_champ.each do |type_de_champ|
|
|
|
|
if type_de_champ.header_section?
|
|
|
|
new_tree = [type_de_champ]
|
|
|
|
walk[type_de_champ.header_section_level_value - 1].push(new_tree)
|
|
|
|
current_tree = walk[type_de_champ.header_section_level_value] = new_tree
|
2023-03-24 11:08:31 +01:00
|
|
|
else
|
2023-11-28 17:27:09 +01:00
|
|
|
current_tree.push(type_de_champ)
|
2023-03-24 11:08:31 +01:00
|
|
|
end
|
|
|
|
end
|
2023-04-14 10:55:36 +02:00
|
|
|
rooted_tree
|
2023-03-24 11:08:31 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|