Class: BlastRadius::Node
- Inherits:
-
Object
- Object
- BlastRadius::Node
- Defined in:
- lib/blast_radius/node.rb
Instance Attribute Summary collapse
-
#association_name ⇒ Object
readonly
Returns the value of attribute association_name.
-
#association_type ⇒ Object
readonly
Returns the value of attribute association_type.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#dependent_type ⇒ Object
readonly
Returns the value of attribute dependent_type.
-
#depth ⇒ Object
readonly
Returns the value of attribute depth.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#polymorphic ⇒ Object
readonly
Returns the value of attribute polymorphic.
-
#through ⇒ Object
readonly
Returns the value of attribute through.
Instance Method Summary collapse
- #add_child(child_node) ⇒ Object
-
#ancestors ⇒ Array<Node>
Get all ancestor nodes.
-
#circular_reference? ⇒ Boolean
Check if this node creates a circular reference.
-
#initialize(model_class, options = {}) ⇒ Node
constructor
A new instance of Node.
-
#leaf? ⇒ Boolean
Check if this is a leaf node.
-
#model_name ⇒ String
Get model name as string.
-
#path ⇒ Array<String>
Get the path from root to this node.
-
#root? ⇒ Boolean
Check if this is the root node.
Constructor Details
#initialize(model_class, options = {}) ⇒ Node
Returns a new instance of Node.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/blast_radius/node.rb', line 9 def initialize(model_class, = {}) @model_class = model_class @association_name = [:association_name] @dependent_type = [:dependent_type] @association_type = [:association_type] @through = [:through] @polymorphic = [:polymorphic] || false @parent = [:parent] @depth = [:depth] || 0 @children = [] end |
Instance Attribute Details
#association_name ⇒ Object (readonly)
Returns the value of attribute association_name.
5 6 7 |
# File 'lib/blast_radius/node.rb', line 5 def association_name @association_name end |
#association_type ⇒ Object (readonly)
Returns the value of attribute association_type.
5 6 7 |
# File 'lib/blast_radius/node.rb', line 5 def association_type @association_type end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
5 6 7 |
# File 'lib/blast_radius/node.rb', line 5 def children @children end |
#dependent_type ⇒ Object (readonly)
Returns the value of attribute dependent_type.
5 6 7 |
# File 'lib/blast_radius/node.rb', line 5 def dependent_type @dependent_type end |
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
5 6 7 |
# File 'lib/blast_radius/node.rb', line 5 def depth @depth end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
5 6 7 |
# File 'lib/blast_radius/node.rb', line 5 def model_class @model_class end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
5 6 7 |
# File 'lib/blast_radius/node.rb', line 5 def parent @parent end |
#polymorphic ⇒ Object (readonly)
Returns the value of attribute polymorphic.
5 6 7 |
# File 'lib/blast_radius/node.rb', line 5 def polymorphic @polymorphic end |
#through ⇒ Object (readonly)
Returns the value of attribute through.
5 6 7 |
# File 'lib/blast_radius/node.rb', line 5 def through @through end |
Instance Method Details
#add_child(child_node) ⇒ Object
21 22 23 |
# File 'lib/blast_radius/node.rb', line 21 def add_child(child_node) @children << child_node end |
#ancestors ⇒ Array<Node>
Get all ancestor nodes
33 34 35 36 37 |
# File 'lib/blast_radius/node.rb', line 33 def ancestors return [] unless @parent [@parent] + @parent.ancestors end |
#circular_reference? ⇒ Boolean
Check if this node creates a circular reference
27 28 29 |
# File 'lib/blast_radius/node.rb', line 27 def circular_reference? ancestors.any? { |ancestor| ancestor.model_class == @model_class } end |
#leaf? ⇒ Boolean
Check if this is a leaf node
53 54 55 |
# File 'lib/blast_radius/node.rb', line 53 def leaf? @children.empty? end |
#model_name ⇒ String
Get model name as string
41 42 43 |
# File 'lib/blast_radius/node.rb', line 41 def model_name @model_class.name end |
#path ⇒ Array<String>
Get the path from root to this node
59 60 61 62 63 |
# File 'lib/blast_radius/node.rb', line 59 def path return [model_name] if root? @parent.path + [model_name] end |
#root? ⇒ Boolean
Check if this is the root node
47 48 49 |
# File 'lib/blast_radius/node.rb', line 47 def root? @parent.nil? end |