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
Querying the Tree Location collapse
-
#depth ⇒ Integer
Distance from the root node.
-
#first?
True when the node has no leftward siblings.
-
#last?
True when the node has no rightward siblings.
-
#leaf?
True if the node has no children.
-
#root?
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, 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.
13 14 15 16 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 13 def initialize(node) @node, @path = node, Root end |
Instance Attribute Details
#node ⇒ AbstractNode (readonly)
8 9 10 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 8 def node @node end |
#path ⇒ AbstractPath (readonly)
11 12 13 |
# File 'lib/stupidedi/zipper/root_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
91 92 93 94 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 91 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.
111 112 113 114 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 111 def delete raise Exceptions::ZipperError, "cannot delete root node" end |
#depth ⇒ Integer
Distance from the root node
22 23 24 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 22 def depth 0 end |
#first ⇒ AbstractCursor
Navigate to the first (leftmost) sibling node
50 51 52 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 50 def first self end |
#first?
True when the node has no leftward siblings
27 28 29 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 27 def first? true end |
#last ⇒ AbstractCursor
Navigate to the last (rightmost) sibling node
55 56 57 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 55 def last self end |
#last?
True when the node has no rightward siblings
32 33 34 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 32 def last? true end |
#leaf?
True if the node has no children
37 38 39 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 37 def leaf? @node.leaf? or @node.children.empty? end |
#next ⇒ AbstractCursor, void
Navigate to the next (rightward) sibling node
61 62 63 64 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 61 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
98 99 100 101 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 98 def prepend(node) raise Exceptions::ZipperError, "root node has no siblings" end |
#prev ⇒ AbstractCursor, void
Navigate to the previous (leftward) sibling node
68 69 70 71 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 68 def prev raise Exceptions::ZipperError, "root node has no siblings" end |
#replace(node) ⇒ AbstractCursor, RootCursor
Replace the current node with the given node
105 106 107 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 105 def replace(node) RootCursor.new(node) end |
#root ⇒ RootCursor
Navigate to the root node
75 76 77 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 75 def root self end |
#root?
True if the node has no parent
42 43 44 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 42 def root? true end |
#up ⇒ AbstractCursor, void
Navigate to the parent node
81 82 83 84 |
# File 'lib/stupidedi/zipper/root_cursor.rb', line 81 def up raise Exceptions::ZipperError, "root node has no parent" end |