Sha256: fc24ee161c9f173ee50e2cc6fa8eafd96684934203803b0606ae1ad91ee01aca

Contents?: true

Size: 857 Bytes

Versions: 1

Compression:

Stored size: 857 Bytes

Contents

class FindMostPopularBook
  def self.perform(library, places_number: 1)
    new(library, places_number: places_number).perform
  end

  def initialize(library, places_number: 1)
    @library = library
    @places_number = places_number
  end

  def perform
    sorted_frequencies = sort_frequency_hash(build_frequency_hash)
    find_n_most_popular(sorted_frequencies)
  end

  private

  def build_frequency_hash
    frequency_hash = Hash.new { |hash, key| hash[key] = 0 }
    library.orders.each { |order| frequency_hash[order.book] += 1 }
    frequency_hash
  end

  def sort_frequency_hash(frequency_hash)
    frequency_hash.sort_by { |_key, value| value }.reverse
  end

  def find_n_most_popular(sorted_frequencies)
    (0...places_number).map do |position|
      sorted_frequencies[position]
    end
  end

  attr_reader :library, :places_number
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bokov_library-1.0.0 lib/find_most_popular_book.rb