class Sitepress::Node

Resource nodes give resources their parent/sibling/child relationships. The relationship are determined by the `request_path` given to an asset when its added to a node. Given the `request_path` `/foo/bar/biz/buz.html`, a tree of resource nodes would be built named `foo`, `bar`, `biz`, `buz`. `foo` would be the “root” node and `buz` a leaf node. The actual `buz.html` asset is then stored on the leaf node as a resource. This tree structure makes it possible to reason through path relationships from code to build out elements in a website like tree navigation.

Constants

DEFAULT_EXTENSION

Default extension

DEFAULT_NAME

Attributes

default_format[R]
default_name[R]
name[R]
parent[R]

Public Class Methods

new(parent: nil, name: nil, default_format: DEFAULT_EXTENSION, default_name: DEFAULT_NAME) { |self| ... } click to toggle source
# File lib/sitepress/node.rb, line 15
def initialize(parent: nil, name: nil, default_format: DEFAULT_EXTENSION, default_name: DEFAULT_NAME)
  @parent = parent
  @name = name.freeze
  @default_format = default_format
  @default_name = default_name
  yield self if block_given?
end

Public Instance Methods

add_child(name) { |node| ... } click to toggle source
# File lib/sitepress/node.rb, line 75
def add_child(name)
  return self if name == default_name
  child_nodes[name].tap do |node|
    yield node if block_given?
  end
end
children() click to toggle source

Returns the immediate children nodes.

# File lib/sitepress/node.rb, line 28
def children
  child_nodes.values
end
dig(*args) click to toggle source
# File lib/sitepress/node.rb, line 86
def dig(*args)
  head, *tail = args
  if (head.nil? or head.empty? or head == default_name) and tail.empty?
    self
  elsif child_nodes.has_key?(head)
    child_nodes[head].dig(*tail)
  else
    nil
  end
end
flatten(resources: []) click to toggle source
# File lib/sitepress/node.rb, line 56
def flatten(resources: [])
  formats.each{ |resource| resources << resource }
  children.each do |child|
    child.flatten.each{ |resource| resources << resource }
  end
  resources
end
formats() click to toggle source
# File lib/sitepress/node.rb, line 23
def formats
  @formats ||= Formats.new(node: self)
end
get(path) click to toggle source
# File lib/sitepress/node.rb, line 69
def get(path)
  path = Path.new(path)
  node = dig(*path.node_names)
  node.formats.get(path.format) if node
end
inspect() click to toggle source
# File lib/sitepress/node.rb, line 82
def inspect
  "<#{self.class}: name=#{name.inspect}, formats=#{formats.extensions.inspect}, children=#{children.map(&:name).inspect}, resource_request_paths=#{formats.map(&:request_path)}>"
end
leaf?() click to toggle source
# File lib/sitepress/node.rb, line 52
def leaf?
  child_nodes.empty?
end
parents() click to toggle source

Returns all parents up to the root node.

# File lib/sitepress/node.rb, line 38
def parents
  parents = []
  node = parent
  while node do
    parents << node
    node = node.parent
  end
  parents
end
remove() click to toggle source
# File lib/sitepress/node.rb, line 64
def remove
  formats.clear
  parent.remove_child(name) if leaf?
end
root?() click to toggle source
# File lib/sitepress/node.rb, line 48
def root?
  parent.nil?
end
siblings() click to toggle source

Returns sibling nodes and self.

# File lib/sitepress/node.rb, line 33
def siblings
  parent ? parent.children : []
end

Protected Instance Methods

remove_child(name) click to toggle source
# File lib/sitepress/node.rb, line 98
def remove_child(name)
  child_nodes.delete(name)
end

Private Instance Methods

build_child(name) click to toggle source
# File lib/sitepress/node.rb, line 103
def build_child(name)
  Node.new(parent: self, name: name, default_format: default_format, default_name: default_name)
end
child_nodes() click to toggle source
# File lib/sitepress/node.rb, line 107
def child_nodes
  @child_nodes ||= Hash.new { |hash, key| hash[key] = build_child(key) }
end