Sha256: 4c95bba16123051d91eb64f99a7d341c3c876c3ee0b38f32109db3d00e4f3f8b

Contents?: true

Size: 913 Bytes

Versions: 1

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

1 entries across 1 versions & 1 rubygems

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