Class: Eco::Data::Locations::NodeDiff::NodesDiff::DiffsTree

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb

Overview

Helper class to wrap into a tree a set of diffs

Defined Under Namespace

Classes: CyclicAncestorsChain, CyclicHierarchy

Constant Summary collapse

MAX_ITERATIONS =
16

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diff = nil, id:) ⇒ DiffsTree

Returns a new instance of DiffsTree.



16
17
18
19
20
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 16

def initialize(diff = nil, id:)
  @diff      = diff
  @id        = id
  @children  = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



14
15
16
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 14

def children
  @children
end

#diffObject

Returns the value of attribute diff.



13
14
15
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 13

def diff
  @diff
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 12

def id
  @id
end

#parentObject (readonly)

Returns the value of attribute parent.



14
15
16
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 14

def parent
  @parent
end

Instance Method Details

#add_child(child_tree) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 46

def add_child(child_tree)
  msg = "Expecting #{self.class}. Given: #{child_tree.class}"
  raise ArgumentError, msg unless child_tree.is_a?(self.class)

  prevent_cyclic_chain(child_tree)
  children.push(child_tree)
  child_tree.link_parent(self)
end

#all_descendants(iteration: 0) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 63

def all_descendants(iteration: 0)
  msg = "Node '#{id}' can't be the #{iteration}th offspring of any other node."
  raise CyclicAncestorsChain, msg if max_iterations?(iteration)

  children.each_with_object([]) do |child, out|
    out.push(child)
    out.concat(child.all_descendants(iteration: iteration + 1))
  end
end

#ancestors(iteration: 0) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 55

def ancestors(iteration: 0)
  msg = "Node '#{id}' can't be the #{iteration}th ancestor of any other node."
  raise CyclicAncestorsChain, msg if max_iterations?(iteration)

  return [] unless parent
  [parent].concat(parent.ancestors(iteration: iteration + 1))
end

#diff?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 28

def diff?
  !!diff
end

#parent_idObject

Note:

that the abscense of parent_id (i.e. nil) does NOT mean that this node does not have an actual parent. This class supports building partial trees (clusters), where some parents may not have presence.



40
41
42
43
44
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 40

def parent_id
  return unless parent

  parent.id
end

#parent_present?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 32

def parent_present?
  !!parent && parent.diff?
end