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)


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

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

.card(codename) ⇒ Card

Parameters:

  • codename (Symbol, String)

Returns:



66
67
68
69
70
71
72
# File 'lib/card/codename.rb', line 66

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

.codehashHash

A hash of two hashes:

  • the c2i hash has codenames (symbols) as keys and ids (integers) as values

  • the i2c hash has the opposite.

Returns:

  • (Hash)

    (the codehash)



138
139
140
# File 'lib/card/codename.rb', line 138

def codehash
  @codehash ||= load_codehash
end

.exist?(codename) ⇒ True/False Also known as: exists?

Parameters:

  • codename (Symbol, String)

Returns:

  • (True/False)


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

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

.generate_id_constantsObject

Creates ruby constants for codenames. Eg, if a card has the codename gibbon, then Card::GibbonID will contain the id for that card.



106
107
108
109
110
# File 'lib/card/codename.rb', line 106

def generate_id_constants
  codehash[:c2i].each do |codename, id|
    Card.const_get_or_set("#{codename.to_s.camelize}ID") { id }
  end
end

.id(codename) ⇒ Integer

Parameters:

  • codename (Symbol, String)

Returns:

  • (Integer)


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

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

.id!(codename) ⇒ Integer

Parameters:

  • codename (Symbol, String)

Returns:

  • (Integer)


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

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

.idsArray<Integer>

Returns list of ids of cards with codenames.

Returns:

  • (Array<Integer>)

    list of ids of cards with codenames



100
101
102
# File 'lib/card/codename.rb', line 100

def ids
  codehash[:i2c].keys
end

.name(codename) ⇒ Card::Name

Parameters:

  • codename (Symbol, String)

Returns:



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

def name codename
  (card_id = id codename) && Lexicon.name(card_id)
end

.name!(codename) ⇒ Card::Name

Parameters:

  • codename (Symbol, String)

Returns:



95
96
97
# File 'lib/card/codename.rb', line 95

def name! codename
  (card_id = id! codename) && Lexicon.name(card_id)
end

.process_codenamesHash

Queries the database and generates a “codehash” (see #codehash).

It also seeds the Card and Lexicon caches with codename details

Returns:

  • (Hash)

    (the codehash)



126
127
128
129
130
131
132
# File 'lib/card/codename.rb', line 126

def process_codenames
  codenamed_cards.each_with_object(c2i: {}, i2c: {}) do |card, hash|
    hash[:c2i][card.codename] = card.id
    hash[:i2c][card.id] = card.codename
    seed_caches card
  end
end

.recode(oldcode, newcode) ⇒ Object

Update a codenamed card’s codename



113
114
115
116
117
118
119
# File 'lib/card/codename.rb', line 113

def recode oldcode, newcode
  return unless id(oldcode) && !id(newcode)

  Rails.logger.info "recode #{oldcode}, #{newcode}"
  Card.where(codename: oldcode).take.update_column :codename, newcode
  reset_cache
end

.reset_cacheObject

clear codename cache both in local variable and in temporary and shared caches



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

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