Class: Sitepress::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/sitepress/node.rb

Overview

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.

Constant Summary collapse

DEFAULT_EXTENSION =

Default extension

:html
DEFAULT_NAME =
"index".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil, name: nil, default_format: DEFAULT_EXTENSION, default_name: DEFAULT_NAME) {|_self| ... } ⇒ Node

Returns a new instance of Node.

Yields:

  • (_self)

Yield Parameters:



15
16
17
18
19
20
21
# 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

Instance Attribute Details

#default_formatObject (readonly)

Returns the value of attribute default_format.



8
9
10
# File 'lib/sitepress/node.rb', line 8

def default_format
  @default_format
end

#default_nameObject (readonly)

Returns the value of attribute default_name.



8
9
10
# File 'lib/sitepress/node.rb', line 8

def default_name
  @default_name
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/sitepress/node.rb', line 8

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/sitepress/node.rb', line 8

def parent
  @parent
end

Instance Method Details

#add_child(name) ⇒ Object



75
76
77
78
79
80
# 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

#childrenObject

Returns the immediate children nodes.



28
29
30
# File 'lib/sitepress/node.rb', line 28

def children
  child_nodes.values
end

#dig(*args) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# 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: []) ⇒ Object



56
57
58
59
60
61
62
# 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

#formatsObject



23
24
25
# File 'lib/sitepress/node.rb', line 23

def formats
  @formats ||= Formats.new(node: self)
end

#get(path) ⇒ Object



69
70
71
72
73
# 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

#inspectObject



82
83
84
# 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?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/sitepress/node.rb', line 52

def leaf?
  child_nodes.empty?
end

#parentsObject

Returns all parents up to the root node.



38
39
40
41
42
43
44
45
46
# File 'lib/sitepress/node.rb', line 38

def parents
  parents = []
  node = parent
  while node do
    parents << node
    node = node.parent
  end
  parents
end

#removeObject



64
65
66
67
# File 'lib/sitepress/node.rb', line 64

def remove
  formats.clear
  parent.remove_child(name) if leaf?
end

#root?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/sitepress/node.rb', line 48

def root?
  parent.nil?
end

#siblingsObject

Returns sibling nodes and self.



33
34
35
# File 'lib/sitepress/node.rb', line 33

def siblings
  parent ? parent.children : []
end