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

.[](codename) ⇒ Symbol

returns codename for id and id for codename

Parameters:

Returns:

  • (Symbol)


28
29
30
31
32
33
34
35
# File 'lib/card/codename.rb', line 28

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



52
53
54
55
56
57
58
# File 'lib/card/codename.rb', line 52

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)


68
69
70
# File 'lib/card/codename.rb', line 68

def codehash
  @codehash ||= load_codehash
end

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

Returns:

  • (Boolean)


60
61
62
# File 'lib/card/codename.rb', line 60

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

.id(codename) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/card/codename.rb', line 37

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)


80
81
82
# File 'lib/card/codename.rb', line 80

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

.name(codename) ⇒ Object



46
47
48
49
50
# File 'lib/card/codename.rb', line 46

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

.name!(codename) ⇒ Card::Name

Parameters:

  • codename (Symbol, String)

Returns:



86
87
88
# File 'lib/card/codename.rb', line 86

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

.reset_cacheObject

clear cache both locally and in cache



73
74
75
76
# File 'lib/card/codename.rb', line 73

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