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

- (DanglingCursor) initialize(parent)

A new instance of DanglingCursor



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

def initialize(parent)
  @parent = parent
end

Instance Attribute Details

- (AbstractCursor) parent (readonly)

Returns:



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

def parent
  @parent
end

Instance Method Details

- (EditedCursor) delete

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:



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

def delete
  self
end

- (Integer) depth

Distance from the root node

Returns:



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

def depth
  @parent.depth + 1
end

- (AbstractCursor) first

Navigate to the first (leftmost) sibling node



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

def first
  self
end

- (Boolean) first?

Returns:

  • (Boolean)


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

def first?
  true
end

- (AbstractCursor) last

Navigate to the last (rightmost) sibling node



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

def last
  self
end

- (Boolean) last?

Returns:

  • (Boolean)


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

def last?
  true
end

- (Boolean) leaf?

Returns:

  • (Boolean)


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

def leaf?
  true
end

- (AbstractCursor) next

Navigate to the next (rightward) sibling node



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

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

- (#leaf?, ...) node

Returns:

  • (#leaf?, #children, #copy)
  • (#leaf?, #children, #copy)

Raises:



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

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

- (AbstractPath) path

Returns:



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

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

- (AbstractCursor) prev

Navigate to the previous (leftward) sibling node



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

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

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

Replace the current node with the given node



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

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

- (Boolean) root?

Returns:

  • (Boolean)


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

def root?
  false
end

- (AbstractCursor) up

Returns:



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

def up
  @parent
end