Class: PolyrexLinks

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PolyrexLinks.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/polyrex-links.rb', line 9

def initialize(rawx='links/link[name,url]', delimiter: ' # ', debug: false)
  
  x, _ = RXFHelper.read(rawx)
  obj = x.lstrip.sub(/<\?polyrex-links\?>/, 
                '<?polyrex schema="links/link[name,url]" delimiter=" # "?>')
  puts 'obj: ' + x.inspect if debug
  
  super(obj)
  @delimiter = self.delimiter = delimiter
  @debug = debug
  
end

Instance Method Details

#find(s) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/polyrex-links.rb', line 22

def find(s)
  
  found = find_by_link_name s
  
  if found then
    
    path = backtrack_path found 
    [link(path.join('/')), path.join('/')]
    
  end
  
end

Returns the Polyrex element for the given path



52
53
54
55
56
# File 'lib/polyrex-links.rb', line 52

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/polyrex-links.rb', line 60

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

#migrate(raws) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/polyrex-links.rb', line 35

def migrate(raws)
  
  s, _ = RXFHelper.read(raws)
  pl = PolyrexLinks.new("<?polyrex-links?>\n\n" + 
                        s.sub(/<\?[^\?]+\?>/,'').lstrip)
        
  pl.each_recursive do |x|
    link, linkpath = find(x.name)
    x.url = link.url if link and link.url
  end
  
  pl
  
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



88
89
90
91
92
93
94
95
# File 'lib/polyrex-links.rb', line 88

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

#to_sObject



97
98
99
# File 'lib/polyrex-links.rb', line 97

def to_s()
  "<?polyrex-links?>\n\n" + super(header: false)
end