Sha256: 143dab581fb5309db6f6c158bfad4b8139872745867a9d3ff882c800ae1389b5

Contents?: true

Size: 913 Bytes

Versions: 2

Compression:

Stored size: 913 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; end

      def sort(library)
        library
      end

      def to_s
        "default order"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alexandria-book-collection-manager-0.7.5 lib/alexandria/library_sort_order.rb
alexandria-book-collection-manager-0.7.4 lib/alexandria/library_sort_order.rb