Class: UserBook

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/user_book.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book_data = {}) ⇒ UserBook

Returns a new instance of UserBook.

Raises:

  • (ArgumentError)


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

def initialize(book_data = {})
  raise ArgumentError unless book_data.is_a?(Hash)

  @info = book_data

  @id = info['id'] || ""
  @title = info['title'] || ""
  @authors = info['authors'] || [] 
  @publisher = info['publisher'] || ""
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/user_book.rb', line 7

def id
  @id
end

#infoObject

Returns the value of attribute info.



7
8
9
# File 'lib/user_book.rb', line 7

def info
  @info
end

#publisherObject

Returns the value of attribute publisher.



7
8
9
# File 'lib/user_book.rb', line 7

def publisher
  @publisher
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/user_book.rb', line 7

def title
  @title
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  return authors if key =~ /authors?/i
  info[key]
end

#authorObject



37
38
39
# File 'lib/user_book.rb', line 37

def author
  authors
end

#authorsObject



33
34
35
# File 'lib/user_book.rb', line 33

def authors
  @authors.join(', ')
end

#pretty_exportObject



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

def pretty_export
  str = []
  %w[title author publisher].each do |ind|
    str << "%s: %s" % [ind.capitalize, self[ind]]
  end
  return str.join("\n")
end