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.



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.

Returns:

  • (Boolean)


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

#cloneTree

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

Returns:

Raises:



138
139
140
# File 'lib/sycamore/absence.rb', line 138

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.



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

#dupTree

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

Returns:

Raises:



127
128
129
# File 'lib/sycamore/absence.rb', line 127

def dup
  presence.dup
end

#frozen?Boolean

Checks if the absent tree is frozen.

Returns:

  • (Boolean)


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

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

#inspectString

A developer-friendly string representation of the absent tree.

Returns:

  • (String)


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.

Returns:

  • (Boolean)


73
74
75
# File 'lib/sycamore/absence.rb', line 73

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.



45
46
47
# File 'lib/sycamore/absence.rb', line 45

def presence
  @tree or Nothing
end