module Gamefic::Node
Parent/child relationships for objects.
Attributes
parent[R]
The object’s parent.
@return [Node, nil]
Public Instance Methods
accessible?()
click to toggle source
Determine if external objects can interact with this object’s children. For example, a game can designate that the contents of a bowl are accessible, while the contents of a locked safe are not.
@return [Boolean]
# File lib/gamefic/node.rb, line 59 def accessible? true end
adjacent?(other)
click to toggle source
# File lib/gamefic/node.rb, line 70 def adjacent?(other) other.parent == parent end
children()
click to toggle source
An array of the object’s children.
@return [Array<Node>]
# File lib/gamefic/node.rb, line 21 def children child_set.to_a.freeze end
flatten()
click to toggle source
Get a flat array of all descendants.
@return [Array<Node>]
# File lib/gamefic/node.rb, line 28 def flatten children.flat_map { |child| [child] + child.flatten } end
include?(other)
click to toggle source
True if this node is the other’s parent.
@param other [Node]
# File lib/gamefic/node.rb, line 66 def include?(other) other.parent == self end
parent=(node)
click to toggle source
Set the object’s parent.
@param node [Node, nil]
# File lib/gamefic/node.rb, line 35 def parent=(node) return if node == parent validate_parent node parent&.rem_child self @parent = node parent&.add_child self end
take(*children)
click to toggle source
Add children to the node. Return all the node’s children.
@param children [Array<Node, Array
<Node>>] @return [Array<Node>]
# File lib/gamefic/node.rb, line 49 def take *children children.flatten.each { |child| child.parent = self } children end
Protected Instance Methods
add_child(node)
click to toggle source
# File lib/gamefic/node.rb, line 76 def add_child(node) child_set.add node end
rem_child(node)
click to toggle source
# File lib/gamefic/node.rb, line 80 def rem_child(node) child_set.delete node end
Private Instance Methods
child_set()
click to toggle source
# File lib/gamefic/node.rb, line 86 def child_set @child_set ||= Set.new end
validate_parent(node)
click to toggle source
# File lib/gamefic/node.rb, line 90 def validate_parent(node) raise NodeError, "Parent of #{inspect} must be a Node, received #{node.inspect}" unless node.is_a?(Node) || node.nil? raise NodeError, "#{inspect} cannot be its own parent" if node == self raise NodeError, "#{inspect} cannot be a child of descendant #{node.inspect}" if flatten.include?(node) end