Module: Gamefic::Node

Included in:
Entity
Defined in:
lib/gamefic/node.rb

Instance Method Summary collapse

Instance Method Details

#accessible?Boolean



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

def accessible?
  true
end

#childrenArray



10
11
12
13
# File 'lib/gamefic/node.rb', line 10

def children
  @children ||= []
  @children.clone
end

#flattenArray



16
17
18
19
20
21
22
# File 'lib/gamefic/node.rb', line 16

def flatten
  array = Array.new
  children.each { |child|
    array = array + recurse_flatten(child)
  }
  return array
end

#parentObject



25
26
27
# File 'lib/gamefic/node.rb', line 25

def parent
  @parent
end

#parent=(node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gamefic/node.rb', line 29

def parent=(node)
  return if node == @parent 
  if node == self
    raise CircularNodeReferenceError.new("Node cannot be its own parent")
  end
  # Do not permit circular references

  if node != nil and node.parent == self
    node.parent = nil
  end
  if node != nil and flatten.include?(node)
    raise CircularNodeReferenceError.new("Node cannot be a child of a descendant")
  end
  if @parent != node
    if @parent != nil
      @parent.send(:rem_child, self)
    end
    @parent = node
    if @parent != nil
      @parent.send(:add_child, self)
    end
  end
end