Sha256: 54d6600d93f31ea2dd10a372eccfbcdd652e47a67937717df392d9e7c2e79390

Contents?: true

Size: 930 Bytes

Versions: 138

Compression:

Stored size: 930 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 render
    "<li class='book'><p>\"#{@title}\", by #{@author}</p><p>#{@year}, #{@publisher}</p><p>#{@subjects}</p></li>"
  end

end

Version data entries

138 entries across 89 versions & 1 rubygems

Version Path
picky-generators-3.2.0 prototypes/all_in_one/sinatra/book.rb
picky-generators-3.1.13 prototypes/client/sinatra/book.rb
picky-generators-3.1.13 prototypes/all_in_one/sinatra/book.rb
picky-generators-3.1.12 prototypes/all_in_one/sinatra/book.rb
picky-generators-3.1.12 prototypes/client/sinatra/book.rb
picky-generators-3.1.11 prototypes/client/sinatra/book.rb
picky-generators-3.1.11 prototypes/all_in_one/sinatra/book.rb
picky-generators-3.1.10 prototypes/client/sinatra/book.rb
picky-generators-3.1.10 prototypes/all_in_one/sinatra/book.rb
picky-generators-3.1.9 prototypes/all_in_one/sinatra/book.rb
picky-generators-3.1.9 prototypes/client/sinatra/book.rb
picky-generators-3.1.8 prototypes/all_in_one/sinatra/book.rb
picky-generators-3.1.8 prototypes/client/sinatra/book.rb
picky-generators-3.1.7 prototypes/client/sinatra/book.rb
picky-generators-3.1.7 prototypes/all_in_one/sinatra/book.rb
picky-generators-3.1.6 prototypes/all_in_one/sinatra/book.rb
picky-generators-3.1.6 prototypes/client/sinatra/book.rb
picky-generators-3.1.5 prototypes/all_in_one/sinatra/book.rb
picky-generators-3.1.5 prototypes/client/sinatra/book.rb
picky-generators-3.1.4 prototypes/client/sinatra/book.rb