Module: Linked::Mutable
- Included in:
- Listable
- Defined in:
- lib/linked/mutable.rb
Overview
This module collects all the methods that mutate a listable item.
Instance Method Summary collapse
-
#append(object) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#delete ⇒ self
Remove an item from the chain.
-
#delete_after ⇒ nil, Listable
Remove all items after this one in the chain.
-
#delete_before ⇒ nil, Listable
Remove all items before this one in the chain.
-
#prepend(object) ⇒ Object
rubocop:disable Metrics/MethodLength.
Instance Method Details
#append(object) ⇒ Object
rubocop:disable Metrics/MethodLength
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/linked/mutable.rb', line 8 def append(object) # Assume the given object to be the head of its chain # B. If it is not, chain B will be split before the # object, and the sub chain in which the object now is # the head will be appended. sub_chain_b_head = coerce object # Grab the first item in this chain. We will need it # later. target_chain = chain_head # Split chain B before the given object and grab the # tail of that new sub chain. sub_chain_b_tail = sub_chain_b_head.split_before_and_insert target_chain # If we are the last item in our chain we need to # notify the head that there is a new tail. # Otherwise the next item in the list need to be # linked up correctly. if chain_tail? target_chain.prev = sub_chain_b_tail else sub_chain_b_tail.next = next! next!.prev = sub_chain_b_tail end # Connect sub chain B to this item sub_chain_b_head.prev = self self.next = sub_chain_b_head sub_chain_b_tail end |
#delete ⇒ self
Remove an item from the chain. Note that calling #delete on the first item in a chain causes all subsequent items to be moved to a new chain.
Usage
Example using the chain A <> B <> C
A.delete # => A | B <> C
B.delete # => B | A <> C
C.delete # => C | A <> B
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/linked/mutable.rb', line 83 def delete if chain_head? split_after_and_insert else shrink if chain_tail? chain_head.prev = @_prev else @_next.prev = @_prev end @_prev.next = @_next end reset_item end |
#delete_after ⇒ nil, Listable
Remove all items after this one in the chain.
115 116 117 118 119 120 121 |
# File 'lib/linked/mutable.rb', line 115 def delete_after return nil if chain_tail? item = next! item.split_before_and_insert item end |
#delete_before ⇒ nil, Listable
Remove all items before this one in the chain.
107 108 109 |
# File 'lib/linked/mutable.rb', line 107 def delete_before prev!.split_after_and_insert unless chain_head? end |
#prepend(object) ⇒ Object
rubocop:disable Metrics/MethodLength
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/linked/mutable.rb', line 45 def prepend(object) sub_chain_a_tail = coerce object if chain_head? sub_chain_a_tail.split_after_and_insert sub_chain_a_tail.append self return chain_head end target_chain = chain_head sub_chain_a_head = sub_chain_a_tail.split_after_and_insert target_chain prev!.next = sub_chain_a_head sub_chain_a_head.prev = prev! sub_chain_a_tail.next = self self.prev = sub_chain_a_tail sub_chain_a_head end |