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)


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



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

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)


66
67
68
# File 'lib/card/codename.rb', line 66

def codehash
  @codehash ||= load_codehash
end

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

Returns:

  • (Boolean)


58
59
60
# File 'lib/card/codename.rb', line 58

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

.generate_id_constantsObject



88
89
90
91
92
93
94
95
96
# File 'lib/card/codename.rb', line 88

def generate_id_constants
  # If a card has the codename _example_, then Card::ExampleID will
  # return the id for that card.
  codehash.each do |codename, id|
    next unless codename.is_a?(Symbol) && !codename.to_s.match?(/\W/)

    id_constant codename, id
  end
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)


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

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

.name(codename = nil) ⇒ Object



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

def name codename=nil
  id(codename)&.cardname
end

.name!(codename) ⇒ Card::Name

Parameters:

  • codename (Symbol, String)

Returns:



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

def name! codename
  id!(codename)&.cardname
end

.reset_cacheObject

clear cache both locally and in cache



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

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