Module: Booklist

Defined in:
lib/booklist.rb,
lib/booklist/tag.rb,
lib/booklist/book.rb,
lib/booklist/version.rb

Defined Under Namespace

Classes: Book, Tag, Tagging

Constant Summary collapse

VERSION =
"0.0.10"

Class Method Summary collapse

Class Method Details

.add_book(options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/booklist.rb', line 6

def self.add_book(options)
  if !options.title || options.title == ""
    puts "Please add a --title with the book name"
    puts usage
    exit
  end
  book = Book.new do |b|
    b.title = options.title
    b.author = options.author if options.author
    b.addn_authors = options.addn_authors if options.addn_authors
    b.state = options.state if options.state
    b.date_read = options.date_read if options.date_read
    b.tag_list = options.tags if options.tags
  end
  book.save
  book.cli_display
end

.delete_book(id) ⇒ Object



61
62
63
64
# File 'lib/booklist.rb', line 61

def self.delete_book(id)
  book = Book.find_by(id: id)
  book.destroy
end

.edit_book(options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/booklist.rb', line 24

def self.edit_book(options)
  book = Book.find_by(id: options.id)
  if book
    book.title = options.title if options.title
    book.author = options.author if options.author
    book.addn_authors = options.addn_authors if options.addn_authors
    book.state = options.state if options.state
    book.date_read = options.date_read if options.date_read
    book.tag_list = options.tags if options.tags
    book.save
    book.cli_display
  else
    puts "No book with ID of #{options.id} was found"
  end
end

.list_booksObject



66
67
68
69
# File 'lib/booklist.rb', line 66

def self.list_books
  books =Book.all
  books.each{ |b| b.cli_display }
end

.read_book(id) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/booklist.rb', line 40

def self.read_book(id)
  book = Book.find_by(id: id)
  if book
    book.cli_display
  else
    puts "No book with ID of #{id} was found"
  end
end

.search_books(options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/booklist.rb', line 49

def self.search_books(options)
  books = []
  books += Book.where(["title LIKE ?", "%#{options.title}%"]).to_a if options.title
  books += Book.where(["author LIKE ? or addn_authors LIKE ?", "%#{options.author}%", "%#{options.author}%"]).to_a if options.author
  books += Book.where(["author LIKE ? or addn_authors LIKE ?", "%#{options.addn_authors}%", "%#{options.addn_authors}%"]).to_a if options.addn_authors
  books += Book.where(["state LIKE ?", "%#{options.state}%"]).to_a if options.state
  books.uniq!
  books.sort!{ |a, b| a.id <=> b.id}
  books.select!{ |b| b.state == options.state} if options.state
  books.each { |b| b.cli_display }
end