module Shanty::Mixins::ActsAsLinkGraphNode

A mixin module enabling classes to have parents and children. It provides convenience methods for determining dependencies, depdenants, and a distance from the root node. Note that in contrast to a tree, this link graph module allows multiple parents.

Public Class Methods

included(cls) click to toggle source

The self.included idiom. This is described in great detail in a fantastic blog post here:

www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/

Basically, this idiom allows us to add both instance and class methods to the class that is mixing this module into itself without forcing them to call extend and include for this mixin. You’ll see this idiom everywhere in the Ruby/Rails world, so we use it too.

# File lib/shanty/mixins/acts_as_link_graph_node.rb, line 17
def self.included(cls)
  cls.extend(ClassMethods)
end

Public Instance Methods

add_child(node) click to toggle source

Public: Add a node to the children linked to this instance.

node - The Object to add.

# File lib/shanty/mixins/acts_as_link_graph_node.rb, line 50
def add_child(node)
  children << node
end
add_parent(node) click to toggle source

Public: Add a node to the parents linked to this instance.

node - The Object to add.

# File lib/shanty/mixins/acts_as_link_graph_node.rb, line 43
def add_parent(node)
  parents << node
end
all_children() click to toggle source

Public: Convenience method to return all of the children from this node in the tree downwards to the leaves of the tree.

Returns an Array of child Objects.

# File lib/shanty/mixins/acts_as_link_graph_node.rb, line 58
def all_children
  children + children.map(&:all_children).flatten
end
all_parents() click to toggle source

Public: Convenience method to return all of the parents from this node in the tree upwards to the root of the tree.

Returns an Array of parent Objects.

# File lib/shanty/mixins/acts_as_link_graph_node.rb, line 66
def all_parents
  parents + parents.map(&:all_parents).flatten
end
children() click to toggle source

Public: The children linked to this instance.

Returns an Array of Objects.

# File lib/shanty/mixins/acts_as_link_graph_node.rb, line 36
def children
  @children ||= []
end
parents() click to toggle source

Public: The parents linked to this instance.

Returns an Array of Objects.

# File lib/shanty/mixins/acts_as_link_graph_node.rb, line 29
def parents
  @parents ||= []
end