Module: Sidebars::Concerns::PositionableList

Included in:
Menu, Panel
Defined in:
lib/sidebars/concerns/positionable_list.rb

Instance Method Summary collapse

Instance Method Details

#add_element(list, element) ⇒ Object


7
8
9
10
11
# File 'lib/sidebars/concerns/positionable_list.rb', line 7

def add_element(list, element)
  return unless element

  list << element
end

#insert_element_after(list, after_element, new_element) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sidebars/concerns/positionable_list.rb', line 25

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

#insert_element_before(list, before_element, new_element) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sidebars/concerns/positionable_list.rb', line 13

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

#remove_element(list, element_to_remove) ⇒ Object


47
48
49
50
51
52
53
# File 'lib/sidebars/concerns/positionable_list.rb', line 47

def remove_element(list, element_to_remove)
  index = index_of(list, element_to_remove)

  return unless index

  list.slice!(index)
end

#replace_element(list, element_to_replace, new_element) ⇒ Object


37
38
39
40
41
42
43
44
45
# File 'lib/sidebars/concerns/positionable_list.rb', line 37

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