Created
July 20, 2009 18:28
-
-
Save brapse/150492 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
class Table | |
def initialize(selector, document) | |
@table = document.search(selector).search("tr").map do |tr| | |
tr.search("th, td").map do |td| | |
td.inner_text.strip.gsub(/$/, '$') | |
end.first | |
end | |
# confirms it's a table | |
unless @table.is_a?(Array) && @table.first.is_a?(Array) | |
raise UnAdaptedError.new("table parsing failed") | |
end | |
[@table, @table.first].each do |instance| | |
table.metaclass.send(:define_method, :[]) do |index| | |
unless table[index] | |
raise UnAdaptedError.new("selector expectations not met") | |
else | |
super | |
end | |
end | |
end | |
end | |
def method_missing(msg, *args, &block) | |
if @table.responds_to?(msg) | |
@table.send(msg, args, &block) | |
else | |
super | |
end | |
end | |
end # Table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment