Module: Card::Set::All::Rules::ClassMethods

Defined in:
tmpsets/set/mod003-core/all/rules.rb

Instance Method Summary collapse

Instance Method Details

#add_preference_hash_values(key, rule_id, user_id) ⇒ Object



183
184
185
186
187
188
189
# File 'tmpsets/set/mod003-core/all/rules.rb', line 183

def add_preference_hash_values key, rule_id, user_id
  @rule_hash[preference_key(key, user_id)] = rule_id
  @user_ids_hash[key] ||= []
  @user_ids_hash[key] << user_id
  @rule_keys_hash[user_id] ||= []
  @rule_keys_hash[user_id] << key
end

#all_user_ids_with_rule_for(set_card, setting_code) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'tmpsets/set/mod003-core/all/rules.rb', line 195

def all_user_ids_with_rule_for set_card, setting_code
  key =
    if (l = set_card.left) && (r = set_card.right)
      set_class_code = Card::Codename[r.id]
      "#{l.id}+#{set_class_code}+#{setting_code}"
    else
      set_class_code = Card::Codename[set_card.id]
      "#{set_class_code}+#{setting_code}"
    end
  user_ids = user_ids_cache[key] || []
  if user_ids.include? AllID  # rule for all -> return all user ids
    Card.where(type_id: UserID).pluck(:id)
  else
    user_ids
  end
end

#cached_rule_keys_for(user_id) ⇒ Object



191
192
193
# File 'tmpsets/set/mod003-core/all/rules.rb', line 191

def cached_rule_keys_for user_id
  rule_keys_cache[user_id] || []
end

#clear_preference_cacheObject



269
270
271
272
# File 'tmpsets/set/mod003-core/all/rules.rb', line 269

def clear_preference_cache
  # FIXME: too entwined!
  clear_rule_cache
end

#clear_read_rule_cacheObject



300
301
302
# File 'tmpsets/set/mod003-core/all/rules.rb', line 300

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

#clear_rule_cacheObject



263
264
265
266
267
# File 'tmpsets/set/mod003-core/all/rules.rb', line 263

def clear_rule_cache
  write_rule_cache nil
  write_user_ids_cache nil
  write_rule_keys_cache nil
end

#global_setting(name) ⇒ Object



149
150
151
152
153
# File 'tmpsets/set/mod003-core/all/rules.rb', line 149

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

#interpret_preferencesObject



176
177
178
179
180
181
# File 'tmpsets/set/mod003-core/all/rules.rb', line 176

def interpret_preferences
  ActiveRecord::Base.connection.select_all(preference_sql).each do |row|
    next unless (key = rule_cache_key row) && (user_id = row["user_id"])
    add_preference_hash_values key, row["rule_id"].to_i, user_id.to_i
  end
end

#interpret_simple_rulesObject



169
170
171
172
173
174
# File 'tmpsets/set/mod003-core/all/rules.rb', line 169

def interpret_simple_rules
  ActiveRecord::Base.connection.select_all(RULE_SQL).each do |row|
    next unless (key = rule_cache_key row)
    @rule_hash[key] = row["rule_id"].to_i
  end
end

#preference_cards(user_name, setting_code) ⇒ Object



222
223
224
# File 'tmpsets/set/mod003-core/all/rules.rb', line 222

def preference_cards user_name, setting_code
  preference_names(user_name, setting_code).map { |name| Card.fetch name }
end

#preference_key(key, user_id) ⇒ Object



241
242
243
# File 'tmpsets/set/mod003-core/all/rules.rb', line 241

def preference_key key, user_id
  "#{key}+#{user_id}"
end

#preference_names(user_name, setting_code) ⇒ Object



212
213
214
215
216
217
218
219
220
# File 'tmpsets/set/mod003-core/all/rules.rb', line 212

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

#preference_sql(user_id = nil) ⇒ Object

User-specific rule use the pattern user+set+setting



139
140
141
142
143
144
145
146
147
# File 'tmpsets/set/mod003-core/all/rules.rb', line 139

def preference_sql user_id=nil
  user_restriction =
    if user_id
      "users.id = #{user_id}"
    else
      "users.type_id = #{Card::UserID}"
    end
  PREFERENCE_SQL % user_restriction
end

#read_rule_cacheObject



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'tmpsets/set/mod003-core/all/rules.rb', line 286

def read_rule_cache
  Card.cache.read("READRULES") || begin
    hash = {}
    Card.connection.select_all(
      Card::Set::All::Rules::READ_RULE_SQL
    ).each do |row|
      party_id = row["party_id"].to_i
      hash[party_id] ||= []
      hash[party_id] << row["read_rule_id"].to_i
    end
    Card.cache.write "READRULES", hash
  end
end

#rule_cacheObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'tmpsets/set/mod003-core/all/rules.rb', line 226

def rule_cache
  Card.cache.read("RULES") || begin
    @rule_hash = {}
    @user_ids_hash = {}
    @rule_keys_hash = {}

    interpret_simple_rules
    interpret_preferences

    write_user_ids_cache @user_ids_hash
    write_rule_keys_cache @rule_keys_hash
    write_rule_cache @rule_hash
  end
end

#rule_cache_key(row) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'tmpsets/set/mod003-core/all/rules.rb', line 159

def rule_cache_key row
  return false unless (setting_code = Card::Codename[row["setting_id"].to_i])

  anchor_id = row["anchor_id"]
  set_class_id = anchor_id.nil? ? row["set_id"] : row["set_tag_id"]
  return false unless (set_class_code = Card::Codename[set_class_id.to_i])

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

#rule_keys_cacheObject

all keys of user-specific rules for a given user



255
256
257
258
259
260
261
# File 'tmpsets/set/mod003-core/all/rules.rb', line 255

def rule_keys_cache
  Card.cache.read("RULE_KEYS") || begin
    clear_rule_cache
    rule_cache
    @rule_keys_hash
  end
end

#toggle(val) ⇒ Object



155
156
157
# File 'tmpsets/set/mod003-core/all/rules.rb', line 155

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



246
247
248
249
250
251
252
# File 'tmpsets/set/mod003-core/all/rules.rb', line 246

def user_ids_cache
  Card.cache.read("USER_IDS") || begin
    clear_rule_cache
    rule_cache
    @user_ids_hash
  end
end

#write_rule_cache(hash) ⇒ Object



274
275
276
# File 'tmpsets/set/mod003-core/all/rules.rb', line 274

def write_rule_cache hash
  Card.cache.write "RULES", hash
end

#write_rule_keys_cache(hash) ⇒ Object



282
283
284
# File 'tmpsets/set/mod003-core/all/rules.rb', line 282

def write_rule_keys_cache hash
  Card.cache.write "RULE_KEYS", hash
end

#write_user_ids_cache(hash) ⇒ Object



278
279
280
# File 'tmpsets/set/mod003-core/all/rules.rb', line 278

def write_user_ids_cache hash
  Card.cache.write "USER_IDS", hash
end