Module: Card::Cache::Populate

Included in:
ClassMethods
Defined in:
lib/card/cache/populate.rb

Overview

population-related class methods for Card::Cache

Instance Method Summary collapse

Instance Method Details

#populate_fields(list, *fields) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/card/cache/populate.rb', line 28

def populate_fields list, *fields
  name_arrays = list.each_with_object([]) do |item, arrays|
    fields.flatten.each do |field|
      arrays << [item, field]
    end
  end
  populate_names name_arrays
end

#populate_ids(ids) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/card/cache/populate.rb', line 5

def populate_ids ids
  # use ids to look up names
  results = Lexicon.cache.read_multi(ids.map(&:to_s)).values
  names = []
  pairs = []
  results.each do |result|
    result.is_a?(String) ? (names << result) : (pairs << result)
  end

  if pairs.any?
    populate_ids pairs.flatten
    names += pairs.map(&:cardname)
  end

  # use keys to look up
  populate_names names
end

#populate_names(names) ⇒ Object



23
24
25
26
# File 'lib/card/cache/populate.rb', line 23

def populate_names names
  keys = names.map { |n| n.to_name.key }
  Card.cache.read_multi keys
end