Class: CycleFinder::Path

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/cycle_finder.rb

Overview

A path is a node plus the sequence of ancestor nodes we followed to get there. A path contains a cycle if the current node also appears in the sequence of ancestors (meaning we doubled back)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, ancestors = []) ⇒ Path

Returns a new instance of Path.



87
88
89
90
# File 'backend/app/lib/cycle_finder.rb', line 87

def initialize(node, ancestors = [])
  @node = node
  @ancestors = ancestors
end

Instance Attribute Details

#ancestorsObject (readonly)

Returns the value of attribute ancestors



85
86
87
# File 'backend/app/lib/cycle_finder.rb', line 85

def ancestors
  @ancestors
end

#nodeObject (readonly)

Returns the value of attribute node



85
86
87
# File 'backend/app/lib/cycle_finder.rb', line 85

def node
  @node
end

Instance Method Details

#contains_cycle?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'backend/app/lib/cycle_finder.rb', line 92

def contains_cycle?
  ancestors.include?(node)
end

#next_node(new_node) ⇒ Object



96
97
98
# File 'backend/app/lib/cycle_finder.rb', line 96

def next_node(new_node)
  self.class.new(new_node, ancestors + [node])
end