class UserLibrary

Attributes

books[R]
filename[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/user_library.rb, line 13
def initialize(args = {})

  @books = []
  @nonpersistent = args[:nonpersistent]

  unless nonpersistent?
    @filename = args[:filename] || 
      File.join(File.dirname(__FILE__), "../saved_libraries/library.json")

    determine_filename!
    load_saved_library!
  end

end

Public Instance Methods

<<(book) click to toggle source
# File lib/user_library.rb, line 42
def <<(book)
  add(book)
end
[](index) click to toggle source
# File lib/user_library.rb, line 64
def [](index)
  @books[index]
end
add(book) click to toggle source
# File lib/user_library.rb, line 28
def add(book)
  raise NotABook unless valid?(book)
  raise BookDuplicateError if any? { |b| b['id'] == book['id'] }
  @books << book
end
delete(index) click to toggle source
# File lib/user_library.rb, line 34
def delete(index)
  @books.delete_at(index)
end
each() { |b| ... } click to toggle source
# File lib/user_library.rb, line 59
def each
  return to_enum :each unless block_given?
  @books.each { |b| yield(b) }
end
length() click to toggle source
# File lib/user_library.rb, line 55
def length
  size
end
nonpersistent?() click to toggle source
# File lib/user_library.rb, line 38
def nonpersistent?
  @nonpersistent
end
pretty_export() click to toggle source
# File lib/user_library.rb, line 79
def pretty_export
  a = [""]
  each_with_index do |b, i|
    j = i+1
    a << "%s%s%s" % ["#{j}.)--", b['id'], "-----"]
    a << b.pretty_export
    a << ""
  end
  a << ""
  a.join("\n")
end
pretty_print() click to toggle source
# File lib/user_library.rb, line 75
def pretty_print
  puts pretty_export
end
save() click to toggle source
# File lib/user_library.rb, line 46
def save
  raise PersistenceError if nonpersistent?
  File.write(@filename, to_json)
end
size() click to toggle source
# File lib/user_library.rb, line 51
def size
  @books.size
end
to_json() click to toggle source
# File lib/user_library.rb, line 68
def to_json
  a = @books.map do |b| 
    b.info 
  end
  JSON.pretty_generate(a)
end

Private Instance Methods

determine_filename!() click to toggle source
# File lib/user_library.rb, line 101
def determine_filename!
  @filename = FileHelper.prepare(@filename)
end
get_raw_JSON_data() click to toggle source
# File lib/user_library.rb, line 105
def get_raw_JSON_data
  file = File.open(@filename)
  JSON.parse(file.read)
end
load_saved_library!() click to toggle source
# File lib/user_library.rb, line 110
def load_saved_library!
  if File.exist?(@filename)
    book_data_set = get_raw_JSON_data
    @books = book_data_set.map { |info| UserBook.new(info) }
    raise ArgumentError unless books.all? { |b| valid?(b) }
  end
end
set_filename(name) click to toggle source
# File lib/user_library.rb, line 96
def set_filename(name)
  @nonpersistent = false
  @filename = FileHelper.prepare(name)
end
valid?(b) click to toggle source
# File lib/user_library.rb, line 92
def valid?(b)
  b.respond_to?(:info)
end