class Card::Codename
{Card}‘s names can be changed, and therefore names should not be directly mentioned in code, lest a name change break the application.
Instead, a {Card} that needs specific code manipulations should be given a {Codename}, which will not change even if the card’s name does.
An administrator might add to the Company card via the RESTful web API with a url like
/update/CARDNAME?card[codename]=CODENAME
…or via the api like
Card[CARDNAME].update! codename: CODENAME
Generally speaking, codenames are represented by Symbols.
The {Codename} class provides a fast cache for this slow-changing data. Every process maintains a complete cache that is not frequently reset
Public Class Methods
Source
# File lib/card/codename.rb, line 36 def [] codename case codename when Integer codehash[:i2c][codename] when Symbol, String codehash[:c2i].key?(codename.to_sym) ? codename.to_sym : nil end end
returns codename for id and id for codename @param codename [Integer, Symbol, String, Card::Name
] @return [Symbol]
Source
# File lib/card/codename.rb, line 64 def card codename if (card_id = id(codename)) Card[card_id] elsif block_given? yield end end
@param codename [Symbol, String] @return [Card]
Source
# File lib/card/codename.rb, line 136 def codehash @codehash ||= load_codehash end
A hash of two hashes:
-
the c2i hash has codenames (symbols) as keys and ids (integers) as values
-
the i2c hash has the opposite.
@return [Hash] (the codehash)
Source
# File lib/card/codename.rb, line 74 def exist? codename id(codename).present? end
@param codename [Symbol, String] @return [True/False]
Source
# File lib/card/codename.rb, line 104 def generate_id_constants codehash[:c2i].each do |codename, id| Card.const_get_or_set("#{codename.to_s.camelize}ID") { id } end end
Creates ruby constants for codenames. Eg, if a card has the codename gibbon, then Card::GibbonID will contain the id for that card.
Source
# File lib/card/codename.rb, line 47 def id codename case codename when Symbol, String codehash[:c2i][codename.to_sym] when Integer codehash[:i2c].key?(codename) ? codename : nil end end
@param codename [Symbol, String] @return [Integer]
Source
# File lib/card/codename.rb, line 87 def id! codename id(codename) || unknown_codename!(codename) end
@param codename [Symbol, String] @return [Integer]
Source
# File lib/card/codename.rb, line 98 def ids codehash[:i2c].keys end
@return [Array<Integer>] list of ids of cards with codenames
Source
# File lib/card/codename.rb, line 58 def name codename (card_id = id codename) && Lexicon.name(card_id) end
@param codename [Symbol, String] @return [Card::Name]
Source
# File lib/card/codename.rb, line 93 def name! codename (card_id = id! codename) && Lexicon.name(card_id) end
@param codename [Symbol, String] @return [Card::Name]
Source
# File lib/card/codename.rb, line 124 def process_codenames codenamed_cards.each_with_object(c2i: {}, i2c: {}) do |card, hash| hash[:c2i][card.codename] = card.id hash[:i2c][card.id] = card.codename seed_caches card end end
Queries the database and generates a “codehash” (see codehash).
It also seeds the Card
and Lexicon
caches with codename details
@return [Hash] (the codehash)
Source
# File lib/card/codename.rb, line 111 def recode oldcode, newcode return unless id(oldcode) && !id(newcode) Rails.logger.info "recode #{oldcode}, #{newcode}" Card.where(codename: oldcode).take.update_column :codename, newcode reset_cache end
Update a codenamed card’s codename
Source
# File lib/card/codename.rb, line 80 def reset_cache @codehash = nil ::Card.cache.delete "CODENAMES" end
clear codename cache both in local variable and in temporary and shared caches
Private Class Methods
Source
# File lib/card/codename.rb, line 142 def codenamed_cards Card.where "codename is not NULL" end
Source
# File lib/card/codename.rb, line 147 def load_codehash Card.cache.fetch("CODENAMES") { process_codenames } end
generate Hash for @codehash and put it in the cache
Source
# File lib/card/codename.rb, line 151 def seed_caches card return if card.left_id Card::Lexicon.write card.id, card.name, card.lex # Card.cache.write card.key, card end
Source
# File lib/card/codename.rb, line 158 def unknown_codename! mark raise Card::Error::CodenameNotFound, ::I18n.t(:lib_exception_unknown_codename, codename: mark) end