Class: Sitepress::Node
- Inherits:
-
Object
- Object
- Sitepress::Node
- 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
-
#default_format ⇒ Object
readonly
Returns the value of attribute default_format.
-
#default_name ⇒ Object
readonly
Returns the value of attribute default_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Method Summary collapse
- #child(name) ⇒ Object
- #child?(name) ⇒ Boolean
-
#children ⇒ Object
Returns the immediate children nodes.
- #dig(*args) ⇒ Object
- #get(path) ⇒ Object
-
#initialize(parent: nil, name: nil, default_format: DEFAULT_FORMAT, default_name: DEFAULT_NAME) {|_self| ... } ⇒ Node
constructor
A new instance of Node.
- #inspect ⇒ Object
- #leaf? ⇒ Boolean
-
#parents ⇒ Object
Returns all parents up to the root node.
- #remove ⇒ Object
- #root? ⇒ Boolean
-
#siblings ⇒ Object
Returns sibling nodes and self.
Constructor Details
#initialize(parent: nil, name: nil, default_format: DEFAULT_FORMAT, default_name: DEFAULT_NAME) {|_self| ... } ⇒ Node
Returns a new instance of Node.
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_format ⇒ Object (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_name ⇒ Object (readonly)
Returns the value of attribute default_name.
11 12 13 |
# File 'lib/sitepress/node.rb', line 11 def default_name @default_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/sitepress/node.rb', line 11 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
11 12 13 |
# File 'lib/sitepress/node.rb', line 11 def parent @parent end |
#resources ⇒ Object (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
91 92 93 |
# File 'lib/sitepress/node.rb', line 91 def child?(name) @children.key? name end |
#children ⇒ Object
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 |
#inspect ⇒ Object
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
46 47 48 |
# File 'lib/sitepress/node.rb', line 46 def leaf? @children.empty? end |
#parents ⇒ Object
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 |
#remove ⇒ Object
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
42 43 44 |
# File 'lib/sitepress/node.rb', line 42 def root? parent.nil? end |
#siblings ⇒ Object
Returns sibling nodes and self.
33 34 35 |
# File 'lib/sitepress/node.rb', line 33 def siblings parent ? parent.children : [] end |