Class: Mint::DocumentTree
- Inherits:
-
Object
- Object
- Mint::DocumentTree
- Defined in:
- lib/mint/document_tree.rb
Instance Attribute Summary collapse
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
-
#initialize(documents) ⇒ DocumentTree
constructor
Initializes a new DocumentTree with the given documents.
-
#reorient(reference_pathname) ⇒ DocumentTree
Returns new DocumentTree with paths relative to reference_pathname Preserves the tree structure but reorients all paths.
-
#serialize(max_depth: nil) ⇒ Array<Hash>
Serializes the tree to a flat array for ERB template consumption.
Constructor Details
#initialize(documents) ⇒ DocumentTree
Initializes a new DocumentTree with the given documents
10 11 12 13 14 15 16 |
# File 'lib/mint/document_tree.rb', line 10 def initialize(documents) @nodes = [] documents.each do |document| add_document(document.destination_path, document.title) end sort_nodes! end |
Instance Attribute Details
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
5 6 7 |
# File 'lib/mint/document_tree.rb', line 5 def nodes @nodes end |
Instance Method Details
#reorient(reference_pathname) ⇒ DocumentTree
Returns new DocumentTree with paths relative to reference_pathname Preserves the tree structure but reorients all paths
23 24 25 26 27 28 |
# File 'lib/mint/document_tree.rb', line 23 def reorient(reference_pathname) reoriented_nodes = reorient_nodes(@nodes, reference_pathname) new_tree = DocumentTree.allocate new_tree.instance_variable_set(:@nodes, reoriented_nodes) new_tree end |
#serialize(max_depth: nil) ⇒ Array<Hash>
Serializes the tree to a flat array for ERB template consumption
ERB templates cannot easily handle recursive tree structures with arbitrary depth, so we flatten the tree into a simple array of hashes that the template can iterate over. Each hash contains the navigation item data (title, paths, depth) needed for rendering.
38 39 40 41 42 |
# File 'lib/mint/document_tree.rb', line 38 def serialize(max_depth: nil) result = [] flatten_nodes(@nodes, result, max_depth: max_depth) result end |