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.



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

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

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#diffObject

Returns the value of attribute diff.



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

def diff
  @diff
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#add_child(child_tree) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 44

def add_child(child_tree)
  raise ArgumentError, "Expecting #{self.class}. Given: #{child_tree.class}" 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



59
60
61
62
63
64
65
66
67
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 59

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



51
52
53
54
55
56
57
# File 'lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb', line 51

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)


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

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.



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

def parent_id
  return nil unless parent
  parent.id
end

#parent_present?Boolean

Returns:

  • (Boolean)


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

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