Class: Sitepress::ResourcesNode
- Inherits:
-
Object
- Object
- Sitepress::ResourcesNode
- 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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #add(path:, asset:) ⇒ Object (also: #[]=)
-
#children ⇒ Object
Returns the immediate children nodes.
-
#dig(*args) ⇒ Object
TODO: I don’t really like how the path is broken up with the “ext” at the end.
- #flatten(resources: []) ⇒ Object
- #formats ⇒ Object
- #get(path) ⇒ Object
- #get_node(path) ⇒ Object (also: #[])
-
#initialize(parent: nil, delimiter: ResourcesNode::DELIMITER, name: nil) ⇒ ResourcesNode
constructor
A new instance of ResourcesNode.
- #inspect ⇒ Object
- #leaf? ⇒ Boolean
-
#parents ⇒ Object
Returns all parents up to the root node.
- #remove ⇒ Object
- #root? ⇒ Boolean
-
#siblings ⇒ Object
Returns sibling nodes.
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/sitepress/resources_node.rb', line 8 def name @name end |
#parent ⇒ Object (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 |
#children ⇒ Object
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 |
#formats ⇒ Object
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 |
#inspect ⇒ Object
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
47 48 49 |
# File 'lib/sitepress/resources_node.rb', line 47 def leaf? child_nodes.empty? end |
#parents ⇒ Object
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 |
#remove ⇒ Object
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
43 44 45 |
# File 'lib/sitepress/resources_node.rb', line 43 def root? parent.nil? end |
#siblings ⇒ Object
Returns sibling nodes.
28 29 30 |
# File 'lib/sitepress/resources_node.rb', line 28 def siblings parent ? parent.children.reject{ |c| c == self } : [] end |