Class: Card::Rule::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/card/rule/cache.rb

Direct Known Subclasses

PreferenceCache, ReadRuleCache

Class Method Summary collapse

Class Method Details

.clearObject



35
36
37
# File 'lib/card/rule/cache.rb', line 35

def clear
  Card.cache.write cache_key, nil
end

.lookup_hashObject



39
40
41
42
43
44
45
# File 'lib/card/rule/cache.rb', line 39

def lookup_hash
  rows.each_with_object({}) do |row, hash|
    next unless (key = lookup_key row)

    hash[key] = row["rule_id"].to_i
  end
end

.lookup_key(row) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/card/rule/cache.rb', line 47

def lookup_key row
  return false unless (setting_code = setting_code(row))

  anchor_id = row["anchor_id"]
  return false unless (pattern_code = pattern_code(anchor_id, row))

  [anchor_id, pattern_code, setting_code].compact.map(&:to_s).join "+"
end

.pattern_code(anchor_id, row) ⇒ Object



56
57
58
59
# File 'lib/card/rule/cache.rb', line 56

def pattern_code anchor_id, row
  set_class_id = anchor_id.nil? ? row["set_id"] : row["set_tag_id"]
  Card::Codename[set_class_id.to_i]
end

.populateObject



31
32
33
# File 'lib/card/rule/cache.rb', line 31

def populate
  Card.cache.write cache_key, lookup_hash
end

.readObject



27
28
29
# File 'lib/card/rule/cache.rb', line 27

def read
  Card.cache.read(cache_key) || populate
end

.rowsObject



65
66
67
# File 'lib/card/rule/cache.rb', line 65

def rows
  Card.connection.select_all sql
end

.setting_code(row) ⇒ Object



61
62
63
# File 'lib/card/rule/cache.rb', line 61

def setting_code row
  Card::Codename[row["setting_id"].to_i]
end