I have an array of objects "orders" which consists of books/readers/date
I need to get the top reader (is the one that takes the most number of books) and the quantity of returned top readers must be configurable. Default quantity is 1 reader
Here is an output from 16 orders
[#<Order:0x0000562626492558 @book="Codename: American Man", @reader="Mariel Beier", @date="30.03.2021">,
#<Order:0x00005626264924e0 @book="Forbidden Wizard", @reader="Mariel Beier", @date="30.03.2021">,
#<Order:0x0000562626492468 @book="Action Ninja", @reader="Taryn Gutmann", @date="30.03.2021">,
#<Order:0x00005626264923f0 @book="Action Ninja", @reader="Garret Lindgren", @date="30.03.2021">,
#<Order:0x0000562626492378 @book="Forbidden Wizard", @reader="Alysa Keeling", @date="30.03.2021">,
#<Order:0x0000562626492300 @book="War of the American Imp", @reader="Garrett Stroman", @date="30.03.2021">,
#<Order:0x0000562626492288 @book="Blue Witch", @reader="Garrett Stroman", @date="30.03.2021">,
#<Order:0x0000562626492210 @book="War of the American Imp", @reader="Sherlyn Schumm", @date="30.03.2021">,
#<Order:0x0000562626492198 @book="Action Ninja", @reader="Mac Funk", @date="30.03.2021">,
#<Order:0x0000562626492120 @book="Forbidden Wizard", @reader="Mariel Beier", @date="30.03.2021">,
#<Order:0x00005626264920a8 @book="The Nuclear Wolves", @reader="Les Conn", @date="30.03.2021">,
#<Order:0x0000562626492030 @book="War of the American Imp", @reader="Taryn Gutmann", @date="30.03.2021">,
#<Order:0x0000562626491fb8 @book="War of the American Imp", @reader="Les Conn", @date="30.03.2021">,
#<Order:0x0000562626491f40 @book="The Nuclear Wolves", @reader="Les Conn", @date="30.03.2021">,
#<Order:0x0000562626491ec8 @book="Action Ninja", @reader="Diamond Cole", @date="30.03.2021">,
#<Order:0x0000562626491e50 @book="Action Ninja", @reader="Garret Lindgren", @date="30.03.2021">]
Basically i need to get the mode by keywords (books/readers/the most popular author)
I am using ffaker to generate random data for this array
def build_author
name = FFaker::Book.author
biography = FFaker::Book.description
Author.new(name, biography)
end
def build_book(author)
title = FFaker::Book.title
Book.new(title, author.name)
end
def build_reader
name = FFaker::Name.name
email = FFaker::Internet.email
city = FFaker::Address.city
street = FFaker::Address.street_name
house = rand(1 - 10_000)
Reader.new(name, email, city, street, house)
end
def build_order(book, reader)
date = Time.now.utc.strftime('%d.%m.%Y')
Order.new(book.title, reader.name, date)
end
def fill_author
3.times do
author = build_author
@authors.push(author)
end
end
def fill_book
6.times do
book = build_book(@authors.sample)
@books.push(book)
end
end
def fill_reader
10.times do
reader = build_reader
@readers.push(reader)
end
end
def fill_order
16.times do
@orders.push(build_order(@books.sample, @readers.sample))
end
end
Here is the main library class
class Library
include DataBuilder
include Statistics
attr_accessor :authors, :books, :orders, :readers
def initialize(authors: [], books: [], orders: [], readers: [])
@books = books
@orders = orders
@readers = readers
@authors = authors
end
def create_data
create_arrays
generate_data
end
def show
create_data
p top_reader
end
Edit:
There is how i get top reader/book now
def top_reader
orders_grouped = orders.group_by(&:reader)
tab = orders_grouped.max_by { |_k, v| v.count }.first
puts "#{tab} is the top reader"
end
def top_book
orders_grouped = orders.group_by(&:book)
tab = orders_grouped.max_by { |_k, v| v.count }.first
puts "#{tab} is the top book"
end
Here is an output:
Mariel Beier is the top reader
Action Ninja is the top book
Don't know yet how to configure the amount of top readers/books