Class: PolyrexLinks

Inherits:
Polyrex
  • Object
show all
Defined in:
lib/polyrex-links.rb

Instance Method Summary collapse

Constructor Details

#initialize(x = 'links/link[name,url]', delimiter: ' # ', debug: false) ⇒ PolyrexLinks

Returns a new instance of PolyrexLinks.



9
10
11
12
13
# File 'lib/polyrex-links.rb', line 9

def initialize(x='links/link[name,url]', delimiter: ' # ', debug: false)
  super(x)
  @delimiter = self.delimiter = delimiter
  @debug = debug
end

Instance Method Details

Returns the Polyrex element for the given path



17
18
19
20
21
# File 'lib/polyrex-links.rb', line 17

def link(s)
  
  self.rxpath(s.split('/').map {|x| "link[name='%s']" % x}.join('/')).first
  
end

#locate(raw_path) ⇒ Object

Returns the url and remaining arguments for the given path



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/polyrex-links.rb', line 25

def locate(raw_path)
 
  return nil if raw_path.nil? or raw_path.strip.empty?
  path = raw_path.sub(/^\//,'')
  a = path.split('/')
  a2 = []
  (a2 << a.clone; a.pop) while a.any?
  return nil if a2.empty?
  
  mask = "records/link[summary/name='%s']"
  
  begin 
    c = a2.shift; xpath = c.map{|x| mask % x}.join + '/summary/url/text()'
    r = self.element xpath 
  end until r or a2.empty?
  
  # return the found path along with any remaining string which 
  #   it didn't find, or it will return nil.
  
  puts "c: %s\npath: %s\nr: %s" % [c, path, r].map(&:inspect) if @debug

  r ? [r, path.sub(c.join('/'),'')] : nil
end

#to_hObject

Return a flat Hash object containing all the links. The link path is used as the key and the value is the url



53
54
55
56
57
58
59
60
# File 'lib/polyrex-links.rb', line 53

def to_h()
  
  h = {}
  
  each_link {|key, value| h[key] = value }
  h
  
end