diff options
Diffstat (limited to 'lib/sidebars/concerns/positionable_list.rb')
-rw-r--r-- | lib/sidebars/concerns/positionable_list.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/sidebars/concerns/positionable_list.rb b/lib/sidebars/concerns/positionable_list.rb new file mode 100644 index 00000000000..0bbe1d918e5 --- /dev/null +++ b/lib/sidebars/concerns/positionable_list.rb @@ -0,0 +1,56 @@ +# 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 + + 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 |