Class: Stupidedi::Zipper::RootCursor
- Inherits:
-
AbstractCursor
- Object
- AbstractCursor
- Stupidedi::Zipper::RootCursor
- Defined in:
- lib/stupidedi/zipper/root_cursor.rb
Instance Attribute Summary collapse
- #node ⇒ AbstractNode readonly
- #path ⇒ AbstractPath readonly
Query the Tree Location collapse
-
#depth ⇒ Integer
Distance from the root node.
- #first? ⇒ Object
- #last? ⇒ Object
- #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, void
Navigate to the next (rightward) sibling node.
-
#prev ⇒ AbstractCursor, void
Navigate to the previous (leftward) sibling node.
-
#root ⇒ RootCursor
Navigate to the root node.
-
#up ⇒ AbstractCursor, void
Navigate to the parent node.
Editing the Tree collapse
-
#append(node) ⇒ EditedCursor, void
Insert a new sibling node after (to the right of) the current node, and navigate to the new sibling node.
-
#delete ⇒ EditedCursor, void
Remove the current node, and navigate to the next (rightward) node if one exists.
-
#prepend(node) ⇒ EditedCursor, void
Insert a new sibling node before (to the left of) the current node, and navigate to the new sibling node.
-
#replace(node) ⇒ AbstractCursor, RootCursor
Replace the current node with the given node.
Instance Method Summary collapse
-
#initialize(node) ⇒ RootCursor
constructor
A new instance of RootCursor.
Methods inherited from AbstractCursor
#append_child, #between, #child, #children, #dangle, #descendant, #down, #flatten, #insert_left, #insert_right, #prepend_child
Constructor Details
#initialize(node) ⇒ RootCursor
Returns a new instance of RootCursor.
12 13 14 15 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 12 def initialize(node) @node, @path = node, Root end |
Instance Attribute Details
#node ⇒ AbstractNode (readonly)
7 8 9 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 7 def node @node end |
#path ⇒ AbstractPath (readonly)
10 11 12 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 10 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
90 91 92 93 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 90 def append(node) raise Exceptions::ZipperError, "root node has no siblings" end |
#delete ⇒ EditedCursor, void
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.
110 111 112 113 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 110 def delete raise Exceptions::ZipperError, "cannot delete root node" end |
#depth ⇒ Integer
Distance from the root node
21 22 23 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 21 def depth 0 end |
#first ⇒ AbstractCursor
Navigate to the first (leftmost) sibling node
49 50 51 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 49 def first self end |
#first? ⇒ Object
26 27 28 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 26 def first? true end |
#last ⇒ AbstractCursor
Navigate to the last (rightmost) sibling node
54 55 56 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 54 def last self end |
#last? ⇒ Object
31 32 33 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 31 def last? true end |
#leaf? ⇒ Boolean
36 37 38 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 36 def leaf? @node.leaf? or @node.children.empty? end |
#next ⇒ AbstractCursor, void
Navigate to the next (rightward) sibling node
60 61 62 63 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 60 def next raise Exceptions::ZipperError, "root node has no siblings" 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
97 98 99 100 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 97 def prepend(node) raise Exceptions::ZipperError, "root node has no siblings" end |
#prev ⇒ AbstractCursor, void
Navigate to the previous (leftward) sibling node
67 68 69 70 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 67 def prev raise Exceptions::ZipperError, "root node has no siblings" end |
#replace(node) ⇒ AbstractCursor, RootCursor
Replace the current node with the given node
104 105 106 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 104 def replace(node) RootCursor.new(node) end |
#root ⇒ RootCursor
Navigate to the root node
74 75 76 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 74 def root self end |
#root? ⇒ Boolean
41 42 43 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 41 def root? true end |
#up ⇒ AbstractCursor, void
Navigate to the parent node
80 81 82 83 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 80 def up raise Exceptions::ZipperError, "root node has no parent" end |