Class: Stupidedi::Zipper::StackCursor

Inherits:
AbstractCursor show all
Defined in:
lib/stupidedi/zipper/stack_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

#between, #child, #children, #delete, #depth, #descendant, #down, #first, #first?, #flatten, #insert_left, #insert_right, #last?, #next, #prepend_child, #prev, #root

Constructor Details

#initialize(node, path, parent) ⇒ StackCursor

Returns a new instance of StackCursor.



17
18
19
20
# File 'lib/stupidedi/zipper/stack_cursor.rb', line 17

def initialize(node, path, parent)
  @node, @path, @parent =
    node, path, parent
end

Instance Attribute Details

#node#leaf?, ... (readonly)

Returns:



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

def node
  @node
end

#pathHole (readonly)

Returns:



11
12
13
# File 'lib/stupidedi/zipper/stack_cursor.rb', line 11

def path
  @path
end

Instance Method Details

#append(node) ⇒ EditedCursor, void

Insert a new sibling node after (to the right of) the current node, and navigate to the new sibling node

Returns:



58
59
60
61
62
63
64
65
# File 'lib/stupidedi/zipper/stack_cursor.rb', line 58

def append(node)
  if root?
    raise Exceptions::ZipperError,
      "root node has no siblings"
  end

  replace(node)
end

#append_child(child)



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

def append_child(child)
  StackCursor.new(child, Hole.new([], @path, []), self)
end

#dangle



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/stupidedi/zipper/stack_cursor.rb', line 88

def dangle
  if leaf?
    StackCursor.new(nil, Hole.new([], @path, []), self)
  else
    head, *tail = @node.children

    unless tail.empty?
      raise Exceptions::ZipperError,
        "stack cursor doesn't support nodes with multiple children"
    end

    StackCursor.new(head, Hole.new([], @path, []), self)
  end
end

#lastAbstractCursor

Navigate to the last (rightmost) sibling node

Returns:



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

def last
  self
end

#leaf?

True if the node has no children



26
27
28
# File 'lib/stupidedi/zipper/stack_cursor.rb', line 26

def leaf?
  @node.leaf? or @node.children.empty?
end

#prepend(node) ⇒ EditedCursor, void

Insert a new sibling node before (to the left of) the current node, and navigate to the new sibling node

Returns:



69
70
71
72
73
74
75
76
# File 'lib/stupidedi/zipper/stack_cursor.rb', line 69

def prepend(node)
  if root?
    raise Exceptions::ZipperError,
      "root node has no siblings"
  end

  replace(node)
end

#replace(node) ⇒ AbstractCursor, RootCursor

Replace the current node with the given node

Returns:



84
85
86
# File 'lib/stupidedi/zipper/stack_cursor.rb', line 84

def replace(node)
  StackCursor.new(node, @path, @parent)
end

#root?

True if the node has no parent



31
32
33
# File 'lib/stupidedi/zipper/stack_cursor.rb', line 31

def root?
  @path.root?
end

#upAbstractCursor

Navigate to the parent node

Returns:



44
45
46
47
48
49
50
51
# File 'lib/stupidedi/zipper/stack_cursor.rb', line 44

def up
  if root?
    raise Exceptions::ZipperError,
      "root node has no siblings"
  end

  @parent
end