Module: Cardname::Contextual

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

Overview

contextual (or relative) names are names that vary by context

Constant Summary collapse

RELATIVE_REGEXP =
/\b_(left|right|whole|self|user|main|\d+|L*R?)\b/

Instance Method Summary collapse

Instance Method Details

#absolute(context) ⇒ String

interpret contextual name

Returns:

  • (String)


50
51
52
53
54
55
56
57
# File 'cardname/lib/cardname/contextual.rb', line 50

def absolute context
  context = (context || "").to_name
  new_parts = absolutize_contextual_parts context
  return "" if new_parts.empty?

  absolutize_extremes new_parts, context.s
  new_parts.join self.class.joint
end

#absolute?Boolean

not relative

Returns:

  • (Boolean)


28
29
30
# File 'cardname/lib/cardname/contextual.rb', line 28

def absolute?
  !relative?
end

#absolute_name(context) ⇒ Cardname

Returns:



60
61
62
# File 'cardname/lib/cardname/contextual.rb', line 60

def absolute_name context
  absolute(context).to_name
end

#child_of?(context) ⇒ Boolean

true if name is left or right of context

Returns:

  • (Boolean)


8
9
10
11
12
13
# File 'cardname/lib/cardname/contextual.rb', line 8

def child_of? context
  return false unless compound?

  context_key = context.to_name.key
  absolute_name(context).parent_keys.include? context_key
end

#from(*from) ⇒ String

Returns:

  • (String)


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

def from *from
  name_from(*from).s
end

#name_from(*from) ⇒ Cardname

if possible, relativize name into one beginning with a “+”. The new name must absolutize back to the correct original name in the context of “from”

Returns:



41
42
43
44
45
46
# File 'cardname/lib/cardname/contextual.rb', line 41

def name_from *from
  return self unless (remaining = remove_context(*from))

  compressed = remaining.compact.unshift(nil).to_name  # exactly one nil at beginning
  key == compressed.absolute_name(from).key ? compressed : self
end

#nth_left_name(n) ⇒ Cardname

1 = left; 2= left of left; 3 = left of left of left….

Returns:



66
67
68
# File 'cardname/lib/cardname/contextual.rb', line 66

def nth_left_name n
  (n >= length ? parts[0] : parts[0..-n - 1]).to_name
end

#relative?Boolean

Returns:

  • (Boolean)


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

def relative?
  starts_with_joint? || (s =~ RELATIVE_REGEXP).present?
end

#simple_relative?Boolean

starts with joint, no other contextual element

Returns:

  • (Boolean)


22
23
24
# File 'cardname/lib/cardname/contextual.rb', line 22

def simple_relative?
  starts_with_joint? && (s =~ RELATIVE_REGEXP).nil?
end