Sha256: 2104f4a6f3e5a00ca4285bf5a4a776bd361234df24a1eca16ffb0b764ec80d4a

Contents?: true

Size: 942 Bytes

Versions: 5

Compression:

Stored size: 942 Bytes

Contents

# frozen_string_literal: true

# This file is part of Alexandria.
#
# See the file README.md for authorship and licensing information.

module Alexandria
  class LibrarySortOrder
    include Logging

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

    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

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

    class Unsorted < LibrarySortOrder
      def initialize
        super(nil, nil)
      end

      def sort(library)
        library
      end

      def to_s
        "default order"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alexandria-book-collection-manager-0.7.10 lib/alexandria/library_sort_order.rb
alexandria-book-collection-manager-0.7.9 lib/alexandria/library_sort_order.rb
alexandria-book-collection-manager-0.7.8 lib/alexandria/library_sort_order.rb
alexandria-book-collection-manager-0.7.7 lib/alexandria/library_sort_order.rb
alexandria-book-collection-manager-0.7.6 lib/alexandria/library_sort_order.rb