Class: Stupidedi::Zipper::DanglingCursor
- Inherits:
-
AbstractCursor
- Object
- AbstractCursor
- Stupidedi::Zipper::DanglingCursor
- Defined in:
- lib/stupidedi/zipper/dangling_cursor.rb
Instance Attribute Summary collapse
- #parent ⇒ AbstractCursor readonly
Querying the Tree Location collapse
-
#depth ⇒ Integer
Distance from the root node.
- #first? ⇒ Boolean
- #last? ⇒ Boolean
- #leaf? ⇒ Boolean
- #root? ⇒ Boolean
Traversing the Tree collapse
-
#first ⇒ AbstractCursor
Navigate to the first (leftmost) sibling node.
-
#last ⇒ AbstractCursor
Navigate to the last (rightmost) sibling node.
-
#next ⇒ AbstractCursor
Navigate to the next (rightward) sibling node.
-
#prev ⇒ AbstractCursor
Navigate to the previous (leftward) sibling node.
- #up ⇒ AbstractCursor
Editing the Tree collapse
-
#delete ⇒ EditedCursor
Remove the current node, and navigate to the next (rightward) node if one exists.
-
#replace(node) ⇒ AbstractCursor
(also: #prepend, #append)
Replace the current node with the given node.
Instance Method Summary collapse
-
#initialize(parent) ⇒ DanglingCursor
constructor
A new instance of DanglingCursor.
- #node ⇒ #leaf?, ...
- #path ⇒ AbstractPath
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.
9 10 11 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 9 def initialize(parent) @parent = parent end |
Instance Attribute Details
#parent ⇒ AbstractCursor (readonly)
7 8 9 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 7 def parent @parent end |
Instance Method Details
#delete ⇒ EditedCursor
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.
94 95 96 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 94 def delete self end |
#depth ⇒ Integer
Distance from the root node
38 39 40 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 38 def depth @parent.depth + 1 end |
#first ⇒ AbstractCursor
Navigate to the first (leftmost) sibling node
73 74 75 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 73 def first self end |
#first? ⇒ Boolean
43 44 45 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 43 def first? true end |
#last ⇒ AbstractCursor
Navigate to the last (rightmost) sibling node
78 79 80 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 78 def last self end |
#last? ⇒ Boolean
48 49 50 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 48 def last? true end |
#leaf? ⇒ Boolean
28 29 30 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 28 def leaf? true end |
#next ⇒ AbstractCursor
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 |
#node ⇒ #leaf?, ...
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 |
#path ⇒ AbstractPath
20 21 22 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 20 def path Hole.new([], @parent.path.parent, []) end |
#prev ⇒ AbstractCursor
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 |
#replace(node) ⇒ AbstractCursor 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 |
#root? ⇒ Boolean
33 34 35 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 33 def root? false end |
#up ⇒ AbstractCursor
56 57 58 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 56 def up @parent end |