Class: Card::Rule::PreferenceCache
- Defined in:
- lib/card/rule/preference_cache.rb
Overview
a preference is a user-specific rule. This caches all preferences in the deck
Constant Summary collapse
- USER_ID_CACHE_KEY =
"USER_IDS".freeze
Class Method Summary collapse
- .clear ⇒ Object
- .lookup_key(row) ⇒ Object
- .lookup_key_without_user ⇒ Object
- .populate ⇒ Object
- .populate_user_ids ⇒ Object
- .rows ⇒ Object
- .user_id_hash ⇒ Object
- .user_ids ⇒ Object
Methods inherited from Cache
Class Method Details
.clear ⇒ Object
57 58 59 60 |
# File 'lib/card/rule/preference_cache.rb', line 57 def clear super Card.cache.write USER_ID_CACHE_KEY, nil end |
.lookup_key(row) ⇒ Object
68 69 70 71 72 |
# File 'lib/card/rule/preference_cache.rb', line 68 def lookup_key row return unless (base = lookup_key_without_user row) "#{base}+#{row['user_id']}" end |
.lookup_key_without_user ⇒ Object
66 |
# File 'lib/card/rule/preference_cache.rb', line 66 alias_method :lookup_key_without_user, :lookup_key |
.populate ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/card/rule/preference_cache.rb', line 37 def populate @rows = nil super.tap do populate_user_ids @rows = nil end end |
.populate_user_ids ⇒ Object
45 46 47 |
# File 'lib/card/rule/preference_cache.rb', line 45 def populate_user_ids Card.cache.write USER_ID_CACHE_KEY, user_id_hash end |
.rows ⇒ Object
62 63 64 |
# File 'lib/card/rule/preference_cache.rb', line 62 def rows @rows ||= super end |
.user_id_hash ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/card/rule/preference_cache.rb', line 49 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
33 34 35 |
# File 'lib/card/rule/preference_cache.rb', line 33 def user_ids Card.cache.read(USER_ID_CACHE_KEY) || (populate && user_ids) end |