Sha256: ceab214758e9ea2837d4666c0cb74bd984384af8790ab4819312587ee972ca10

Contents?: true

Size: 1.01 KB

Versions: 26

Compression:

Stored size: 1.01 KB

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_all_by_id uses a lookup table.
  #
  def self.find_all_by_id 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

26 entries across 26 versions & 1 rubygems

Version Path
picky-generators-4.19.4 prototypes/shared/client/book.rb
picky-generators-4.19.3 prototypes/shared/client/book.rb
picky-generators-4.19.2 prototypes/shared/client/book.rb
picky-generators-4.19.1 prototypes/shared/client/book.rb
picky-generators-4.19.0 prototypes/shared/client/book.rb
picky-generators-4.18.0 prototypes/shared/client/book.rb
picky-generators-4.17.1 prototypes/shared/client/book.rb
picky-generators-4.17.0 prototypes/shared/client/book.rb
picky-generators-4.16.0 prototypes/shared/client/book.rb
picky-generators-4.15.1 prototypes/shared/client/book.rb
picky-generators-4.15.0 prototypes/shared/client/book.rb
picky-generators-4.14.0 prototypes/shared/client/book.rb
picky-generators-4.13.1 prototypes/shared/client/book.rb
picky-generators-4.13.0 prototypes/shared/client/book.rb
picky-generators-4.12.13 prototypes/shared/client/book.rb
picky-generators-4.12.12 prototypes/shared/client/book.rb
picky-generators-4.12.11 prototypes/shared/client/book.rb
picky-generators-4.12.10 prototypes/shared/client/book.rb
picky-generators-4.12.8 prototypes/shared/client/book.rb
picky-generators-4.12.7 prototypes/shared/client/book.rb