Class: Alexandria::LibrarySortOrder
- Inherits:
-
Object
- Object
- Alexandria::LibrarySortOrder
- Includes:
- Logging
- Defined in:
- lib/alexandria/export_library.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Unsorted
Instance Method Summary collapse
-
#initialize(book_attribute, ascending = true) ⇒ LibrarySortOrder
constructor
A new instance of LibrarySortOrder.
- #sort(library) ⇒ Object
- #to_s ⇒ Object
Methods included from Logging
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.}" } library end |
#to_s ⇒ Object
50 51 52 |
# File 'lib/alexandria/export_library.rb', line 50 def to_s "#{@book_attribute} #{@ascending ? '(ascending)' : '(descending)'}" end |