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
-
#absolute? ⇒ Boolean
not relative.
-
#absolute_name(*args) ⇒ Cardname
-
#child_of?(context) ⇒ Boolean
True if name is left or right of context.
-
#from(*from) ⇒ Object
-
#name_from(*from) ⇒ Object
if possible, relativize name into one beginning with a “+”.
-
#nth_left(n) ⇒ String
1 = left; 2= left of left; 3 = left of left of left….
-
#relative? ⇒ Boolean
-
#simple_relative? ⇒ Boolean
starts with joint, no other contextual element.
-
#starts_with_joint? ⇒ Boolean
+X.
-
#stripped ⇒ String
contextual elements removed.
Instance Method Details
#absolute(context) ⇒ String
58 59 60 61 62 63 64 65 |
# File 'cardname/lib/cardname/contextual.rb', line 58 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
27 28 29 |
# File 'cardname/lib/cardname/contextual.rb', line 27 def absolute? !relative? end |
#absolute_name(*args) ⇒ Cardname
68 69 70 |
# File 'cardname/lib/cardname/contextual.rb', line 68 def absolute_name *args absolute(*args).to_name end |
#child_of?(context) ⇒ Boolean
Returns true if name is left or right of context.
7 8 9 10 11 12 |
# File 'cardname/lib/cardname/contextual.rb', line 7 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
43 44 45 |
# File 'cardname/lib/cardname/contextual.rb', line 43 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”
50 51 52 53 54 55 |
# File 'cardname/lib/cardname/contextual.rb', line 50 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) ⇒ String
1 = left; 2= left of left; 3 = left of left of left….
74 75 76 |
# File 'cardname/lib/cardname/contextual.rb', line 74 def nth_left n n >= length ? parts[0] : parts[0..-n - 1] end |
#relative? ⇒ Boolean
15 16 17 |
# File 'cardname/lib/cardname/contextual.rb', line 15 def relative? starts_with_joint? || (s =~ RELATIVE_REGEXP).present? end |
#simple_relative? ⇒ Boolean
starts with joint, no other contextual element
21 22 23 |
# File 'cardname/lib/cardname/contextual.rb', line 21 def simple_relative? starts_with_joint? && (s =~ RELATIVE_REGEXP).nil? end |
#starts_with_joint? ⇒ Boolean
+X
39 40 41 |
# File 'cardname/lib/cardname/contextual.rb', line 39 def starts_with_joint? compound? && parts.first.empty? end |
#stripped ⇒ String
contextual elements removed
33 34 35 |
# File 'cardname/lib/cardname/contextual.rb', line 33 def stripped s.gsub RELATIVE_REGEXP, "" end |