Class: Card::Rule::PreferenceCache
- Inherits:
-
Cache
- Object
- Cache
- Card::Rule::PreferenceCache
show all
- Defined in:
- lib/card/rule/preference_cache.rb
Constant Summary
collapse
- USER_ID_CACHE_KEY =
"USER_IDS".freeze
Class Method Summary
collapse
Methods inherited from Cache
lookup_hash, pattern_code, read, setting_code
Class Method Details
.lookup_key(row) ⇒ Object
66
67
68
69
70
|
# File 'lib/card/rule/preference_cache.rb', line 66
def lookup_key row
return unless (base = lookup_key_without_user row)
"#{base}+#{row['user_id']}"
end
|
.lookup_key_without_user ⇒ Object
64
|
# File 'lib/card/rule/preference_cache.rb', line 64
alias_method :lookup_key_without_user, :lookup_key
|
.populate ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/card/rule/preference_cache.rb', line 35
def populate
@rows = nil
super.tap do
populate_user_ids
@rows = nil
end
end
|
.populate_user_ids ⇒ Object
43
44
45
|
# File 'lib/card/rule/preference_cache.rb', line 43
def populate_user_ids
Card.cache.write USER_ID_CACHE_KEY, user_id_hash
end
|
.rows ⇒ Object
60
61
62
|
# File 'lib/card/rule/preference_cache.rb', line 60
def rows
@rows ||= super
end
|
.user_id_hash ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/card/rule/preference_cache.rb', line 47
def user_id_hash
rows.each_with_object({}) do |row, hash|
key = lookup_key_without_user row
hash[key] ||= []
hash[key] << row["user_id"]
end
end
|
.user_ids ⇒ Object
31
32
33
|
# File 'lib/card/rule/preference_cache.rb', line 31
def user_ids
Card.cache.read(USER_ID_CACHE_KEY) || (populate && user_ids)
end
|