Sha256: c78d30addc766e565b5eba32fb1a714236c4e900570249b8b41a1d893702c956

Contents?: true

Size: 1.59 KB

Versions: 10

Compression:

Stored size: 1.59 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)
    raise NotImplementedError, "XFN not supported yet. What's the Nokogiri equivalent of Hpricot::Doc?"
    case doc
    when Nokogiri::XML::Element then @occurences = XFN.new(doc)
    else @occurences
    end
  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.inner_html, :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

10 entries across 10 versions & 2 rubygems

Version Path
calagator-1.0.0 vendor/gems/mofo-0.2.8/lib/mofo/xfn.rb
grokus-1.0.0.9 vendor/gems/mofo-0.2.8/lib/mofo/xfn.rb
grokus-1.0.0.8 vendor/gems/mofo-0.2.8/lib/mofo/xfn.rb
grokus-1.0.0.7 vendor/gems/mofo-0.2.8/lib/mofo/xfn.rb
grokus-1.0.0.6 vendor/gems/mofo-0.2.8/lib/mofo/xfn.rb
grokus-1.0.0.5 vendor/gems/mofo-0.2.8/lib/mofo/xfn.rb
grokus-1.0.0.3 vendor/gems/mofo-0.2.8/lib/mofo/xfn.rb
grokus-1.0.0.2 vendor/gems/mofo-0.2.8/lib/mofo/xfn.rb
grokus-1.0.0.1 vendor/gems/mofo-0.2.8/lib/mofo/xfn.rb
calagator-1.0.0.rc3 vendor/gems/mofo-0.2.8/lib/mofo/xfn.rb