Class: Exa::TreeNode

Inherits:
Object
  • Object
show all
Defined in:
lib/exa/tree_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Returns the value of attribute links.



3
4
5
# File 'lib/exa/tree_node.rb', line 3

def links
  @links
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/exa/tree_node.rb', line 3

def name
  @name
end

#overlaysObject (readonly)

Returns the value of attribute overlays.



3
4
5
# File 'lib/exa/tree_node.rb', line 3

def overlays
  @overlays
end

#parentObject (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

#childrenObject



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

#constituentsObject (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.overlays.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

#deleteObject



107
108
109
# File 'lib/exa/tree_node.rb', line 107

def delete
  Deleter.new(self).perform!
end


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

#inspectObject



34
35
36
# File 'lib/exa/tree_node.rb', line 34

def inspect
  "<#{@name}>"
end


81
82
83
84
# File 'lib/exa/tree_node.rb', line 81

def link(source:)
  @links << source
  self
end

#pathObject



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

Returns:

  • (Boolean)


30
31
32
# File 'lib/exa/tree_node.rb', line 30

def symbolic?
  @symbolic
end

#symbolic_childrenObject (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 overlay.virtual?
    raise "Won't union mount virtual paths! (Try `link(source: ...)` instead.)"
  end

  @overlays << overlay
  self
end

#update(val) ⇒ Object



55
56
57
58
# File 'lib/exa/tree_node.rb', line 55

def update(val)
  @value = val
  self
end

#valueObject



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

Returns:

  • (Boolean)


26
27
28
# File 'lib/exa/tree_node.rb', line 26

def virtual?
  @virtual
end

#virtual_childrenObject (protected)



124
125
126
# File 'lib/exa/tree_node.rb', line 124

def virtual_children
  @overlays.flat_map(&:children)
end