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, 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_attributes! 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

Class Method Details

.[](key) ⇒ String, Integer

returns codename for id and id for codename

Parameters:

  • key (Integer, String)

Returns:

  • (String, Integer)


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

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)


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

def codehash
  @codehash ||= load_codehash
end

.reset_cacheObject

clear cache both locally and in cache



40
41
42
43
# File 'lib/card/codename.rb', line 40

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