Created
March 5, 2009 05:45
-
-
Save tenderlove/74217 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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