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
True when the node has no leftward siblings.
-
#last? ⇒ Boolean
True when the node has no rightward siblings.
-
#leaf? ⇒ Boolean
True if the node has no children.
-
#root? ⇒ Boolean
True if the node has no parent.
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.
10 11 12 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 10 def initialize(parent) @parent = parent end |
Instance Attribute Details
#parent ⇒ AbstractCursor (readonly)
8 9 10 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 8 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.
95 96 97 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 95 def delete self end |
#depth ⇒ Integer
Distance from the root node
39 40 41 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 39 def depth @parent.depth + 1 end |
#first ⇒ AbstractCursor
Navigate to the first (leftmost) sibling node
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
44 45 46 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 44 def first? true end |
#last ⇒ AbstractCursor
Navigate to the last (rightmost) sibling node
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
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
29 30 31 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 29 def leaf? true end |
#next ⇒ AbstractCursor
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?, ...
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 |
#path ⇒ AbstractPath
21 22 23 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 21 def path Hole.new([], @parent.path.parent, []) end |
#prev ⇒ AbstractCursor
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
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
34 35 36 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 34 def root? false end |
#up ⇒ AbstractCursor
57 58 59 |
# File 'lib/stupidedi/zipper/dangling_cursor.rb', line 57 def up @parent end |