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+)$/
- CODENAME_MARK_RE =
/^:(?<codename>\w+)$/
Instance Method Summary collapse
- #bad_mark(mark) ⇒ Object
-
#cardish(cardish) ⇒ Object
translates marks (and other inputs) into a Card.
- #id_from_mark(mark) ⇒ Object
-
#id_from_string(mark) ⇒ Integer or nil
translates string identifiers into an id: - string id notation (eg "~75") - string codename notation (eg ":options").
-
#id_or_name(parts) ⇒ Integer or Card::Name
translates various inputs into either an id or a name.
- #name_from_mark(mark) ⇒ Object
Instance Method Details
#bad_mark(mark) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/card/mark.rb', line 50 def bad_mark mark case mark when ID_MARK_RE raise Card::Error::NotFound, "id doesn't exist: #{Regexp.last_match[:id]}" when CODENAME_MARK_RE raise Card::Error::CodenameNotFound, "codename doesn't exist: #{Regexp.last_match[:codename]}" else raise Card::Error, "invalid mark: #{mark}" end end |
#cardish(cardish) ⇒ Object
translates marks (and other inputs) into a 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_mark(mark) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/card/mark.rb', line 29 def id_from_mark mark case mark when Integer then mark when Symbol then Card::Codename.id! mark when String then id_from_string mark 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")
43 44 45 46 47 48 |
# File 'lib/card/mark.rb', line 43 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.
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) || name_from_mark(mark) end |