Class: Dato::Node

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/dato/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, root = nil, parent = nil) ⇒ Node

Returns a new instance of Node.



7
8
9
10
11
# File 'app/components/dato/node.rb', line 7

def initialize(node, root = nil, parent = nil)
  @node = node
  @root = root
  @parent = parent
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



5
6
7
# File 'app/components/dato/node.rb', line 5

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'app/components/dato/node.rb', line 5

def parent
  @parent
end

#rootObject (readonly)

Returns the value of attribute root.



5
6
7
# File 'app/components/dato/node.rb', line 5

def root
  @root
end

Instance Method Details

#blocksObject



35
36
37
# File 'app/components/dato/node.rb', line 35

def blocks
  root.blocks
end

#debug_nodeObject



47
48
49
# File 'app/components/dato/node.rb', line 47

def debug_node
  ("pre", JSON.pretty_generate(@node))
end

#find_ancestor(type) ⇒ Node?

Find the first ancestor of a specific type

Parameters:

  • the type to search for (e.g., "list", "structuredText")

Returns:

  • the first ancestor matching the type or nil



20
21
22
23
24
25
26
27
# File 'app/components/dato/node.rb', line 20

def find_ancestor(type)
  current = parent
  while current
    return current if current.node_type == type
    current = current.parent
  end
  nil
end


39
40
41
# File 'app/components/dato/node.rb', line 39

def links
  root.links
end

#node_typeString?

Get the type of the immediate parent node

Returns:

  • the parent's type or nil if no parent



31
# File 'app/components/dato/node.rb', line 31

def node_type = node&.type

#overridesObject



43
44
45
# File 'app/components/dato/node.rb', line 43

def overrides
  root.overrides
end

#parent_typeObject



33
# File 'app/components/dato/node.rb', line 33

def parent_type = parent&.node_type

#render_node(node) ⇒ Object



13
14
15
# File 'app/components/dato/node.rb', line 13

def render_node(node)
  render class_for_node(node).new(node, root, self)
end