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



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

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)


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

def codehash
  @codehash ||= load_codehash
end

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

Returns:

  • (Boolean)


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

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

.generate_id_constantsObject



92
93
94
95
96
97
98
99
100
# File 'lib/card/codename.rb', line 92

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)


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

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

.name(codename = nil) ⇒ Object



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

def name codename=nil
  return super() if codename.nil?

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

.name!(codename) ⇒ Card::Name

Parameters:

  • codename (Symbol, String)

Returns:



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

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

.reset_cacheObject

clear cache both locally and in cache



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

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