Module: Card::Name::FieldsAndTraits

Included in:
Card::Name
Defined in:
card/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 'card/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 'card/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
34
# File 'card/lib/card/name/fields_and_traits.rb', line 26

def field_of? context
  return false unless compound?

  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)


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

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

#relative_field_name(tag_name) ⇒ Object



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

def relative_field_name tag_name
  field_name(tag_name).name_from self
end

#trait(tag_code) ⇒ String

Returns:

  • (String)


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

def trait tag_code
  name = trait_name tag_code
  name.s
end

#trait_name(tag_code) ⇒ Card::Name

Returns:



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

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

#trait_name?(*traitlist) ⇒ True/False

Returns:

  • (True/False)


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

def trait_name? *traitlist
  return false unless compound?

  right_key = right_name.key
  traitlist.any? do |codename|
    Card::Codename.name(codename)&.key == right_key
  end
end