Class: Card::Codename
Overview
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
Class Method Summary collapse
-
.[](codename) ⇒ Symbol
returns codename for id and id for codename.
-
.card(codename) ⇒ Card
-
.codehash ⇒ Hash
A hash of two hashes: - the c2i hash has codenames (symbols) as keys and ids (integers) as values - the i2c hash has the opposite.
-
.exist?(codename) ⇒ True/False
(also: exists?)
-
.generate_id_constants ⇒ Object
Creates ruby constants for codenames.
-
.id(codename) ⇒ Integer
-
.id!(codename) ⇒ Integer
-
.ids ⇒ Array<Integer>
List of ids of cards with codenames.
-
.name(codename) ⇒ Card::Name
-
.name!(codename) ⇒ Card::Name
-
.process_codenames ⇒ Hash
Queries the database and generates a “codehash” (see #codehash).
-
.recode(oldcode, newcode) ⇒ Object
Update a codenamed card’s codename.
-
.reset_cache ⇒ Object
clear codename cache both in local variable and in temporary and shared caches.
Class Method Details
.[](codename) ⇒ Symbol
returns codename for id and id for codename
38 39 40 41 42 43 44 45 |
# File 'card/lib/card/codename.rb', line 38 def [] codename case codename when Integer codehash[:i2c][codename] when Symbol, String codehash[:c2i].key?(codename.to_sym) ? codename.to_sym : nil end end |
.card(codename) ⇒ Card
66 67 68 69 70 71 72 |
# File 'card/lib/card/codename.rb', line 66 def card codename if (card_id = id(codename)) Card[card_id] elsif block_given? yield end end |
.codehash ⇒ Hash
A hash of two hashes: - the c2i hash has codenames (symbols) as keys and ids (integers) as values - the i2c hash has the opposite.
138 139 140 |
# File 'card/lib/card/codename.rb', line 138 def codehash @codehash ||= load_codehash end |
.exist?(codename) ⇒ True/False Also known as: exists?
76 77 78 |
# File 'card/lib/card/codename.rb', line 76 def exist? codename id(codename).present? end |
.generate_id_constants ⇒ Object
Creates ruby constants for codenames. Eg, if a card has the codename gibbon, then Card::GibbonID will contain the id for that card.
106 107 108 109 110 |
# File 'card/lib/card/codename.rb', line 106 def generate_id_constants codehash[:c2i].each do |codename, id| Card.const_get_or_set("#{codename.to_s.camelize}ID") { id } end end |
.id(codename) ⇒ Integer
49 50 51 52 53 54 55 56 |
# File 'card/lib/card/codename.rb', line 49 def id codename case codename when Symbol, String codehash[:c2i][codename.to_sym] when Integer codehash[:i2c].key?(codename) ? codename : nil end end |
.id!(codename) ⇒ Integer
89 90 91 |
# File 'card/lib/card/codename.rb', line 89 def id! codename id(codename) || unknown_codename!(codename) end |
.ids ⇒ Array<Integer>
Returns list of ids of cards with codenames.
100 101 102 |
# File 'card/lib/card/codename.rb', line 100 def ids codehash[:i2c].keys end |
.name(codename) ⇒ Card::Name
60 61 62 |
# File 'card/lib/card/codename.rb', line 60 def name codename (card_id = id codename) && Lexicon.name(card_id) end |
.name!(codename) ⇒ Card::Name
95 96 97 |
# File 'card/lib/card/codename.rb', line 95 def name! codename (card_id = id! codename) && Lexicon.name(card_id) end |
.process_codenames ⇒ Hash
Queries the database and generates a “codehash” (see #codehash).
It also seeds the Card and Lexicon caches with codename details
126 127 128 129 130 131 132 |
# File 'card/lib/card/codename.rb', line 126 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 |
.recode(oldcode, newcode) ⇒ Object
Update a codenamed card’s codename
113 114 115 116 117 118 119 |
# File 'card/lib/card/codename.rb', line 113 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 |