Module: Cardname::Contextual

Included in:
Cardname
Defined in:
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, args = {}) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/cardname/contextual.rb', line 67

def absolute context, args={}
  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



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

def absolute?
  !relative?
end

#absolute_name(*args) ⇒ Object



75
76
77
# File 'lib/cardname/contextual.rb', line 75

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

#child_of?(context) ⇒ Boolean



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

def child_of? context
  return false unless junction?
  context_key = context.to_name.key
  absolute_name(context).parent_keys.include? context_key
end

#from(*from) ⇒ Object



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

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”



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

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



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

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



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

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



58
59
60
61
62
63
64
65
# File 'lib/cardname/contextual.rb', line 58

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



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

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

#remove_context(*from) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/cardname/contextual.rb', line 44

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



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

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

#starts_with_joint?Boolean



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

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

#strippedObject



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

def stripped
  s.gsub RELATIVE_REGEXP, ""
end