Class: Alexandria::LibrarySortOrder

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/alexandria/export_library.rb

Direct Known Subclasses

Unsorted

Defined Under Namespace

Classes: Unsorted

Instance Method Summary collapse

Methods included from Logging

included, #log

Constructor Details

#initialize(book_attribute, ascending = true) ⇒ LibrarySortOrder

Returns a new instance of LibrarySortOrder.



34
35
36
37
# File 'lib/alexandria/export_library.rb', line 34

def initialize(book_attribute, ascending = true)
  @book_attribute = book_attribute
  @ascending = ascending
end

Instance Method Details

#sort(library) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/alexandria/export_library.rb', line 39

def sort(library)
  sorted = library.sort_by do |book|
    book.send(@book_attribute)
  end
  sorted.reverse! unless @ascending
  sorted
rescue => ex
  log.warn { "Could not sort library by #{@book_attribute.inspect}: #{ex.message}" }
  library
end

#to_sObject



50
51
52
# File 'lib/alexandria/export_library.rb', line 50

def to_s
  "#{@book_attribute} #{@ascending ? '(ascending)' : '(descending)'}"
end