Class: Sycamore::Absence

Inherits:
Delegator
  • Object
show all
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

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.



26
27
28
# File 'lib/sycamore/absence.rb', line 26

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.

Returns:

  • (Boolean)


64
65
66
# File 'lib/sycamore/absence.rb', line 64

def absent?
  @tree.nil?
end

#child_at(*path) ⇒ Object Also known as: [], dig



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sycamore/absence.rb', line 93

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..])
    end
  else
    presence.child_at(*path)
  end
end

#child_of(node) ⇒ Object

query methods #



83
84
85
86
87
88
89
90
91
# File 'lib/sycamore/absence.rb', line 83

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

#cloneTree

Clones the resolved tree or raises an error, when unresolved.

Returns:

Raises:



136
137
138
# File 'lib/sycamore/absence.rb', line 136

def clone
  presence.clone
end

#createObject

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.



52
53
54
55
# File 'lib/sycamore/absence.rb', line 52

def create
  @parent_tree = @parent_tree.add_node_with_empty_child(@parent_node)
  @tree = @parent_tree[@parent_node]
end

#dupTree

Duplicates the resolved tree or raises an error, when unresolved.

Returns:

Raises:



125
126
127
# File 'lib/sycamore/absence.rb', line 125

def dup
  presence.dup
end

#frozen?Boolean

Checks if the absent tree is frozen.

Returns:

  • (Boolean)


145
146
147
148
149
150
151
# File 'lib/sycamore/absence.rb', line 145

def frozen?
  if absent?
    false
  else
    presence.frozen?
  end
end

#inspectString

A developer-friendly string representation of the absent tree.

Returns:

  • (String)


114
115
116
# File 'lib/sycamore/absence.rb', line 114

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.

Returns:

  • (Boolean)


71
72
73
# File 'lib/sycamore/absence.rb', line 71

def nothing?
  false
end

#presenceObject 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.



43
44
45
# File 'lib/sycamore/absence.rb', line 43

def presence
  @tree or Nothing
end