Sha256: e14c6c8b46a313dfb0e91be86cd0d7e8ead721c5a5be9f94e1636090ca659d2a

Contents?: true

Size: 973 Bytes

Versions: 15

Compression:

Stored size: 973 Bytes

Contents

require 'csv'

# A book is simple, it has just:
#  * a title
#  * an author
#  * an isbn
#  * a publishing year
#  * a publisher
#  * a number of subjects
#
class Book
  
  @@books_mapping = {}
  
  # Load the books on startup.
  #
  file_name = File.expand_path 'library.csv', File.dirname(__FILE__)
  CSV.open(file_name, 'r').each do |row|
    @@books_mapping[row.shift.to_i] = row
  end
  
  # Find uses a lookup table.
  #
  def self.find ids, _ = {}
    ids.map { |id| new(id, *@@books_mapping[id]) }
  end
  
  attr_reader :id
  
  def initialize id, title, author, isbn, year, publisher, subjects
    @id, @title, @author, @isbn, @year, @publisher, @subjects = id, title, author, isbn, year, publisher, subjects
  end
  
  # "Rendering" ;)
  #
  # Note: This is just an example. Please do not render in the model.
  #
  def to_s
    "<div class='book'><p>\"#{@title}\", by #{@author}</p><p>#{@year}, #{@publisher}, #{@isbn}</p><p>#{@subjects}</p></div>"
  end
  
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
picky-client-0.12.0 sinatra_prototype/book.rb
picky-client-0.11.2 sinatra_prototype/book.rb
picky-client-0.11.1 sinatra_prototype/book.rb
picky-client-0.11.0 sinatra_prototype/book.rb
picky-client-0.10.5 sinatra_prototype/book.rb
picky-client-0.10.4 sinatra_prototype/book.rb
picky-client-0.10.2 sinatra_prototype/book.rb
picky-client-0.10.1 sinatra_prototype/book.rb
picky-client-0.10.0 sinatra_prototype/book.rb
picky-client-0.9.4 sinatra_prototype/book.rb
picky-client-0.9.3 sinatra_prototype/book.rb
picky-client-0.9.2 sinatra_prototype/book.rb
picky-client-0.9.1 sinatra_prototype/book.rb
picky-client-0.9.0 sinatra_prototype/book.rb
picky-client-0.3.0 sinatra_prototype/book.rb