Module: Gamefic::Node
- Included in:
- Entity
- Defined in:
- lib/gamefic/node.rb
Instance Method Summary collapse
Instance Method Details
#children ⇒ Array
10 11 12 13 |
# File 'lib/gamefic/node.rb', line 10 def children @children ||= [] @children.clone end |
#flatten ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/gamefic/node.rb', line 14 def flatten array = Array.new children.each { |child| array = array + recurse_flatten(child) } return array end |
#parent ⇒ Object
21 22 23 |
# File 'lib/gamefic/node.rb', line 21 def parent @parent end |
#parent=(node) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/gamefic/node.rb', line 24 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 |