module Card::Fetch::Retrieve

retrieval and instantiation methods for Card::Fetch

Public Instance Methods

id_from_mark() click to toggle source
# File lib/card/fetch/retrieve.rb, line 43
def id_from_mark
  mark_type == :id ? mark_value : Lexicon.id(mark_value)
end
mark_type() click to toggle source

In both the cache and the db, ids and keys are used to retrieve card data. These methods identify the kind of mark to use and its value

# File lib/card/fetch/retrieve.rb, line 49
def mark_type
  @mark_type ||= mark.is_a?(Integer) ? :id : :key
end
mark_value() click to toggle source
# File lib/card/fetch/retrieve.rb, line 53
def mark_value
  @mark_value ||= mark.is_a?(Integer) ? mark : mark.key
end
retrieval_from_db_query() click to toggle source
# File lib/card/fetch/retrieve.rb, line 28
def retrieval_from_db_query
  return unless (query = retrieval_from_db_query_base)

  query[:trash] = false unless look_in_trash?
  query
end
retrieval_from_db_query_base() click to toggle source
# File lib/card/fetch/retrieve.rb, line 35
def retrieval_from_db_query_base
  if mark_type == :key && mark.simple?
    { key: mark_value }
  elsif (id = id_from_mark)
    { id: id }
  end
end
retrieve_existing() click to toggle source

look for card in cache. if that doesn’t work, look in database

# File lib/card/fetch/retrieve.rb, line 6
def retrieve_existing
  return unless mark.present?

  retrieve_from_cache || retrieve_from_db
end
retrieve_from_cache() click to toggle source
# File lib/card/fetch/retrieve.rb, line 12
def retrieve_from_cache
  @card = Card.send "retrieve_from_cache_by_#{mark_type}",
                    mark_value, @opts[:local_only]
  @card = nil if card&.new? && look_in_trash?
  # don't return cached cards if looking in trash -
  # we want the db version
  card
end
retrieve_from_db() click to toggle source
# File lib/card/fetch/retrieve.rb, line 21
def retrieve_from_db
  query = retrieval_from_db_query
  @card = query ? Card.where(query).take : nil
  @cache_ready = true if card.present? && !card.trash
  card
end