Class: Sitepress::ResourcesNode

Inherits:
Object
  • Object
show all
Defined in:
lib/sitepress/resources_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

DELIMITER =
"/".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil, delimiter: ResourcesNode::DELIMITER, name: nil) ⇒ ResourcesNode

Returns a new instance of ResourcesNode.



12
13
14
15
16
# File 'lib/sitepress/resources_node.rb', line 12

def initialize(parent: nil, delimiter: ResourcesNode::DELIMITER, name: nil)
  @parent = parent
  @name = name.freeze
  @delimiter = delimiter.freeze
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#add(path:, asset:) ⇒ Object Also known as: []=



69
70
71
72
73
74
75
76
77
# File 'lib/sitepress/resources_node.rb', line 69

def add(path: , asset: )
  head, *path = tokenize(path)
  if path.empty?
    # When there's no more paths, we're left with the format (e.g. ".html")
    formats.add(asset: asset, ext: head)
  else
    child_nodes[head].add(path: path, asset: asset)
  end
end

#childrenObject

Returns the immediate children nodes.



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

def children
  child_nodes.values
end

#dig(*args) ⇒ Object

TODO: I don’t really like how the path is broken up with the “ext” at the end. It feels inconsistent. Either make an object/struct that encaspulates this or just pass ‘index.html` through to the end.



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

def dig(*args)
  head, *tail = args
  if head.nil? and tail.empty?
    self
  elsif child_nodes.has_key?(head)
    child_nodes[head].dig(*tail)
  else
    nil
  end
end

#flatten(resources: []) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/sitepress/resources_node.rb', line 51

def flatten(resources: [])
  formats.each{ |resource| resources << resource }
  children.each do |child|
    child.flatten.each{ |resource| resources << resource }
  end
  resources
end

#formatsObject



18
19
20
# File 'lib/sitepress/resources_node.rb', line 18

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

#get(path) ⇒ Object



80
81
82
83
84
85
# File 'lib/sitepress/resources_node.rb', line 80

def get(path)
  *path, ext = tokenize(path)
  if node = dig(*path)
    node.formats.ext(ext)
  end
end

#get_node(path) ⇒ Object Also known as: []



87
88
89
90
# File 'lib/sitepress/resources_node.rb', line 87

def get_node(path)
  *path, _ = tokenize(path)
  dig(*path)
end

#inspectObject



93
94
95
# File 'lib/sitepress/resources_node.rb', line 93

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

#leaf?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/sitepress/resources_node.rb', line 47

def leaf?
  child_nodes.empty?
end

#parentsObject

Returns all parents up to the root node.



33
34
35
36
37
38
39
40
41
# File 'lib/sitepress/resources_node.rb', line 33

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

#removeObject



59
60
61
62
63
64
65
66
67
# File 'lib/sitepress/resources_node.rb', line 59

def remove
  if leaf?
    # TODO: Check the parents to see if they also need to be removed if
    # this call orphans the tree up to a resource.
    parent.remove_child(name)
  else
    formats.clear
  end
end

#root?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/sitepress/resources_node.rb', line 43

def root?
  parent.nil?
end

#siblingsObject

Returns sibling nodes.



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

def siblings
  parent ? parent.children.reject{ |c| c == self } : []
end