Module: Shanty::Mixins::ActsAsLinkGraphNode
- Included in:
- Project
- Defined in:
- lib/shanty/mixins/acts_as_link_graph_node.rb
Overview
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.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(cls) ⇒ Object
The self.included idiom.
Instance Method Summary collapse
-
#add_child(node) ⇒ Object
Public: Add a node to the children linked to this instance.
-
#add_parent(node) ⇒ Object
Public: Add a node to the parents linked to this instance.
-
#all_children ⇒ Object
Public: Convenience method to return all of the children from this node in the tree downwards to the leaves of the tree.
-
#all_parents ⇒ Object
Public: Convenience method to return all of the parents from this node in the tree upwards to the root of the tree.
-
#children ⇒ Object
Public: The children linked to this instance.
-
#parents ⇒ Object
Public: The parents linked to this instance.
Class Method Details
.included(cls) ⇒ Object
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.
17 18 19 |
# File 'lib/shanty/mixins/acts_as_link_graph_node.rb', line 17 def self.included(cls) cls.extend(ClassMethods) end |
Instance Method Details
#add_child(node) ⇒ Object
Public: Add a node to the children linked to this instance.
node - The Object to add.
50 51 52 |
# File 'lib/shanty/mixins/acts_as_link_graph_node.rb', line 50 def add_child(node) children << node end |
#add_parent(node) ⇒ Object
Public: Add a node to the parents linked to this instance.
node - The Object to add.
43 44 45 |
# File 'lib/shanty/mixins/acts_as_link_graph_node.rb', line 43 def add_parent(node) parents << node end |
#all_children ⇒ Object
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.
58 59 60 |
# File 'lib/shanty/mixins/acts_as_link_graph_node.rb', line 58 def all_children children + children.map(&:all_children).flatten end |
#all_parents ⇒ Object
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.
66 67 68 |
# File 'lib/shanty/mixins/acts_as_link_graph_node.rb', line 66 def all_parents parents + parents.map(&:all_parents).flatten end |
#children ⇒ Object
Public: The children linked to this instance.
Returns an Array of Objects.
36 37 38 |
# File 'lib/shanty/mixins/acts_as_link_graph_node.rb', line 36 def children @children ||= [] end |
#parents ⇒ Object
Public: The parents linked to this instance.
Returns an Array of Objects.
29 30 31 |
# File 'lib/shanty/mixins/acts_as_link_graph_node.rb', line 29 def parents @parents ||= [] end |