1 2 3 4 5 6 7 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
# frozen_string_literal: true
# This module handles element positions in a list.
module Sidebars
module Concerns
module PositionableList
def add_element(list, element)
return unless element
list << element
end
def insert_element_before(list, before_element, new_element)
return unless new_element
index = index_of(list, before_element)
if index
list.insert(index, new_element)
else
list.unshift(new_element)
end
end
def insert_element_after(list, after_element, new_element)
return unless new_element
index = index_of(list, after_element)
if index
list.insert(index + 1, new_element)
else
add_element(list, new_element)
end
end
def replace_element(list, element_to_replace, new_element)
return unless new_element
index = index_of(list, element_to_replace)
return unless index
list[index] = new_element
end
def remove_element(list, element_to_remove)
index = index_of(list, element_to_remove)
return unless index
list.slice!(index)
end
private
# Classes including this method will have to define
# the way to identify elements through this method
def index_of(list, element)
raise NotImplementedError
end
end
end
end
|