Class: Card::Codename

Inherits:
Object
  • Object
show all
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, a unique string identifier that will not change even if the card's name does.

The Codename class provides a fast cache for this slow-changing data. Every process maintains a complete cache that is not frequently reset

Generally speaking, codenames are represented by Symbols, names are Strings, and ids are Integers.

Class Method Summary collapse

Class Method Details

.[](key) ⇒ String, Integer

returns codename for id and id for codename

Parameters:

  • key (Integer, String)

Returns:

  • (String, Integer)


23
24
25
26
# File 'lib/card/codename.rb', line 23

def [] key
  return if key.nil?
  codehash[key.is_a?(Integer) ? key : key.to_sym]
end

.codehashHash

a Hash in which String keys have Integer values and vice versa

Returns:

  • (Hash)


30
31
32
# File 'lib/card/codename.rb', line 30

def codehash
  @codehash ||= load_codehash
end

.reset_cacheObject

clear cache both locally and in cache



35
36
37
38
# File 'lib/card/codename.rb', line 35

def reset_cache
  @codehash = nil
  Card.cache.delete "CODEHASH"
end