Module: Riml::Walkable
Instance Method Summary collapse
- #child_after(node) ⇒ Object
- #child_previous_to(node) ⇒ Object
- #each(&block) ⇒ Object (also: #walk)
- #index_by_children ⇒ Object
- #index_by_member ⇒ Object
- #insert_after(node, new_node) ⇒ Object
- #insert_before(node, new_node) ⇒ Object
- #next ⇒ Object
- #previous ⇒ Object
- #remove ⇒ Object
- #replace_with(new_node) ⇒ Object
Instance Method Details
#child_after(node) ⇒ Object
44 45 46 |
# File 'lib/riml/walkable.rb', line 44 def child_after(node) node.next end |
#child_previous_to(node) ⇒ Object
22 23 24 |
# File 'lib/riml/walkable.rb', line 22 def child_previous_to(node) node.previous end |
#each(&block) ⇒ Object Also known as: walk
5 6 7 |
# File 'lib/riml/walkable.rb', line 5 def each(&block) children.each(&block) end |
#index_by_children ⇒ Object
64 65 66 |
# File 'lib/riml/walkable.rb', line 64 def index_by_children parent.children.find_index(self) end |
#index_by_member ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/riml/walkable.rb', line 54 def index_by_member attrs = parent.members attrs.each_with_index do |attr, i| if parent.send(attr) == self return i end end nil end |
#insert_after(node, new_node) ⇒ Object
48 49 50 51 52 |
# File 'lib/riml/walkable.rb', line 48 def insert_after(node, new_node) idx = children.find_index(node) return unless idx children.insert(idx + 1, new_node) end |
#insert_before(node, new_node) ⇒ Object
26 27 28 29 30 |
# File 'lib/riml/walkable.rb', line 26 def insert_before(node, new_node) idx = children.find_index(node) return unless idx children.insert(idx - 1, new_node) end |
#next ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/riml/walkable.rb', line 32 def next idx = index_by_member if idx && parent.members[idx + 1] attr = parent.members[idx + 1] return parent.send(attr) else idx = index_by_children return unless idx parent.children.fetch(idx + 1) end end |
#previous ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/riml/walkable.rb', line 10 def previous idx = index_by_member if idx && parent.members[idx - 1] attr = parent.members[idx - 1] return send(attr) else idx = index_by_children return unless idx parent.children.fetch(idx - 1) end end |
#remove ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/riml/walkable.rb', line 68 def remove idx = index_by_member if idx attr = parent.members[idx] parent.send("#{attr}=", nil) else idx = index_by_children parent.children.slice!(idx) if idx end end |
#replace_with(new_node) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/riml/walkable.rb', line 79 def replace_with(new_node) idx = index_by_member if idx attr = parent.members[idx] new_node.parent = parent parent.send("#{attr}=", new_node) new_node else idx = index_by_children return unless idx new_node.parent = parent parent.children.insert(idx, new_node) parent.children.slice!(idx + 1) new_node end end |