Class: Exa::TreeNode
- Inherits:
-
Object
- Object
- Exa::TreeNode
- Defined in:
- lib/exa/tree_node.rb
Instance Attribute Summary collapse
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#overlays ⇒ Object
readonly
Returns the value of attribute overlays.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #children ⇒ Object
- #constituents ⇒ Object protected
- #copy(target) ⇒ Object
- #create_child(child_name:) ⇒ Object
- #delete ⇒ Object
- #dereference_symbolic_link ⇒ Object protected
-
#initialize(name:, value: nil, parent: nil, virtual: false, symbolic: false) ⇒ TreeNode
constructor
A new instance of TreeNode.
- #inspect ⇒ Object
- #link(source:) ⇒ Object
- #path ⇒ Object
- #query(path) ⇒ Object
- #recall(path) ⇒ Object (also: #[])
- #remove_child(child_name:) ⇒ Object
- #symbolic? ⇒ Boolean
- #symbolic_children ⇒ Object protected
- #unify(overlay:) ⇒ Object
- #update(val) ⇒ Object
- #value ⇒ Object
- #virtual? ⇒ Boolean
- #virtual_children ⇒ Object protected
Constructor Details
#initialize(name:, value: nil, parent: nil, virtual: false, symbolic: false) ⇒ TreeNode
Returns a new instance of TreeNode.
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/exa/tree_node.rb', line 4 def initialize(name:, value: nil, parent: nil, virtual: false, symbolic: false) p [ :tree_node, name: name ] @name = name @value = value @parent = parent @virtual = virtual @symbolic = symbolic @children = [] @overlays = [] @links = [] end |
Instance Attribute Details
#links ⇒ Object (readonly)
Returns the value of attribute links.
3 4 5 |
# File 'lib/exa/tree_node.rb', line 3 def links @links end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/exa/tree_node.rb', line 3 def name @name end |
#overlays ⇒ Object (readonly)
Returns the value of attribute overlays.
3 4 5 |
# File 'lib/exa/tree_node.rb', line 3 def @overlays end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
3 4 5 |
# File 'lib/exa/tree_node.rb', line 3 def parent @parent end |
Instance Method Details
#children ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/exa/tree_node.rb', line 86 def children if virtual? virtualize(constituents.flat_map(&:children)) else @children + symbolize(symbolic_children) + virtualize(virtual_children) end end |
#constituents ⇒ Object (protected)
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/exa/tree_node.rb', line 112 def constituents sources = if parent.virtual? parent.constituents.flat_map(&:children) else parent..flat_map(&:children) end sources.select do |candidate| candidate.name == @name end end |
#copy(target) ⇒ Object
103 104 105 |
# File 'lib/exa/tree_node.rb', line 103 def copy(target) Copier.new(self, target).perform! end |
#create_child(child_name:) ⇒ Object
60 61 62 63 64 |
# File 'lib/exa/tree_node.rb', line 60 def create_child(child_name:) child = TreeNode.new(name: child_name, parent: self) @children << child child end |
#delete ⇒ Object
107 108 109 |
# File 'lib/exa/tree_node.rb', line 107 def delete Deleter.new(self).perform! end |
#dereference_symbolic_link ⇒ Object (protected)
133 134 135 136 137 138 139 |
# File 'lib/exa/tree_node.rb', line 133 def dereference_symbolic_link # okay, we're symbolic... so our parents created us # and have a link parent.links.flat_map(&:children).detect do |linked_child| linked_child.name == @name end end |
#inspect ⇒ Object
34 35 36 |
# File 'lib/exa/tree_node.rb', line 34 def inspect "<#{@name}>" end |
#link(source:) ⇒ Object
81 82 83 84 |
# File 'lib/exa/tree_node.rb', line 81 def link(source:) @links << source self end |
#path ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/exa/tree_node.rb', line 38 def path if symbolic? dereference_symbolic_link.path else if @parent slash_name = "/#@name" if @parent.path == '/' slash_name else @parent.path + slash_name end else '/' #@name end end end |
#query(path) ⇒ Object
99 100 101 |
# File 'lib/exa/tree_node.rb', line 99 def query(path) Visitor.new(self).query(path) end |
#recall(path) ⇒ Object Also known as: []
94 95 96 |
# File 'lib/exa/tree_node.rb', line 94 def recall(path) Visitor.new(self).seek(path) end |
#remove_child(child_name:) ⇒ Object
66 67 68 69 70 |
# File 'lib/exa/tree_node.rb', line 66 def remove_child(child_name:) child = @children.detect { |c| c.name == child_name } @children.delete(child) self end |
#symbolic? ⇒ Boolean
30 31 32 |
# File 'lib/exa/tree_node.rb', line 30 def symbolic? @symbolic end |
#symbolic_children ⇒ Object (protected)
128 129 130 131 |
# File 'lib/exa/tree_node.rb', line 128 def symbolic_children # this is really a reference @links.flat_map(&:children) end |
#unify(overlay:) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/exa/tree_node.rb', line 72 def unify(overlay:) if .virtual? raise "Won't union mount virtual paths! (Try `link(source: ...)` instead.)" end @overlays << self end |
#update(val) ⇒ Object
55 56 57 58 |
# File 'lib/exa/tree_node.rb', line 55 def update(val) @value = val self end |
#value ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/exa/tree_node.rb', line 16 def value if virtual? constituents.first.value elsif symbolic? dereference_symbolic_link.value else @value end end |
#virtual? ⇒ Boolean
26 27 28 |
# File 'lib/exa/tree_node.rb', line 26 def virtual? @virtual end |
#virtual_children ⇒ Object (protected)
124 125 126 |
# File 'lib/exa/tree_node.rb', line 124 def virtual_children @overlays.flat_map(&:children) end |