Class: Mint::DocumentTree

Inherits:
Object
  • Object
show all
Defined in:
lib/mint/document_tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(documents) ⇒ DocumentTree

Initializes a new DocumentTree with the given documents

Parameters:

  • documents (Array<Document>)

    array of documents to add to the tree



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

#nodesObject (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

Parameters:

  • reference_pathname (Pathname)

    path to the reference pathname

Returns:

  • (DocumentTree)

    new DocumentTree with paths relative to reference_pathname



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.

Parameters:

  • max_depth (Integer) (defaults to: nil)

    maximum navigation depth (optional)

Returns:

  • (Array<Hash>)

    flattened array of navigation items



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