Module: Card::Lexicon

Defined in:
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

.cacheObject



27
28
29
# File '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 '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



41
42
43
44
45
# File 'lib/card/lexicon.rb', line 41

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 '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

.rescuingObject

this is to address problems whereby renaming errors leave the lexicon broken. NEEDS TESTING



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

def rescuing
  @act_lexes = []
  @act_ids = []
  yield
rescue StandardError => e
  @act_lexes.each { |lex| expire_lex lex }
  @act_ids.each { |id| expire_id id }
  @act_lexes = @act_ids = nil
  raise e
end

.update(card) ⇒ Object



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

def update card
  add card
  expire_lex card.lex_before_act
end