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
-
#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
-
#simple_relative? ⇒ Boolean
starts with joint, no other contextual element.
Instance Method Details
#absolute(context) ⇒ String
interpret contextual name
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
28 29 30 |
# File 'cardname/lib/cardname/contextual.rb', line 28 def absolute? !relative? end |
#absolute_name(context) ⇒ Cardname
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
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
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”
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….
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
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
22 23 24 |
# File 'cardname/lib/cardname/contextual.rb', line 22 def simple_relative? starts_with_joint? && (s =~ RELATIVE_REGEXP).nil? end |