Class: Sycamore::Absence
- Inherits:
-
Delegator
- Object
- Delegator
- Sycamore::Absence
- Defined in:
- lib/sycamore/absence.rb
Overview
An Absence object represents the absence of a specific child Tree.
Absence
instances get created when accessing non-existent children of a Tree with Tree#child_of or Tree#child_at. It is not intended to be instantiated by the user.
An Absence
object can be used like a normal Tree. Query and pure destructive command method calls get delegated to Nothing, i.e. will behave like an empty Tree. On every other command calls, the Absence
object gets resolved, which means the missing tree will be created, added to the parent tree and the method call gets delegated to the now existing tree. After the Absence
object is resolved all subsequent method calls are delegated to the created tree. The type of tree eventually created is determined by the Tree#new_child implementation of the parent tree and the parent node.
Instance Method Summary collapse
- #absent? ⇒ Boolean
- #child_at(*path) ⇒ Object (also: #[], #dig)
-
#child_of(node) ⇒ Object
query methods #.
-
#clone ⇒ Tree
Clones the resolved tree or raises an error, when unresolved.
- #create ⇒ Object private
-
#dup ⇒ Tree
Duplicates the resolved tree or raises an error, when unresolved.
-
#frozen? ⇒ Boolean
Checks if the absent tree is frozen.
-
#initialize(parent_tree, parent_node) ⇒ Absence
constructor
private
A new instance of Absence.
-
#inspect ⇒ String
A developer-friendly string representation of the absent tree.
-
#nothing? ⇒ Boolean
Checks if this is the Nothing tree.
-
#presence ⇒ Object
(also: #__getobj__)
private
The tree object to which all method calls are delegated.
Constructor Details
#initialize(parent_tree, parent_node) ⇒ Absence
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Absence.
28 29 30 |
# File 'lib/sycamore/absence.rb', line 28 def initialize(parent_tree, parent_node) @parent_tree, @parent_node = parent_tree, parent_node end |
Instance Method Details
#absent? ⇒ Boolean
Checks if this is an unresolved Sycamore::Absence or Nothing.
66 67 68 |
# File 'lib/sycamore/absence.rb', line 66 def absent? @tree.nil? end |
#child_at(*path) ⇒ Object Also known as: [], dig
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/sycamore/absence.rb', line 95 def child_at(*path) if absent? # TODO: This is duplication of Tree#child_at! How can we remove it, without introducing a module for this single method or inherit from Tree? case path.length when 0 then raise ArgumentError, 'wrong number of arguments (given 0, expected 1+)' when 1 then child_of(*path) else child_of(path[0]).child_at(*path[1..-1]) end else presence.child_at(*path) end end |
#child_of(node) ⇒ Object
query methods #
85 86 87 88 89 90 91 92 93 |
# File 'lib/sycamore/absence.rb', line 85 def child_of(node) if absent? raise InvalidNode, "#{node} is not a valid tree node" if node.is_a? Enumerable Absence.at(self, node) else presence.child_of(node) end end |
#clone ⇒ Tree
Clones the resolved tree or raises an error, when unresolved.
138 139 140 |
# File 'lib/sycamore/absence.rb', line 138 def clone presence.clone end |
#create ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
54 55 56 57 |
# File 'lib/sycamore/absence.rb', line 54 def create @parent_tree = @parent_tree.add_node_with_empty_child(@parent_node) @tree = @parent_tree[@parent_node] end |
#dup ⇒ Tree
Duplicates the resolved tree or raises an error, when unresolved.
127 128 129 |
# File 'lib/sycamore/absence.rb', line 127 def dup presence.dup end |
#frozen? ⇒ Boolean
Checks if the absent tree is frozen.
147 148 149 150 151 152 153 |
# File 'lib/sycamore/absence.rb', line 147 def frozen? if absent? false else presence.frozen? end end |
#inspect ⇒ String
A developer-friendly string representation of the absent tree.
116 117 118 |
# File 'lib/sycamore/absence.rb', line 116 def inspect "#{absent? ? 'absent' : 'present'} child of node #{@parent_node.inspect} in #{@parent_tree.inspect}" end |
#nothing? ⇒ Boolean
Checks if this is the Nothing tree.
73 74 75 |
# File 'lib/sycamore/absence.rb', line 73 def nothing? false end |
#presence ⇒ Object Also known as: __getobj__
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The tree object to which all method calls are delegated.
45 46 47 |
# File 'lib/sycamore/absence.rb', line 45 def presence @tree or Nothing end |