Class: Sitepress::Node

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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_FORMAT =
:html
DEFAULT_NAME =
"index".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Node.

Yields:

  • (_self)

Yield Parameters:



17
18
19
20
21
22
23
24
25
# File 'lib/sitepress/node.rb', line 17

def initialize(parent: nil, name: nil, default_format: DEFAULT_FORMAT, default_name: DEFAULT_NAME)
  @name = name.freeze
  @parent = parent
  @children = Hash.new
  @resources = Resources.new(node: self)
  @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.



11
12
13
# File 'lib/sitepress/node.rb', line 11

def default_format
  @default_format
end

#default_nameObject (readonly)

Returns the value of attribute default_name.



11
12
13
# File 'lib/sitepress/node.rb', line 11

def default_name
  @default_name
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/sitepress/node.rb', line 11

def name
  @name
end

#parentObject

Returns the value of attribute parent.



11
12
13
# File 'lib/sitepress/node.rb', line 11

def parent
  @parent
end

#resourcesObject (readonly)

Returns the value of attribute resources.



11
12
13
# File 'lib/sitepress/node.rb', line 11

def resources
  @resources
end

Instance Method Details

#child(name) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/sitepress/node.rb', line 83

def child(name)
  return self if name == default_name

  @children.fetch(name){ @children[name] = build_child(name: name) }.tap do |child|
    yield child if block_given?
  end
end

#child?(name) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/sitepress/node.rb', line 91

def child?(name)
  @children.key? name
end

#childrenObject

Returns the immediate children nodes.



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

def children
  @children.values
end

#dig(*args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/sitepress/node.rb', line 99

def dig(*args)
  head, *tail = args
  if (head.nil? or head.empty? or head == default_name) and tail.empty?
    self
  elsif child?(head)
    @children[head].dig(*tail)
  else
    nil
  end
end

#get(path) ⇒ Object



77
78
79
80
81
# File 'lib/sitepress/node.rb', line 77

def get(path)
  path = Path.new(path)
  node = dig(*path.node_names)
  node.resources.format(path.format) if node
end

#inspectObject



95
96
97
# File 'lib/sitepress/node.rb', line 95

def inspect
  "<#{self.class}: name=#{name.inspect}, formats=#{formats.inspect}, children=#{children.map(&:name).inspect}, resource_request_paths=#{resources.map(&:request_path)}>"
end

#leaf?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/sitepress/node.rb', line 46

def leaf?
  @children.empty?
end

#parentsObject

Returns all parents up to the root node.



38
39
40
# File 'lib/sitepress/node.rb', line 38

def parents
  Enumerator.produce(parent, &:parent).take_while(&:itself)
end

#removeObject



71
72
73
74
75
# File 'lib/sitepress/node.rb', line 71

def remove
  return if @parent.nil?
  @parent.remove_child name
  @parent = nil
end

#root?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/sitepress/node.rb', line 42

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