Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created March 5, 2009 05:45
Show Gist options
  • Select an option

  • Save tenderlove/74217 to your computer and use it in GitHub Desktop.

Select an option

Save tenderlove/74217 to your computer and use it in GitHub Desktop.
require 'nokogiri'
class MyDSL
def initialize &block
@doc = Nokogiri::HTML(<<-eohtml)
<html>
<body>
<a href="whatever">whatevs</a>
<a id='one' href="http://tenderlovemaking.com/">tlm.com</a>
</body>
</html>
eohtml
instance_eval(&block)
end
###
# Custom XPath function
def awesome_search target_id, value
@doc.xpath('//a[my_search(.)]', Class.new {
def initialize target_id, href
@target_id = target_id
@href = href
end
def my_search nodes
nodes.find_all { |node|
node['href'] == @href && node['id'] == @target_id
}
end
}.new(target_id, value))
end
end
MyDSL.new {
p awesome_search('one', 'http://tenderlovemaking.com/')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment