Class: Card::Codename
- Inherits:
-
Object
- Object
- Card::Codename
- 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
-
.[](codename) ⇒ Symbol
returns codename for id and id for codename.
- .card(codename) ⇒ Object
-
.codehash ⇒ Hash
a Hash in which Symbol keys have Integer values and vice versa.
- .exist?(codename) ⇒ Boolean (also: exists?)
- .generate_id_constants ⇒ Object
- .id(codename) ⇒ Object
- .id!(codename) ⇒ Integer
- .name(codename = nil) ⇒ Object
- .name!(codename) ⇒ Card::Name
-
.reset_cache ⇒ Object
clear cache both locally and in cache.
Class Method Details
.[](codename) ⇒ Symbol
returns codename for id and id for codename
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 |
.codehash ⇒ Hash
a Hash in which Symbol keys have Integer values and vice versa
66 67 68 |
# File 'lib/card/codename.rb', line 66 def codehash @codehash ||= load_codehash end |
.exist?(codename) ⇒ Boolean Also known as: exists?
58 59 60 |
# File 'lib/card/codename.rb', line 58 def exist? codename id(codename).present? end |
.generate_id_constants ⇒ Object
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
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
84 85 86 |
# File 'lib/card/codename.rb', line 84 def name! codename id!(codename)&.cardname end |