Class: BlastRadius::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/blast_radius/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @model_class = model_class
  @association_name = options[:association_name]
  @dependent_type = options[:dependent_type]
  @association_type = options[:association_type]
  @through = options[:through]
  @polymorphic = options[:polymorphic] || false
  @parent = options[:parent]
  @depth = options[:depth] || 0
  @children = []
end

Instance Attribute Details

#association_nameObject (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_typeObject (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

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/blast_radius/node.rb', line 5

def children
  @children
end

#dependent_typeObject (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

#depthObject (readonly)

Returns the value of attribute depth.



5
6
7
# File 'lib/blast_radius/node.rb', line 5

def depth
  @depth
end

#model_classObject (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

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/blast_radius/node.rb', line 5

def parent
  @parent
end

#polymorphicObject (readonly)

Returns the value of attribute polymorphic.



5
6
7
# File 'lib/blast_radius/node.rb', line 5

def polymorphic
  @polymorphic
end

#throughObject (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

#ancestorsArray<Node>

Get all ancestor nodes

Returns:

  • (Array<Node>)

    array of 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

Returns:

  • (Boolean)

    true if circular reference detected



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

Returns:

  • (Boolean)

    true if leaf node



53
54
55
# File 'lib/blast_radius/node.rb', line 53

def leaf?
  @children.empty?
end

#model_nameString

Get model name as string

Returns:

  • (String)

    model class name



41
42
43
# File 'lib/blast_radius/node.rb', line 41

def model_name
  @model_class.name
end

#pathArray<String>

Get the path from root to this node

Returns:

  • (Array<String>)

    array of model names



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

Returns:

  • (Boolean)

    true if root node



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

def root?
  @parent.nil?
end