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
Source
# File lib/card/name.rb, line 19 def [] *cardish cardish = cardish.first if cardish.size <= 1 from_cardish(cardish) || unsupported_class!(cardish) end
@return [Card::Name]
Source
# File lib/card/name.rb, line 47 def compose parts new_from_parts(parts) { |part| self[part] } end
interprets symbols/integers as codenames/ids
Source
# 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
translates string identifiers into an id:
- string id notation (eg "~75") - string codename notation (eg ":options")
@param string [String] @return [Integer or nil]
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
Source
# 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
Calls superclass method
Source
# File lib/card/name.rb, line 24 def session Card::Auth.current.name # also_yuck end
Private Class Methods
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
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
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
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
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
Source
# File lib/card/name.rb, line 82 def unsupported_class! cardish raise ArgumentError, "#{cardish.class} not supported as name identifier" end