Class: Card::Codename
- Inherits:
-
Object
- Object
- Card::Codename
- Defined in:
- lib/card/codename.rb
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
37 38 39 40 41 42 43 44 |
# File 'lib/card/codename.rb', line 37 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
65 66 67 68 69 70 71 |
# File 'lib/card/codename.rb', line 65 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.
137 138 139 |
# File 'lib/card/codename.rb', line 137 def codehash @codehash ||= load_codehash end |
.exist?(codename) ⇒ True/False Also known as: exists?
75 76 77 |
# File 'lib/card/codename.rb', line 75 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.
105 106 107 108 109 |
# File 'lib/card/codename.rb', line 105 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
48 49 50 51 52 53 54 55 |
# File 'lib/card/codename.rb', line 48 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
88 89 90 |
# File 'lib/card/codename.rb', line 88 def id! codename id(codename) || unknown_codename!(codename) end |
.ids ⇒ Array<Integer>
Returns list of ids of cards with codenames.
99 100 101 |
# File 'lib/card/codename.rb', line 99 def ids codehash[:i2c].keys end |
.name(codename) ⇒ Card::Name
59 60 61 |
# File 'lib/card/codename.rb', line 59 def name codename (card_id = id codename) && Lexicon.name(card_id) end |
.name!(codename) ⇒ Card::Name
94 95 96 |
# File 'lib/card/codename.rb', line 94 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
125 126 127 128 129 130 131 |
# File 'lib/card/codename.rb', line 125 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
112 113 114 115 116 117 118 |
# File 'lib/card/codename.rb', line 112 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 |