Sha256: e44055d608ffbd460e04a83e25beec1d3f165bd4fec33ac5f6e76a1b4e0f3ace

Contents?: true

Size: 1015 Bytes

Versions: 12

Compression:

Stored size: 1015 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 "data/#{PICKY_ENVIRONMENT}/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'><h3><a href='http://google.com?q=#{@title}'>#{@title}</a></h3><em>#{@author}</em><p>#{@year}, #{@publisher}</p><p>#{@subjects}</p></li>"
  end
  
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
picky-generators-4.11.3 prototypes/shared/client/book.rb
picky-generators-4.11.2 prototypes/shared/both/book.rb
picky-generators-4.11.1 prototypes/shared/both/book.rb
picky-generators-4.11.0 prototypes/shared/both/book.rb
picky-generators-4.10.0 prototypes/shared/both/book.rb
picky-generators-4.9.0 prototypes/shared/both/book.rb
picky-generators-4.8.1 prototypes/shared/both/book.rb
picky-generators-4.8.0 prototypes/shared/both/book.rb
picky-generators-4.7.0 prototypes/shared/both/book.rb
picky-generators-4.6.6 prototypes/shared/both/book.rb
picky-generators-4.6.5 prototypes/shared/both/book.rb
picky-generators-4.6.4 prototypes/shared/both/book.rb