Class: Lookbook::PageTreeBuilder
- Defined in:
- lib/lookbook/services/entities/page_tree_builder.rb
Instance Attribute Summary collapse
- #include_hidden ⇒ Object readonly
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(pages, include_hidden: false) ⇒ PageTreeBuilder
constructor
A new instance of PageTreeBuilder.
- #pages ⇒ Object
- #parse_segments(path) ⇒ Object
Methods inherited from Service
Constructor Details
#initialize(pages, include_hidden: false) ⇒ PageTreeBuilder
Returns a new instance of PageTreeBuilder.
5 6 7 8 |
# File 'lib/lookbook/services/entities/page_tree_builder.rb', line 5 def initialize(pages, include_hidden: false) @pages = pages.to_a @include_hidden = include_hidden end |
Instance Attribute Details
#include_hidden ⇒ Object (readonly)
3 4 5 |
# File 'lib/lookbook/services/entities/page_tree_builder.rb', line 3 def include_hidden @include_hidden end |
Instance Method Details
#call ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/lookbook/services/entities/page_tree_builder.rb', line 10 def call root_node = TreeNode.new pages.each do |page| current_node = root_node path_segments = parse_segments(page.relative_file_path) path_segments.each.with_index(1) do |segment, i| name, priority_prefix = segment content = page if page.depth == i # pages are always on the leaf nodes current_node.add_child(name, content, priority: priority_prefix) unless current_node.has_child?(name) current_node = current_node.get_child(name) end end root_node end |
#pages ⇒ Object
36 37 38 |
# File 'lib/lookbook/services/entities/page_tree_builder.rb', line 36 def pages include_hidden ? @pages : @pages.select(&:visible?) end |
#parse_segments(path) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/lookbook/services/entities/page_tree_builder.rb', line 27 def parse_segments(path) path.to_s.split("/").map do |segment| unless segment.start_with?(".") priority, name = PriorityPrefixParser.call(segment) [name, priority || 10000] end end.compact end |