Module: Dagnabit::Node::Neighbors

Defined in:
lib/dagnabit/node/neighbors.rb

Overview

Instance methods for finding out the neighbors of a node.

These methods do not behave like association proxies: they’re just wrappers around ActiveRecord::Base#find. Therefore, they do not cache, do not support calculations, do not support extension modules, named scopes, etc.

These methods aren’t association proxies because a link’s ancestor and descendant are polymorphic associations, and ActiveRecord does not support polymorphic has_many :through associations.

Instance Method Summary collapse

Instance Method Details

#ancestorsObject

Find the ancestors (predecessors) of this node.



47
48
49
# File 'lib/dagnabit/node/neighbors.rb', line 47

def ancestors
  links_as_descendant.find(:all, :include => :ancestor).map { |l| l.ancestor }
end

#ancestors_of_type(type) ⇒ Object

Find the ancestors (predecessors) of this node satisfying a given type.



54
55
56
# File 'lib/dagnabit/node/neighbors.rb', line 54

def ancestors_of_type(type)
  links_as_descendant.ancestor_type(type).find(:all, :include => :ancestor).map { |l| l.ancestor }
end

#childrenObject

Finds the children (immediate successors) of this node.



33
34
35
# File 'lib/dagnabit/node/neighbors.rb', line 33

def children
  links_as_parent.find(:all, :include => :descendant).map { |l| l.descendant }
end

#children_of_type(type) ⇒ Object

Finds the children (immediate successors) of this node satisfying a given type.



40
41
42
# File 'lib/dagnabit/node/neighbors.rb', line 40

def children_of_type(type)
  links_as_parent.descendant_type(type).find(:all, :include => :descendant).map { |l| l.descendant }
end

#descendantsObject

Finds the descendants (successors) of this node.



61
62
63
# File 'lib/dagnabit/node/neighbors.rb', line 61

def descendants
  links_as_ancestor.find(:all, :include => :descendant).map { |l| l.descendant }
end

#descendants_of_type(type) ⇒ Object

Finds the descendants (successors) of this node satisfying a given type.



68
69
70
# File 'lib/dagnabit/node/neighbors.rb', line 68

def descendants_of_type(type)
  links_as_ancestor.descendant_type(type).find(:all, :include => :descendant).map { |l| l.descendant }
end

#parentsObject

Finds the parents (immediate predecessors) of this node.



19
20
21
# File 'lib/dagnabit/node/neighbors.rb', line 19

def parents
  links_as_child.find(:all, :include => :ancestor).map { |l| l.ancestor }
end

#parents_of_type(type) ⇒ Object

Finds the parents (immediate predecessors) of this node satisfying a given type.



26
27
28
# File 'lib/dagnabit/node/neighbors.rb', line 26

def parents_of_type(type)
  links_as_child.ancestor_type(type).find(:all, :include => :ancestor).map { |l| l.ancestor }
end