class Card::Name

The {Cardname} class provides generalized of Card naming patterns (compound names, key-based variants, etc) and can be used independently of Card objects.

{Card::Name} adds support for deeper integration with Card objects

Constants

CODENAME_MARK_RE
ID_MARK_RE

Public Class Methods

[](*cardish) click to toggle source

@return [Card::Name]

# File lib/card/name.rb, line 19
def [] *cardish
  cardish = cardish.first if cardish.size <= 1
  from_cardish(cardish) || unsupported_class!(cardish)
end
compose(parts) click to toggle source

interprets symbols/integers as codenames/ids

# File lib/card/name.rb, line 47
def compose parts
  new_from_parts(parts) { |part| self[part] }
end
id_from_string(string) click to toggle source

translates string identifiers into an id:

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

@param string [String] @return [Integer or nil]

# File lib/card/name.rb, line 57
def id_from_string string
  case string
  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_from_string!(string) click to toggle source
# File lib/card/name.rb, line 64
def id_from_string! string
  return unless (id = id_from_string string)

  Lexicon.name(id) ? id : bad_mark(string)
end
new(str, validated_parts=nil) click to toggle source
Calls superclass method
# File lib/card/name.rb, line 32
def new str, validated_parts=nil
  return compose str if str.is_a?(Array)

  str = str.to_s

  if !validated_parts && str.include?(joint)
    new_from_compound_string str
  elsif (id = id_from_string str)  # handles ~[id] and :[codename]
    from_id_from_string id, str
  else
    super str
  end
end
params() click to toggle source
# File lib/card/name.rb, line 28
def params
  Card::Env.params
end
session() click to toggle source
# File lib/card/name.rb, line 24
def session
  Card::Auth.current.name # also_yuck
end

Private Class Methods

bad_mark(string) click to toggle source
# File lib/card/name.rb, line 101
def bad_mark string
  case string
  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: #{string}"
  end
end
from_cardish(cardish) click to toggle source
# File lib/card/name.rb, line 72
def from_cardish cardish
  case cardish
  when Card             then cardish.name
  when Integer          then Lexicon.name cardish
  when Symbol           then Codename.name! cardish
  when Array            then compose cardish
  when String, NilClass then new cardish
  end
end
from_id_from_string(id, str) click to toggle source
# File lib/card/name.rb, line 96
def from_id_from_string id, str
  name = Lexicon.name id
  name.present? ? name : bad_mark(str)
end
new_from_compound_string(string) click to toggle source
# File lib/card/name.rb, line 86
def new_from_compound_string string
  parts = Cardname.split_parts string
  new_from_parts(parts) { |part| new part }
end
new_from_parts(parts, &block) click to toggle source
# File lib/card/name.rb, line 91
def new_from_parts parts, &block
  name_parts = parts.flatten.map(&block)
  new name_parts.join(joint), true
end
unsupported_class!(cardish) click to toggle source
# File lib/card/name.rb, line 82
def unsupported_class! cardish
  raise ArgumentError, "#{cardish.class} not supported as name identifier"
end

Public Instance Methods

rstar?() click to toggle source
# File lib/card/name.rb, line 118
def rstar?
  right && right[0, 1] == "*"
end
star?() click to toggle source
# File lib/card/name.rb, line 114
def star?
  simple? && s[0, 1] == "*"
end