class Alexandria::LibrarySortOrder

Public Class Methods

new(book_attribute, ascending = true) click to toggle source
# File lib/alexandria/library_sort_order.rb, line 11
def initialize(book_attribute, ascending = true)
  @book_attribute = book_attribute
  @ascending = ascending
end

Public Instance Methods

sort(library) click to toggle source
# File lib/alexandria/library_sort_order.rb, line 16
def sort(library)
  sorted = library.sort_by do |book|
    book.send(@book_attribute)
  end
  sorted.reverse! unless @ascending
  sorted
rescue StandardError => ex
  log.warn { "Could not sort library by #{@book_attribute.inspect}: #{ex.message}" }
  library
end
to_s() click to toggle source
# File lib/alexandria/library_sort_order.rb, line 27
def to_s
  "#{@book_attribute} #{@ascending ? '(ascending)' : '(descending)'}"
end