Sha256: 83226655c4b662ccdd28f84c5f8ed46949ab945df265bca3f9be9c483df15c9c

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

module Orders
  # Abstract (equivalent of SortedList)
  # базовый класс "сортированный список"
  class IndexedList < Hash

    # Returns default list index for items
    def index item
      item.object_id
    end

    # Adds new item to the list (replaces item with the same index)
    def add item
      self[index item] = item
      self
    end

    alias << add

    # Removes item from the list
    def remove item
      delete index item
      self
    end

    # Removes item with given index from the list
    def delete_by_index index
      remove self[index] if self[index]
    end

    # Removes either all items, or items for which given block returns trueish value
    def clear
      if block_given?
        each {|item| remove item if yield item}
      else
        each_value { |item| remove item }
      end
    end

    # Yields list items in order of their index
    def each
      if block_given?
        keys.sort.each { |key| yield self[key] }
      else
        ary = []
        keys.sort.each { |key| ary << self[key] }
        ary
      end
    end

    # Make direct setter private
    private :[]=
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
orders-0.0.4 lib/orders/indexed_list.rb
orders-0.0.3 lib/orders/indexed_list.rb