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: does NOT return local name changes until stored

Class Method Summary collapse

Class Method Details

.cacheObject



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

def cache
  Card::Cache[Lexicon]
end

.cache_key(lex) ⇒ Object



46
47
48
# File 'lib/card/lexicon.rb', line 46

def cache_key lex
  "L-#{lex.is_a?(Array) ? lex.join('-') : lex.to_name.key}"
end

.delete(card) ⇒ Object



35
36
37
38
# File 'lib/card/lexicon.rb', line 35

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

.id(name) ⇒ Integer

param name [String]

Returns:

  • (Integer)


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

def id name
  return unless name.present?

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

.lex_query(lex) ⇒ Object



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

def lex_query lex
  if lex.is_a?(Array)
    { left_id: lex.first, right_id: lex.last }
  else
    { key: lex.to_name.key }
  end
end

.lex_to_name(lex) ⇒ Object



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

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)


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

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



60
61
62
63
64
65
66
67
68
69
# File 'lib/card/lexicon.rb', line 60

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



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

def update card
  add card
  expire_lex card.lex_before_act
end

.write(id, name, lex) ⇒ Object



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

def write id, name, lex
  cache.write id.to_s, name
  cache.write cache_key(lex), id
end

.write_to_temp_cache(id, name, lex) ⇒ Object



71
72
73
74
# File 'lib/card/lexicon.rb', line 71

def write_to_temp_cache id, name, lex
  cache.temp.write id.to_s, name if id.present?
  cache.temp.write cache_key(lex), id if lex
end