Module: Card::Lexicon

Defined in:
card/lib/card/lexicon.rb

Overview

Translates names to ids and vice versa via a cached “lex” representation: name for simple cards, [left_id, right_id] for compound cards.

Note, unlike Card::Fetch, Card::Lexicon: 1. does NOT distinguish between trashed and untrashed cards. 2. does NOT respect local name changes

Class Method Summary collapse

Class Method Details

.add(card) ⇒ Object



31
32
33
34
35
# File 'card/lib/card/lexicon.rb', line 31

def add card
  lex = card.lex
  cache.write card.id.to_s, lex
  cache.write cache_key(lex), card.id
end

.cacheObject



27
28
29
# File 'card/lib/card/lexicon.rb', line 27

def cache
  Card::Cache[Lexicon]
end

.id(name) ⇒ Integer

param name [String]

Returns:

  • (Integer)


21
22
23
24
25
# File 'card/lib/card/lexicon.rb', line 21

def id name
  return unless name.present?

  (lex = name_to_lex name.to_name) && lex_to_id(lex)
end

.lex_to_name(lex) ⇒ Object

def delete card cache.delete card.id.to_s cache.delete cache_key(card.lex_before_act) end



47
48
49
50
51
# File 'card/lib/card/lexicon.rb', line 47

def lex_to_name lex
  return lex unless lex.is_a? Array

  lex.map { |side_id| name side_id or return }.join(Card::Name.joint).to_name
end

.name(id) ⇒ String

param id [Integer]

Returns:

  • (String)


12
13
14
15
16
17
# File 'card/lib/card/lexicon.rb', line 12

def name id
  return unless id.present?

  name = (lex = id_to_lex id) && lex_to_name(lex)
  (name || "").to_name
end

.update(card) ⇒ Object



37
38
39
40
# File 'card/lib/card/lexicon.rb', line 37

def update card
  add card
  cache.delete cache_key(card.lex_before_act)
end