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
# 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



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

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



54
55
56
57
# File 'lib/card/rule/cache.rb', line 54

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



63
64
65
# File 'lib/card/rule/cache.rb', line 63

def rows
  Card.connection.select_all sql
end

.setting_code(row) ⇒ Object



59
60
61
# File 'lib/card/rule/cache.rb', line 59

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