Module: Cardname::Fields

Included in:
Cardname
Defined in:
cardname/lib/cardname/fields.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#field(tag_name) ⇒ String

Examples:

"A".cardname.field_name "B" -> "A+B"

Returns:

  • (String)


8
9
10
# File 'cardname/lib/cardname/fields.rb', line 8

def field tag_name
  field_name(tag_name).s
end

#field_name(tag) ⇒ Cardname

Examples:

"A".cardname.field_name "B" -> "A+B"

Returns:



15
16
17
18
# File 'cardname/lib/cardname/fields.rb', line 15

def field_name tag
  tag = tag.to_s[1..-1] if !tag.is_a?(Symbol) && tag.to_s[0] == "+"
  [self, tag].to_name
end

#field_of?(context) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'cardname/lib/cardname/fields.rb', line 21

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*\+[^+]+$/)
  end
end

#field_only?Boolean

name is relative name containing only the rightmost part

Returns:

  • (Boolean)


33
34
35
# File 'cardname/lib/cardname/fields.rb', line 33

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

#relative_field_name(tag_name) ⇒ Object



37
38
39
# File 'cardname/lib/cardname/fields.rb', line 37

def relative_field_name tag_name
  field_name(tag_name).name_from self
end