Class: BookStoreCore

Inherits:
Object
  • Object
show all
Defined in:
lib/BookStoreCore.rb

Overview

core class for bookstore

Instance Method Summary collapse

Instance Method Details

#add_update_book(title, rating, book_hash) ⇒ Object



24
25
26
27
# File 'lib/BookStoreCore.rb', line 24

def add_update_book(title, rating, book_hash)
  book_hash[title.to_sym] = rating.to_i
  book_hash
end

#delete_book(title, book_hash) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/BookStoreCore.rb', line 29

def delete_book(title, book_hash)
  updated_books = {}
  book_hash.each do |k, _v|
    updated_books[k] = v if k.to_s != title
  end
  updated_books
end

#exist?(title, book_hash) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# File 'lib/BookStoreCore.rb', line 16

def exist?(title, book_hash)
  is_exist = false
  book_hash.each do |k, _v|
    is_exist = true if k.to_s == title
  end
  is_exist
end

#get_all_books(book_hash) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/BookStoreCore.rb', line 45

def get_all_books(book_hash)
  output_string = ''
  book_hash.each do |book, rating|
    output_string += "#{book} has rating #{rating}."
  end
  output_string
end

#get_full_title(start_chars, book_hash) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/BookStoreCore.rb', line 37

def get_full_title(start_chars, book_hash)
  output_string = ''
  book_hash.each do |title, _rating|
    output_string += title.to_s if title.to_s.include? start_chars
  end
  output_string
end

#readf(path) ⇒ Object



11
12
13
14
# File 'lib/BookStoreCore.rb', line 11

def readf(path)
  file = File.read(path)
  JSON.parse(file)
end

#writef(path, book_hash) ⇒ Object



5
6
7
8
9
# File 'lib/BookStoreCore.rb', line 5

def writef(path, book_hash)
  File.open(path, 'w') do |f|
    f.write(book_hash.to_json)
  end
end