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
- #field(tag_name) ⇒ String
- #field_name(tag_name) ⇒ Card::Name
- #field_of?(context) ⇒ True/False
- #field_only? ⇒ Boolean
- #relative_field_name(tag_name) ⇒ Object
- #trait(tag_code) ⇒ String
- #trait_name(tag_code) ⇒ Card::Name
- #trait_name?(*traitlist) ⇒ True/False
Instance Method Details
#field(tag_name) ⇒ 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
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
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
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
44 45 46 47 48 |
# 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
51 52 53 54 55 |
# File 'lib/card/name/fields_and_traits.rb', line 51 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
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/card/name/fields_and_traits.rb', line 58 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 |