Module: Card::Mark

Included in:
Card
Defined in:
lib/card/mark.rb

Overview

Card::Mark provides class methods for Card to translate all different kind of card identifiers to card objects.

Constant Summary collapse

ID_MARK_RE =
/^~(?<id>\d+)$/.freeze
CODENAME_MARK_RE =
/^:(?<codename>\w+)$/.freeze

Instance Method Summary collapse

Instance Method Details

#cardish(cardish) ⇒ Object

translates marks (and other inputs) into a Card

Parameters:

Returns:

  • Card



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

def cardish cardish
  if cardish.is_a? Card
    cardish
  else
    fetch cardish, new: {}
  end
end

#id_from_string(mark) ⇒ Integer or nil

translates string identifiers into an id:

- string id notation (eg "~75")
- string codename notation (eg ":options")

Parameters:

  • mark (String)

Returns:

  • (Integer or nil)


35
36
37
38
39
40
# File 'lib/card/mark.rb', line 35

def id_from_string mark
  case mark
  when ID_MARK_RE       then Regexp.last_match[:id].to_i
  when CODENAME_MARK_RE then Card::Codename.id! Regexp.last_match[:codename]
  end
end

#id_or_name(parts) ⇒ Integer or Card::Name

translates various inputs into either an id or a name.

Parameters:

  • parts (Array<Symbol, Integer, String, Card::Name, Card>)

    a mark or mark parts

Returns:



23
24
25
26
27
# File 'lib/card/mark.rb', line 23

def id_or_name parts
  mark = parts.flatten
  mark = mark.first if mark.size <= 1
  id_from_mark(mark) || Card::Name[mark]
end

#name_from_id_from_string(id, string) ⇒ Object



42
43
44
# File 'lib/card/mark.rb', line 42

def name_from_id_from_string id, string
  fetch_name(id) { bad_mark string }
end