class Sycamore::Absence

An Absence object represents the absence of a specific child {Sycamore::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 {Sycamore::Tree}. {Tree::QUERY_METHODS Query} and {Tree::DESTRUCTIVE_COMMAND_METHODS pure destructive command method} calls get delegated to {Sycamore::Nothing}, i.e. will behave like an empty Tree. On every other {Tree::COMMAND_METHODS 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.

Public Class Methods

at(parent_tree, parent_node)
Alias for: new
new(parent_tree, parent_node) click to toggle source

@api private

# File lib/sycamore/absence.rb, line 28
def initialize(parent_tree, parent_node)
  @parent_tree, @parent_node = parent_tree, parent_node
end
Also aliased as: at

Public Instance Methods

[](*path)
Alias for: child_at
__getobj__()
Alias for: presence
absent?() click to toggle source

(see Tree#absent?)

# File lib/sycamore/absence.rb, line 66
def absent?
  @tree.nil?
end
child_at(*path) click to toggle source
# 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
Also aliased as: [], dig
child_of(node) click to toggle source

query methods #

# 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() click to toggle source

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

@return [Tree]

@raise [TypeError] when this {Absence} is not resolved yet

# File lib/sycamore/absence.rb, line 138
def clone
  presence.clone
end
create() click to toggle source

@api private

# 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
dig(*path)
Alias for: child_at
dup() click to toggle source

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

@return [Tree]

@raise [TypeError] when this {Absence} is not resolved yet

# File lib/sycamore/absence.rb, line 127
def dup
  presence.dup
end
frozen?() click to toggle source

Checks if the absent tree is frozen.

@return [Boolean]

# File lib/sycamore/absence.rb, line 147
def frozen?
  if absent?
    false
  else
    presence.frozen?
  end
end
inspect() click to toggle source

A developer-friendly string representation of the absent tree.

@return [String]

# File lib/sycamore/absence.rb, line 116
def inspect
  "#{absent? ? 'absent' : 'present'} child of node #{@parent_node.inspect} in #{@parent_tree.inspect}"
end
nothing?() click to toggle source

(see Tree#nothing?)

# File lib/sycamore/absence.rb, line 73
def nothing?
  false
end
presence() click to toggle source

The tree object to which all method calls are delegated.

@api private

# File lib/sycamore/absence.rb, line 45
def presence
  @tree or Nothing
end
Also aliased as: __getobj__