Module: Cardname::Contextual

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

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) ⇒ Object



72
73
74
75
76
77
78
79
# File 'cardname/lib/cardname/contextual.rb', line 72

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

Returns:

  • (Boolean)


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

def absolute?
  !relative?
end

#absolute_name(*args) ⇒ Object



81
82
83
# File 'cardname/lib/cardname/contextual.rb', line 81

def absolute_name *args
  absolute(*args).to_name
end

#child_of?(context) ⇒ Boolean

Returns true if name is left or right of context.

Returns:

  • (Boolean)

    true if name is left or right of context



6
7
8
9
10
11
# File 'cardname/lib/cardname/contextual.rb', line 6

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) ⇒ Object



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

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

#name_from(*from) ⇒ Object

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”



39
40
41
42
43
44
# File 'cardname/lib/cardname/contextual.rb', line 39

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(n) ⇒ Object



85
86
87
88
# File 'cardname/lib/cardname/contextual.rb', line 85

def nth_left n
  # 1 = left; 2= left of left; 3 = left of left of left....
  (n >= length ? parts[0] : parts[0..-n - 1]).to_name
end

#parts_excluding(*string) ⇒ Object



56
57
58
59
60
# File 'cardname/lib/cardname/contextual.rb', line 56

def parts_excluding *string
  exclude_name = string.to_name
  exclude_keys = exclude_name ? exclude_name.part_names.map(&:key) : []
  parts_minus exclude_keys
end

#parts_minus(keys_to_ignore) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'cardname/lib/cardname/contextual.rb', line 62

def parts_minus keys_to_ignore
  parts.map do |part|
    next if part.empty?
    next if part =~ /^_/ # this removes relative parts.  why?
    next if keys_to_ignore.member? part.to_name.key

    part
  end
end

#relative?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'cardname/lib/cardname/contextual.rb', line 13

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

#remove_context(*from) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'cardname/lib/cardname/contextual.rb', line 46

def remove_context *from
  return false unless from.compact.present?

  remaining = parts_excluding(*from)
  return false if remaining.compact.empty? || # all name parts in context
                  remaining == parts          # no name parts in context

  remaining
end

#simple_relative?Boolean

Returns:

  • (Boolean)


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

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

#starts_with_joint?Boolean

Returns:

  • (Boolean)


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

def starts_with_joint?
  compound? && parts.first.empty?
end

#strippedObject



25
26
27
# File 'cardname/lib/cardname/contextual.rb', line 25

def stripped
  s.gsub RELATIVE_REGEXP, ""
end