Module: Card::Fetch::Retrieve

Included in:
Card::Fetch
Defined in:
lib/card/fetch/retrieve.rb

Overview

retrieval and instantiation methods for Card::Fetch

Instance Method Summary collapse

Instance Method Details

#id_from_markObject



43
44
45
# File 'lib/card/fetch/retrieve.rb', line 43

def id_from_mark
  mark_type == :id ? mark_value : Lexicon.id(mark_value)
end

#mark_typeObject

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



49
50
51
# File 'lib/card/fetch/retrieve.rb', line 49

def mark_type
  @mark_type ||= mark.is_a?(Integer) ? :id : :key
end

#mark_valueObject



53
54
55
# File 'lib/card/fetch/retrieve.rb', line 53

def mark_value
  @mark_value ||= mark.is_a?(Integer) ? mark : mark.key
end

#retrieval_from_db_queryObject



28
29
30
31
32
33
# 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_baseObject



35
36
37
38
39
40
41
# 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_existingObject

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



6
7
8
9
10
# File 'lib/card/fetch/retrieve.rb', line 6

def retrieve_existing
  return unless mark.present?

  retrieve_from_cache || retrieve_from_db
end

#retrieve_from_cacheObject



12
13
14
15
16
17
18
19
# 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_dbObject



21
22
23
24
25
26
# File 'lib/card/fetch/retrieve.rb', line 21

def retrieve_from_db
  query = retrieval_from_db_query
  @card = query ? Card.where(query).take : nil
  @fresh_from_db = true if card.present? && !card.trash
  card
end