Sha256: cc722fcf917b0e6156177affe1a827bb9d0cd394f70ab78393794242f4f78eed
Contents?: true
Size: 1.42 KB
Versions: 4
Compression:
Stored size: 1.42 KB
Contents
$:.unshift 'lib' require 'microformat' class XFN < Microformat class Link < OpenStruct def initialize(*args) super def relation.has?(value) is_a?(Array) ? include?(value) : self == value end end def to_html %[<a href="#{link}" rel="#{Array(relation) * ' '}">#{name}</a>] end def to_s link end end attr_accessor :links def self.find_occurences(doc) @occurences ||= XFN.new(doc) end class << self alias :find_first :find_occurences alias :find_every :find_occurences end def initialize(doc) @links = doc.search("a[@rel]").map do |rl| relation = rl[:rel].include?(' ') ? rl[:rel].split(' ') : rl[:rel] Link.new(:name => rl.innerHTML, :link => rl[:href], :relation => relation) end end def relations @relations ||= @links.map { |l| l.relation } end def [](*rels) @links.select do |link| relation = link.relation relation.respond_to?(:all?) && rels.all? { |rel| relation.include? rel } end.first_or_self end def method_missing(method, *args, &block) method = method.to_s if (rels = method.split(/_and_/)).size > 1 self[*rels] elsif @links.class.public_instance_methods.include? method @links.send(method, *args, &block) else check = args.first == true ? :== : :has? @links.select { |link| link.relation.send(check, method) }.first_or_self end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
mofo-0.2.1 | lib/mofo/xfn.rb |
mofo-0.2.2 | lib/mofo/xfn.rb |
mofo-0.2.3 | ./lib/mofo/xfn.rb |
mofo-0.2.4 | ./lib/mofo/xfn.rb |