Class: Stupidedi::Zipper::DanglingCursor

Inherits:
AbstractCursor show all
Defined in:
lib/stupidedi/zipper/dangling_cursor.rb

Instance Attribute Summary collapse

Querying the Tree Location collapse

Traversing the Tree collapse

Editing the Tree collapse

Instance Method Summary collapse

Methods inherited from AbstractCursor

#append_child, #between, #child, #children, #dangle, #descendant, #down, #flatten, #insert_left, #insert_right, #prepend_child, #root

Constructor Details

#initialize(parent) ⇒ DanglingCursor

Returns a new instance of DanglingCursor.


10
11
12
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 10

def initialize(parent)
  @parent = parent
end

Instance Attribute Details

#parentAbstractCursor (readonly)

Returns:


8
9
10
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 8

def parent
  @parent
end

Instance Method Details

#deleteEditedCursor

Remove the current node, and navigate to the next (rightward) node if one exists. Otherwise, navigate to the previous (leftward) node if one exists. Otherwise, create a placeholder where the next sibling node will be created.

Returns:


95
96
97
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 95

def delete
  self
end

#depthInteger

Distance from the root node

Returns:

  • (Integer)

39
40
41
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 39

def depth
  @parent.depth + 1
end

#firstAbstractCursor

Navigate to the first (leftmost) sibling node

Returns:


74
75
76
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 74

def first
  self
end

#first?Boolean

True when the node has no leftward siblings

Returns:

  • (Boolean)

44
45
46
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 44

def first?
  true
end

#lastAbstractCursor

Navigate to the last (rightmost) sibling node

Returns:


79
80
81
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 79

def last
  self
end

#last?Boolean

True when the node has no rightward siblings

Returns:

  • (Boolean)

49
50
51
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 49

def last?
  true
end

#leaf?Boolean

True if the node has no children

Returns:

  • (Boolean)

29
30
31
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 29

def leaf?
  true
end

#nextAbstractCursor

Navigate to the next (rightward) sibling node


62
63
64
65
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 62

def next
  raise Exceptions::ZipperError,
    "cannot move to next after last node"
end

#node#leaf?, ...

Returns:

Raises:


15
16
17
18
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 15

def node
  raise Exceptions::ZipperError,
    "DanglingCursor#node should not be called"
end

#pathAbstractPath

Returns:


21
22
23
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 21

def path
  Hole.new([], @parent.path.parent, [])
end

#prevAbstractCursor

Navigate to the previous (leftward) sibling node


68
69
70
71
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 68

def prev
  raise Exceptions::ZipperError,
    "cannot move to prev before first node"
end

#replace(node) ⇒ AbstractCursor Also known as: prepend, append

Replace the current node with the given node

Returns:


87
88
89
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 87

def replace(node)
  @parent.append_child(node)
end

#root?Boolean

True if the node has no parent

Returns:

  • (Boolean)

34
35
36
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 34

def root?
  false
end

#upAbstractCursor

Returns:


57
58
59
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 57

def up
  @parent
end