Class: BookStore

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/bookstore.rb

Overview

BookStore.rb

Instance Method Summary collapse

Constructor Details

#initializeBookStore

Returns a new instance of BookStore.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bookstore.rb', line 8

def initialize
  @books = []

  IO.foreach('book_store.txt') do |line|
    name = line.split('|').first
    rating = line.split('|').last

    book = Book.new(name.intern, rating.to_i)

    @books << book
  end
end

Instance Method Details

#add_book(name, rating) ⇒ Object



21
22
23
24
25
26
# File 'lib/bookstore.rb', line 21

def add_book(name, rating)
  book = Book.new(name.intern, rating.to_i)

  @books << book
  update_store
end

#book_exists(title) ⇒ Object



53
54
55
56
57
58
# File 'lib/bookstore.rb', line 53

def book_exists(title)
  @books.each do |book|
    return true if book.title == title
  end
  false
end

#delete_book(name) ⇒ Object



28
29
30
31
32
# File 'lib/bookstore.rb', line 28

def delete_book(name)
  @books.select! { |book| book.title != name.intern }

  update_store
end

#display_booksObject



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

def display_books
  puts 'Books available in store'
  @books.each do |book|
    puts "Book Name: #{book.title}"
    puts "Book Rating: #{book.rating}"
    puts ''
  end
end

#search_book(name) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/bookstore.rb', line 34

def search_book(name)
  result = []
  @books.each do |book|
    next unless book.title.downcase.to_s.include? name.downcase
    result << Book.new(book.title, book.rating)
  end

  result
end