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

.[](codename) ⇒ Symbol

returns codename for id and id for codename

Parameters:

Returns:

  • (Symbol)


31
32
33
34
35
36
37
38
# File 'lib/card/codename.rb', line 31

def [] codename
  case codename
  when Integer
    codehash[codename]
  when Symbol, String
    codehash.key?(codename.to_sym) ? codename.to_sym : nil
  end
end

.card(codename) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/card/codename.rb', line 55

def card codename
  if (card_id = id(codename))
    Card[card_id]
  elsif block_given?
    yield
  end
end

.codehashHash

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

Returns:

  • (Hash)


71
72
73
# File 'lib/card/codename.rb', line 71

def codehash
  @codehash ||= load_codehash
end

.exist?(codename) ⇒ Boolean Also known as: exists?

Returns:

  • (Boolean)


63
64
65
# File 'lib/card/codename.rb', line 63

def exist? codename
  id(codename).present?
end

.id(codename) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/card/codename.rb', line 40

def id codename
  case codename
  when Symbol, String
    codehash[codename.to_sym]
  when Integer
    codehash.key?(codename) ? codename : nil
  end
end

.id!(codename) ⇒ Integer

Parameters:

  • codename (Symbol, String)

Returns:

  • (Integer)


83
84
85
# File 'lib/card/codename.rb', line 83

def id! codename
  id(codename) || unknown_codename!(codename)
end

.name(codename) ⇒ Object



49
50
51
52
53
# File 'lib/card/codename.rb', line 49

def name codename
  name! codename
rescue Error::CodenameNotFound => _e
  yield if block_given?
end

.name!(codename) ⇒ Card::Name

Parameters:

  • codename (Symbol, String)

Returns:



89
90
91
# File 'lib/card/codename.rb', line 89

def name! codename
  Card::Name[codename.to_sym]
end

.reset_cacheObject

clear cache both locally and in cache



76
77
78
79
# File 'lib/card/codename.rb', line 76

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