module SmartName::Contextual

Constants

RELATIVE_REGEXP

Public Instance Methods

absolute?() click to toggle source
   # File lib/smart_name/contextual.rb
28 def absolute?
29   !relative?
30 end
absolute_name(context_name) click to toggle source
   # File lib/smart_name/contextual.rb
 9 def absolute_name context_name
10   to_absolute_name(context_name)
11 end
child_of?(context) click to toggle source

@return true if name is left or right of context

   # File lib/smart_name/contextual.rb
14 def child_of? context
15   return false unless junction?
16   context_key = context.to_name.key
17   absolute_name(context).parent_keys.include? context_key
18 end
nth_left(n) click to toggle source
   # File lib/smart_name/contextual.rb
76 def nth_left n
77   # 1 = left; 2= left of left; 3 = left of left of left....
78   (n >= length ? parts[0] : parts[0..-n - 1]).to_name
79 end
relative?() click to toggle source
   # File lib/smart_name/contextual.rb
20 def relative?
21   s =~ RELATIVE_REGEXP || starts_with_joint?
22 end
relative_name(context_name) click to toggle source
  # File lib/smart_name/contextual.rb
5 def relative_name context_name
6   to_show(*context_name.to_name.parts).to_name
7 end
simple_relative?() click to toggle source
   # File lib/smart_name/contextual.rb
24 def simple_relative?
25   relative? && stripped.to_name.starts_with_joint?
26 end
starts_with_joint?() click to toggle source
   # File lib/smart_name/contextual.rb
36 def starts_with_joint?
37   length >= 2 && parts.first.empty?
38 end
stripped() click to toggle source
   # File lib/smart_name/contextual.rb
32 def stripped
33   s.gsub RELATIVE_REGEXP, ""
34 end
to_absolute(context, args={}) click to toggle source
   # File lib/smart_name/contextual.rb
57 def to_absolute context, args={}
58   context = context.to_name
59 
60   new_parts = replace_contextual_parts context
61   return "" if new_parts.empty?
62 
63   if new_parts.first.empty? && !new_parts.to_name.starts_with?(context)
64     new_parts[0] = context.to_s
65   end
66   if new_parts.last.empty? && !new_parts.to_name.ends_with?(context)
67     new_parts[-1] = context.to_s
68   end
69   new_parts.join self.class.joint
70 end
to_absolute_name(*args) click to toggle source
   # File lib/smart_name/contextual.rb
72 def to_absolute_name *args
73   self.class.new to_absolute(*args)
74 end
to_show(*ignore) click to toggle source
   # File lib/smart_name/contextual.rb
40 def to_show *ignore
41   ignore.map!(&:to_name)
42 
43   show_parts = parts.map do |part|
44     reject = (part.empty? || (part =~ /^_/) || ignore.member?(part.to_name))
45     reject ? nil : part
46   end
47 
48   show_name = show_parts.compact.to_name.s
49 
50   case
51   when show_parts.compact.empty? then  self
52   when show_parts[0].nil?        then  self.class.joint + show_name
53   else show_name
54   end
55 end

Private Instance Methods

replace_contextual_parts(context) click to toggle source
    # File lib/smart_name/contextual.rb
 83 def replace_contextual_parts context
 84   parts.map do |part|
 85     new_part =
 86       case part
 87       when /^_user$/i
 88         name_proc = self.class.session
 89         name_proc ? name_proc.call : part
 90       when /^_main$/i            then self.class.params[:main_name]
 91       when /^(_self|_whole|_)$/i then context.s
 92       when /^_left$/i            then context.trunk
 93         # note - inconsistent use of left v. trunk
 94       when /^_right$/i           then context.tag
 95       when /^_(\d+)$/i
 96         pos = $~[1].to_i
 97         pos = context.length if pos > context.length
 98         context.parts[pos - 1]
 99       when /^_(L*)(R?)$/i
100         l_s, r_s = $~[1].size, !$~[2].empty?
101         l_part = context.nth_left l_s
102         r_s ? l_part.tag : l_part.s
103         # when /^_/
104         #   custom = args[:params] ? args[:params][part] : nil
105         #   custom ? CGI.escapeHTML(custom) : part
106         # why are we escaping HTML here?
107       else
108         part
109       end.to_s.strip
110     new_part
111   end
112 end