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

[](codename) click to toggle source

returns codename for id and id for codename @param codename [Integer, Symbol, String, Card::Name] @return [Symbol]

# 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) { || ... } click to toggle source

@param codename [Symbol, String] @return [Card]

# 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() click to toggle source

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)

# File lib/card/codename.rb, line 137
def codehash
  @codehash ||= load_codehash
end
exist?(codename) click to toggle source

@param codename [Symbol, String] @return [True/False]

# File lib/card/codename.rb, line 75
def exist? codename
  id(codename).present?
end
Also aliased as: exists?
exists?(codename)
Alias for: exist?
generate_id_constants() click to toggle source

Creates ruby constants for codenames. Eg, if a card has the codename gibbon, then Card::GibbonID will contain the id for that card.

# 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) click to toggle source

@param codename [Symbol, String] @return [Integer]

# 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) click to toggle source

@param codename [Symbol, String] @return [Integer]

# File lib/card/codename.rb, line 88
def id! codename
  id(codename) || unknown_codename!(codename)
end
ids() click to toggle source

@return [Array<Integer>] list of ids of cards with codenames

# File lib/card/codename.rb, line 99
def ids
  codehash[:i2c].keys
end
name(codename) click to toggle source

@param codename [Symbol, String] @return [Card::Name]

# File lib/card/codename.rb, line 59
def name codename
  (card_id = id codename) && Lexicon.name(card_id)
end
name!(codename) click to toggle source

@param codename [Symbol, String] @return [Card::Name]

# File lib/card/codename.rb, line 94
def name! codename
  (card_id = id! codename) && Lexicon.name(card_id)
end
process_codenames() click to toggle source

Queries the database and generates a “codehash” (see codehash).

It also seeds the Card and Lexicon caches with codename details

@return [Hash] (the codehash)

# 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) click to toggle source

Update a codenamed card’s codename

# 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
reset_cache() click to toggle source

clear codename cache both in local variable and in temporary and shared caches

# File lib/card/codename.rb, line 81
def reset_cache
  @codehash = nil
  ::Card.cache.delete "CODENAMES"
end

Private Class Methods

codenamed_cards() click to toggle source
# File lib/card/codename.rb, line 143
def codenamed_cards
  Card.where "codename is not NULL"
end
load_codehash() click to toggle source

generate Hash for @codehash and put it in the cache

# File lib/card/codename.rb, line 148
def load_codehash
  Card.cache.fetch("CODENAMES") { process_codenames }
end
seed_caches(card) click to toggle source
# File lib/card/codename.rb, line 152
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
unknown_codename!(mark) click to toggle source
# File lib/card/codename.rb, line 159
def unknown_codename! mark
  raise Card::Error::CodenameNotFound,
        ::I18n.t(:lib_exception_unknown_codename, codename: mark)
end