Module: Cardname::Contextual
- Included in:
- Cardname
- Defined in:
- 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
-
#absolute(context) ⇒ String
interpret contextual name.
-
#absolute? ⇒ Boolean
not relative.
- #absolute_name(context) ⇒ Cardname
-
#child_of?(context) ⇒ Boolean
true if name is left or right of context.
- #from(*from) ⇒ String
-
#name_from(*from) ⇒ Cardname
if possible, relativize name into one beginning with a “+”.
-
#nth_left_name(n) ⇒ Cardname
1 = left; 2= left of left; 3 = left of left of left.…
- #relative? ⇒ Boolean
-
#relative_fragment? ⇒ Boolean
_left, _right, _self or any other name fragment referring to a relative part.
-
#simple_relative? ⇒ Boolean
starts with joint, no other contextual element.
Instance Method Details
#absolute(context) ⇒ String
interpret contextual name
55 56 57 58 59 60 61 62 |
# File 'lib/cardname/contextual.rb', line 55 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
33 34 35 |
# File 'lib/cardname/contextual.rb', line 33 def absolute? !relative? end |
#absolute_name(context) ⇒ Cardname
65 66 67 |
# File 'lib/cardname/contextual.rb', line 65 def absolute_name context absolute(context).to_name end |
#child_of?(context) ⇒ Boolean
true if name is left or right of context
8 9 10 11 12 13 |
# File '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
38 39 40 |
# File 'lib/cardname/contextual.rb', line 38 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”
46 47 48 49 50 51 |
# File 'lib/cardname/contextual.rb', line 46 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.…
71 72 73 |
# File 'lib/cardname/contextual.rb', line 71 def nth_left_name n (n >= length ? parts[0] : parts[0..-n - 1]).to_name end |
#relative? ⇒ Boolean
16 17 18 |
# File 'lib/cardname/contextual.rb', line 16 def relative? starts_with_joint? || s.match?(RELATIVE_REGEXP) end |
#relative_fragment? ⇒ Boolean
_left, _right, _self or any other name fragment referring to a relative part
27 28 29 |
# File 'lib/cardname/contextual.rb', line 27 def relative_fragment? s.match?(/^#{RELATIVE_REGEXP}$/) end |
#simple_relative? ⇒ Boolean
starts with joint, no other contextual element
22 23 24 |
# File 'lib/cardname/contextual.rb', line 22 def simple_relative? starts_with_joint? && !s.match?(RELATIVE_REGEXP) end |