Sha256: 813d2891fca358b530aba6c753991cd6627fa50affd2804e8a34ce26018e583b

Contents?: true

Size: 928 Bytes

Versions: 3

Compression:

Stored size: 928 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, year, publisher, subjects
    @id, @title, @author, @year, @publisher, @subjects = id, title, author, year, publisher, subjects
  end

  # "Rendering" ;)
  #
  # Note: This is just an example. Please do not render in the model.
  #
  def to_s
    "<li class='book'><p>\"#{@title}\", by #{@author}</p><p>#{@year}, #{@publisher}</p><p>#{@subjects}</p></li>"
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
picky-generators-2.0.0.pre1 prototypes/client/sinatra/book.rb
picky-generators-1.5.4 prototypes/client/sinatra/book.rb
picky-generators-1.5.3 prototypes/client/sinatra/book.rb