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?Boolean

Returns:

  • (Boolean)


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

def absolute?
  !relative?
end

#absolute_name(context_name) ⇒ Object



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

def absolute_name context_name
  to_absolute_name(context_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



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

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

#nth_left(n) ⇒ Object



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

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

#relative?Boolean

Returns:

  • (Boolean)


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

def relative?
  s =~ RELATIVE_REGEXP || starts_with_joint?
end

#relative_name(context_name) ⇒ Object



5
6
7
# File 'lib/cardname/contextual.rb', line 5

def relative_name context_name
  to_show(*context_name.to_name.parts).to_name
end

#simple_relative?Boolean

Returns:

  • (Boolean)


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

def simple_relative?
  relative? && stripped.to_name.starts_with_joint?
end

#starts_with_joint?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/cardname/contextual.rb', line 36

def starts_with_joint?
  length >= 2 && parts.first.empty?
end

#strippedObject



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

def stripped
  s.gsub RELATIVE_REGEXP, ""
end

#to_absolute(context, args = {}) ⇒ Object



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

def to_absolute context, args={}
  context = context.to_name

  new_parts = replace_contextual_parts context
  return "" if new_parts.empty?

  if new_parts.first.empty? && !new_parts.to_name.starts_with?(context)
    new_parts[0] = context.to_s
  end
  if new_parts.last.empty? && !new_parts.to_name.ends_with?(context)
    new_parts[-1] = context.to_s
  end
  new_parts.join self.class.joint
end

#to_absolute_name(*args) ⇒ Object



72
73
74
# File 'lib/cardname/contextual.rb', line 72

def to_absolute_name *args
  self.class.new to_absolute(*args)
end

#to_show(*ignore) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cardname/contextual.rb', line 40

def to_show *ignore
  ignore.map!(&:to_name)

  show_parts = parts.map do |part|
    reject = (part.empty? || (part =~ /^_/) || ignore.member?(part.to_name))
    reject ? nil : part
  end

  show_name = show_parts.compact.to_name.s

  case
  when show_parts.compact.empty? then  self
  when show_parts[0].nil?        then  self.class.joint + show_name
  else show_name
  end
end