Module: Card::Name::FieldsAndTraits

Included in:
Card::Name
Defined in:
lib/card/name/fields_and_traits.rb

Overview

Name-based "Fields" are compound names in which the right name is treated as an attribute of the left. (Eg MyName+address)

"Traits" are the subset of fields in which the right name corresponds to a card with a codename

Instance Method Summary collapse

Instance Method Details

#field(tag_name) ⇒ String

Returns:

  • (String)


10
11
12
# File 'lib/card/name/fields_and_traits.rb', line 10

def field tag_name
  field_name(tag_name).s
end

#field_name(tag_name) ⇒ Card::Name

Returns:



15
16
17
18
19
20
21
22
23
# File 'lib/card/name/fields_and_traits.rb', line 15

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) ⇒ True/False

Returns:

  • (True/False)


26
27
28
29
30
31
32
33
# File 'lib/card/name/fields_and_traits.rb', line 26

def field_of? context
  return false unless junction?
  if context.present?
    absolute_name(context).left_name.key == context.to_name.key
  else
    s.match(/^\s*\+[^+]+$/).present?
  end
end

#field_only?Boolean

Returns:

  • (Boolean)


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

def field_only?
  relative? && stripped.to_name.parts.reject(&:blank?).first == parts.last
end

#relative_field_name(tag_name) ⇒ Object



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

def relative_field_name tag_name
  field_name(tag_name).name_from self
end

#trait(tag_code) ⇒ String

Returns:

  • (String)


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

def trait tag_code
  name = trait_name tag_code
  name.s
end

#trait_name(tag_code) ⇒ Card::Name

Returns:



50
51
52
# File 'lib/card/name/fields_and_traits.rb', line 50

def trait_name tag_code
  Card::Name[self, tag_code.to_sym]
end

#trait_name?(*traitlist) ⇒ True/False

Returns:

  • (True/False)


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

def trait_name? *traitlist
  return false unless junction?
  right_key = right_name.key
  traitlist.any? do |codename|
    Card::Codename.name(codename)&.key == right_key
  end
end