Module: Gamefic::Node
- Included in:
- Entity
- Defined in:
- lib/gamefic/node.rb
Overview
Parent/child relationships for objects.
Instance Attribute Summary collapse
-
#parent ⇒ Node?
The object’s parent.
Instance Method Summary collapse
-
#accessible ⇒ Array<Entity>
Get an array of children that are accessible to external entities.
-
#adjacent?(other) ⇒ Boolean
True if this node and the other node have the same parent.
-
#children ⇒ Array<Node>
An array of the object’s children.
-
#flatten ⇒ Array<Node>
Get a flat array of all descendants.
-
#include?(other) ⇒ Boolean
True if this node is the other’s parent.
- #put(parent, relation = nil) ⇒ Object (also: #place)
-
#relation ⇒ Symbol?
The node’s relation to its parent.
- #relation=(symbol) ⇒ Object
-
#take(*children, relation: nil) ⇒ Array<Node>
Add children to the node.
Instance Attribute Details
#parent ⇒ Node?
The object’s parent.
16 17 18 |
# File 'lib/gamefic/node.rb', line 16 def parent @parent end |
Instance Method Details
#accessible ⇒ Array<Entity>
Get an array of children that are accessible to external entities.
A child is considered accessible if external entities can interact with it. For Example, an author can designate that the contents of a bowl are accessible, while the contents of a locked safe are not. All of an entity’s children are accessible by default. Authors should override this method if they need custom behavior.
88 89 90 |
# File 'lib/gamefic/node.rb', line 88 def accessible children end |
#adjacent?(other) ⇒ Boolean
True if this node and the other node have the same parent.
102 103 104 |
# File 'lib/gamefic/node.rb', line 102 def adjacent?(other) other.parent == parent end |
#children ⇒ Array<Node>
An array of the object’s children.
21 22 23 |
# File 'lib/gamefic/node.rb', line 21 def children child_set.to_a.freeze end |
#flatten ⇒ Array<Node>
Get a flat array of all descendants.
28 29 30 |
# File 'lib/gamefic/node.rb', line 28 def flatten children.flat_map { |child| [child] + child.flatten } end |
#include?(other) ⇒ Boolean
True if this node is the other’s parent.
95 96 97 |
# File 'lib/gamefic/node.rb', line 95 def include?(other) other.parent == self end |
#put(parent, relation = nil) ⇒ Object Also known as: place
73 74 75 76 |
# File 'lib/gamefic/node.rb', line 73 def put(parent, relation = nil) self.parent = parent @relation = relation end |
#relation ⇒ Symbol?
The node’s relation to its parent.
The inherently supported relations are ‘:in` and `:on`, but authors are free to define their own.
52 53 54 |
# File 'lib/gamefic/node.rb', line 52 def relation @relation ||= (parent ? :in : nil) end |
#relation=(symbol) ⇒ Object
57 58 59 60 61 |
# File 'lib/gamefic/node.rb', line 57 def relation=(symbol) raise NodeError, "Invalid relation #{symbol.inspect} on #{inspect} without parent" unless parent || !symbol @relation = symbol end |