Module: Card::Rule

Defined in:
mod/core/lib/card/rule.rb

Overview

Optimized handling of card "rules" (Set+Setting) and preferences.

Constant Summary collapse

RULE_SQL =
%(
  SELECT
    rules.id      AS rule_id,
    settings.id   AS setting_id,
    sets.id       AS set_id,
    sets.left_id  AS anchor_id,
    sets.right_id AS set_tag_id
  FROM cards rules
  JOIN cards sets     ON rules.left_id  = sets.id
  JOIN cards settings ON rules.right_id = settings.id
  WHERE     sets.type_id = #{SetID}
    AND settings.type_id = #{SettingID}
    AND (settings.codename != 'follow' OR rules.db_content != '')
    AND    rules.trash is false
    AND     sets.trash is false
    AND settings.trash is false;
).freeze
READ_RULE_SQL =

FIXME: "follow" hardcoded above

%(
  SELECT
    refs.referee_id AS party_id,
    read_rules.id   AS read_rule_id
  FROM cards read_rules
  JOIN card_references refs ON refs.referer_id    = read_rules.id
  JOIN cards sets           ON read_rules.left_id = sets.id
  WHERE read_rules.right_id = #{ReadID}
    AND       sets.type_id  = #{SetID}
    AND read_rules.trash is false
    AND       sets.trash is false;
).freeze
PREFERENCE_SQL =
%(
  SELECT
    preferences.id AS rule_id,
    settings.id    AS setting_id,
    sets.id        AS set_id,
    sets.left_id   AS anchor_id,
    sets.right_id  AS set_tag_id,
    users.id       AS user_id
  FROM cards preferences
  JOIN cards user_sets ON preferences.left_id  = user_sets.id
  JOIN cards settings  ON preferences.right_id = settings.id
  JOIN cards users     ON user_sets.right_id   = users.id
  JOIN cards sets      ON user_sets.left_id    = sets.id
  WHERE sets.type_id     = #{SetID}
    AND settings.type_id = #{SettingID}
    AND (%s or users.codename = 'all')
    AND sets.trash        is false
    AND settings.trash    is false
    AND users.trash       is false
    AND user_sets.trash   is false
    AND preferences.trash is false;
).freeze

Class Method Summary collapse

Class Method Details

.all_user_ids_with_rule_for(set_card, setting_code) ⇒ Object



112
113
114
115
116
# File 'mod/core/lib/card/rule.rb', line 112

def all_user_ids_with_rule_for set_card, setting_code
  cache_key = "#{user_cache_key_base set_card}+#{setting_code}"
  user_ids = user_ids_cache[cache_key] || []
  user_ids.include?(AllID) ? all_user_ids : user_ids
end

.clear_preference_cacheObject



95
96
97
98
# File 'mod/core/lib/card/rule.rb', line 95

def clear_preference_cache
  # FIXME: too entwined!
  clear_rule_cache
end

.clear_read_rule_cacheObject



100
101
102
# File 'mod/core/lib/card/rule.rb', line 100

def clear_read_rule_cache
  Card.cache.write "READRULES", nil
end

.clear_rule_cacheObject



89
90
91
92
93
# File 'mod/core/lib/card/rule.rb', line 89

def clear_rule_cache
  write_rule_cache nil
  write_user_ids_cache nil
  write_rule_keys_cache nil
end

.global_setting(name) ⇒ Object



61
62
63
64
65
# File 'mod/core/lib/card/rule.rb', line 61

def global_setting name
  Auth.as_bot do
    (card = Card[name]) && !card.db_content.strip.empty? && card.db_content
  end
end

.preference_names(user_name, setting_code) ⇒ Object



104
105
106
107
108
109
110
# File 'mod/core/lib/card/rule.rb', line 104

def preference_names user_name, setting_code
  Card.search({ right: { codename: setting_code },
                left: { left: { type_id: SetID },
                        right: user_name },
                return: :name },
              "preference cards for user: #{user_name}")
end

.read_rule_cacheObject



75
76
77
# File 'mod/core/lib/card/rule.rb', line 75

def read_rule_cache
  Card.cache.read("READRULES") || populate_read_rule_cache
end

.rule_cacheObject



71
72
73
# File 'mod/core/lib/card/rule.rb', line 71

def rule_cache
  Card.cache.read("RULES") || populate_rule_caches
end

.rule_keys_cacheObject

all keys of user-specific rules for a given user



85
86
87
# File 'mod/core/lib/card/rule.rb', line 85

def rule_keys_cache
  Card.cache.read("RULE_KEYS") || fresh_rule_cache { @rule_keys_hash }
end

.toggle(val) ⇒ Object



67
68
69
# File 'mod/core/lib/card/rule.rb', line 67

def toggle val
  val.to_s.strip == "1"
end

.user_ids_cacheObject

all users that have a user-specific rule for a given rule key



80
81
82
# File 'mod/core/lib/card/rule.rb', line 80

def user_ids_cache
  Card.cache.read("USER_IDS") || fresh_rule_cache { @user_ids_hash }
end