Class: PolyrexLinks
- Inherits:
-
Polyrex
- Object
- Polyrex
- PolyrexLinks
- Defined in:
- lib/polyrex-links.rb
Instance Method Summary collapse
- #find(s) ⇒ Object
-
#index ⇒ Object
returns a listing of all links sorted by title in alphabetical order.
-
#initialize(rawx = 'links/link[title,url]', delimiter: ' # ', debug: false) ⇒ PolyrexLinks
constructor
A new instance of PolyrexLinks.
-
#link(s) ⇒ Object
Returns the Polyrex element for the given path.
-
#locate(raw_path) ⇒ Object
Returns the url and remaining arguments for the given path.
- #migrate(raws) ⇒ Object
- #save(filepath) ⇒ Object
-
#to_h ⇒ Object
Return a flat Hash object containing all the links.
- #to_s ⇒ Object
Constructor Details
#initialize(rawx = 'links/link[title,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[title,url]', delimiter: ' # ', debug: false) x, _ = RXFHelper.read(rawx) obj = x.lstrip.sub(/<\?polyrex-links\?>/, '<?polyrex schema="links/link[title,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_title s if found then path = backtrack_path found [link(path.join('/')), path.join('/')] end end |
#index ⇒ Object
returns a listing of all links sorted by title in alphabetical order
37 38 39 40 41 42 43 |
# File 'lib/polyrex-links.rb', line 37 def index() a = [] each_recursive {|link| a << [link.title, link.url, backtrack_path(link)] } a.sort_by {|title, _, _| title.downcase} end |
#link(s) ⇒ Object
Returns the Polyrex element for the given path
62 63 64 65 66 |
# File 'lib/polyrex-links.rb', line 62 def link(s) self.rxpath(s.split('/').map {|x| "link[title='%s']" % x}.join('/')).first end |
#locate(raw_path) ⇒ Object
Returns the url and remaining arguments for the given path
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/polyrex-links.rb', line 70 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/title='%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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/polyrex-links.rb', line 45 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.title) x.url = link.url if link and link.url end pl end |
#save(filepath) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/polyrex-links.rb', line 94 def save(filepath) super(filepath) export(filepath.sub(/\.xml$/,'.txt')) end |
#to_h ⇒ Object
Return a flat Hash object containing all the links. The link path is used as the key and the value is the url
105 106 107 108 109 110 111 112 |
# File 'lib/polyrex-links.rb', line 105 def to_h() h = {} each_link {|key, value| h[key] = value } h end |
#to_s ⇒ Object
114 115 116 |
# File 'lib/polyrex-links.rb', line 114 def to_s() "<?polyrex-links?>\n\n" + super(header: false) end |