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
34
35
# File 'lib/card/name/fields_and_traits.rb', line 26

def field_of? context
  if context.present?
    child_of?(context) && relative_name(context).length == 2
    # junction? &&
    #  absolute_name(context).left_name.key == context.to_name.key
    # @child_of?(context_name) && relative_name(context_name).length == 2
  else
    s.match(/^\s*\+[^+]+$/).present?
  end
end

#relative_field_name(tag_name) ⇒ Object



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

def relative_field_name tag_name
  field_name(tag_name).relative_name self
end

#trait(tag_code) ⇒ String

Returns:

  • (String)


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

def trait tag_code
  name = trait_name tag_code

  name.s
end

#trait_name(tag_code) ⇒ Card::Name

Returns:

Raises:



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

def trait_name tag_code
  card_id = Card::Codename[tag_code]
  raise Card::Error::NotFound, "unknown codename: #{tag_code}" unless card_id
  [self, Card.quick_fetch(card_id).name].to_name
end

#trait_name?(*traitlist) ⇒ True/False

Returns:

  • (True/False)


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

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