class SortedArray::SortedArray

A SortedArray takes a Sorter-object as a parameter. The array gets sorted after each push,<<,unsift. It defines marshal-methods and can be used with PStore @see DefaultSorter

Public Class Methods

new(*args) click to toggle source

@params [Array] args the first argument is a Sorter-object @example

my_array = SortedArray.new( DefaultSorter.new(:foo) )
my_array << AnyClassWhichSupportsFoo.new(1)
my_array << AnyClassWhichSupportsFoo.new(3)
my_array << AnyClassWhichSupportsFoo.new(2)
# my_array.map(&:foo) => 1,2,3

@see DefaultSorter @see Array

Calls superclass method
# File lib/sorted_array/sorted_array.rb, line 18
def initialize *args
  @sorter = args.shift
  super
end

Public Instance Methods

<<(other) click to toggle source

@see Array

# File lib/sorted_array/sorted_array.rb, line 36
def << other
  push *other
  @sorter.sort(self)
end
push(*other) click to toggle source

@see Array

Calls superclass method
# File lib/sorted_array/sorted_array.rb, line 24
def push *other
  super
  @sorter.sort(self)
end
unshift(other) click to toggle source

@see Array

Calls superclass method
# File lib/sorted_array/sorted_array.rb, line 30
def unshift other
  super
  @sorter.sort(self)
end

Protected Instance Methods

marshal_dump() click to toggle source

@return [Array] - Sorter-object or nil, […values…]

# File lib/sorted_array/sorted_array.rb, line 44
def marshal_dump
  [@sorter ? @sorter.marshal_dump : nil, to_a]
end
marshal_load(array) click to toggle source

Initialize sorter and read in data @param [Array] array - _sorter, [..entries..]

# File lib/sorted_array/sorted_array.rb, line 50
def marshal_load array
  _sorter, _data = *array
  if _sorter
    initialize_sorter _sorter
    push *_data if _data
  end
end

Private Instance Methods

initialize_sorter(args) click to toggle source
# File lib/sorted_array/sorted_array.rb, line 60
def initialize_sorter args
  @sorter = args.shift.new *args
end