Class: Card::Name

Inherits:
SmartName show all
Defined in:
lib/card/name.rb

Constant Summary collapse

RELATIVE_REGEXP =
/\b_(left|right|whole|self|user|main|\d+|L*R?)\b/

Instance Method Summary collapse

Instance Method Details

#absolute?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/card/name.rb', line 105

def absolute?
  !relative?
end

#absolute_name(context_name) ⇒ Object



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

def absolute_name context_name
  to_absolute_name(context_name)
end

#child_of?(context_name) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
# File 'lib/card/name.rb', line 75

def child_of? context_name
  if context_name.present?
    # Do I still equal myself after I've been relativised in the context
    # of context_name?
    relative_name(context_name).key != absolute_name(context_name).key
  else
    s.match(/^\s*\+/)
  end
end

#codeObject



48
49
50
# File 'lib/card/name.rb', line 48

def code
  Card::Codename[Card.fetch_id self]
end

#field(tag_name) ⇒ Object



44
45
46
# File 'lib/card/name.rb', line 44

def field tag_name
  field_name(tag_name).s
end

#field_name(tag_name) ⇒ Object

returns full name for a field



53
54
55
56
57
58
59
60
61
# File 'lib/card/name.rb', line 53

def field_name tag_name
  case tag_name
  when Symbol
    trait_name tag_name
  else
    tag_name = tag_name.to_s[1..-1] if tag_name.to_s[0] == '+'
    [self, tag_name].to_name
  end
end

#field_of?(context_name) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
# File 'lib/card/name.rb', line 85

def field_of? context_name
  if context_name.present?
    child_of?(context_name) && relative_name(context_name).length == 2
  else
    s.match(/^\s*\+[^+]+$/)
  end
end

#relative?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/card/name.rb', line 101

def relative?
  s =~ RELATIVE_REGEXP || starts_with_joint?
end

#relative_field_name(tag_name) ⇒ Object



63
64
65
# File 'lib/card/name.rb', line 63

def relative_field_name tag_name
  field_name(tag_name).relative_name(self)
end

#relative_name(context_name) ⇒ Object



67
68
69
# File 'lib/card/name.rb', line 67

def relative_name context_name
  to_show(*context_name.to_name.parts).to_name
end

#rstar?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/card/name.rb', line 18

def rstar?
  right && '*' == right[0, 1]
end

#set?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/card/name.rb', line 97

def set?
  SetPattern.card_keys[tag_name.key]
end

#setting?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/card/name.rb', line 93

def setting?
  Set::Type::Setting.member_names[key]
end

#star?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/card/name.rb', line 14

def star?
  simple? && '*' == s[0, 1]
end

#starts_with_joint?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/card/name.rb', line 113

def starts_with_joint?
  s =~ /^\+/
end

#strippedObject



109
110
111
# File 'lib/card/name.rb', line 109

def stripped
  s.gsub RELATIVE_REGEXP, ''
end

#to_symObject



117
118
119
# File 'lib/card/name.rb', line 117

def to_sym
  s.to_sym
end

#trait(tag_code) ⇒ Object



39
40
41
42
# File 'lib/card/name.rb', line 39

def trait tag_code
  name = trait_name(tag_code)
  name ? name.s : (raise Card::NotFound, "unknown codename: #{tag_code}")
end

#trait_name(tag_code) ⇒ Object



33
34
35
36
37
# File 'lib/card/name.rb', line 33

def trait_name tag_code
  (card_id = Card::Codename[tag_code]) &&
    (card = Card.quick_fetch card_id) &&
    [self, card.cardname].to_name
end

#trait_name?(*traitlist) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
# File 'lib/card/name.rb', line 22

def trait_name? *traitlist
  junction? && begin
    right_key = right_name.key
    traitlist.find do |codename|
      (card_id = Card::Codename[codename]) &&
        (card = Card.quick_fetch card_id) &&
        card.key == right_key
    end.present?
  end
end