module ReferenceBook::Library

Public Class Methods

[](key) click to toggle source
# File lib/reference_book/library.rb, line 24
def [](key)
  shelf[key]
end
array_for(attribute) click to toggle source
# File lib/reference_book/library.rb, line 60
def array_for(attribute)
  shelf.map do |key, book|
    book[attribute] if book.respond_to?(attribute)
  end
end
Also aliased as: pluck
book_keys() click to toggle source
# File lib/reference_book/library.rb, line 45
def book_keys
  return @book_keys if @book_keys
  
  if index.any?
    @book_keys = shelf.map { |k, book| book.members }.flatten.uniq
    @book_keys.delete(:title)
    @book_keys.delete(:library_key)
    @book_keys
  else
    []
  end
end
empty!() click to toggle source
# File lib/reference_book/library.rb, line 29
def empty!
  index.each { |key| remove_accessor_for(key) }
  @index = []
  @shelf = {}
  nil
end
hash_for(attribute) click to toggle source
# File lib/reference_book/library.rb, line 68
def hash_for(attribute)
  Hash[
    shelf.map do |key, book|
      [key, (book[attribute] if book.respond_to?(attribute))]
    end
  ]
end
Also aliased as: hash_pluck
hash_pluck(attribute)
Alias for: hash_for
index() click to toggle source
# File lib/reference_book/library.rb, line 19
def index
  @index ||= []
end
pluck(attribute)
Alias for: array_for
rotate(with_meta = false) click to toggle source
# File lib/reference_book/library.rb, line 105
def rotate(with_meta = false)
  hash = Hash[
    book_keys.map do |book_k|
      [book_k, hash_for(book_k)]
    end
  ]

  if with_meta
    hash[:title] = hash_for(:title)
    hash[:library_key] = hash_for(:library_key)
  end

  hash
end
shelf() click to toggle source
# File lib/reference_book/library.rb, line 14
def shelf
  @shelf ||= {}
end
store(book) click to toggle source
# File lib/reference_book/library.rb, line 4
def store(book)
  verify_library_key!(book.library_key)

  shelf[book.library_key] = book
  index << book.library_key
  define_accessor_for(book)
  book
end
to_h(with_meta = false) click to toggle source
# File lib/reference_book/library.rb, line 80
def to_h(with_meta = false)
  hash = {}

  shelf.each do |key, book|
    book_h = book.to_h
    
    unless with_meta
      book_h.delete(:title)
      book_h.delete(:library_key)
    end

    missing = book_keys - book_h.keys
    missing.each do |k|
      book_h[k] = nil
    end

    hash[key] = book_h
  end

  hash
end
verify_library_key!(key) click to toggle source
# File lib/reference_book/library.rb, line 37
def verify_library_key!(key)
  if is_key_in_use?(key)
    raise ReferenceBook::BookDefinitionError, "the library key '#{key}' is already in use."
  end
end

Private Class Methods

define_accessor_for(book) click to toggle source
# File lib/reference_book/library.rb, line 123
def define_accessor_for(book)
  self.define_singleton_method book.library_key do
    self.shelf[book.library_key]
  end
end
is_key_in_use?(key) click to toggle source
# File lib/reference_book/library.rb, line 135
def is_key_in_use?(key)
  index.include?(key)
end
remove_accessor_for(key) click to toggle source
# File lib/reference_book/library.rb, line 130
def remove_accessor_for(key)
  self.singleton_class.send :remove_method, key
end