Skip to content

Instantly share code, notes, and snippets.

@brapse
Created July 20, 2009 18:28
Show Gist options
  • Save brapse/150492 to your computer and use it in GitHub Desktop.
Save brapse/150492 to your computer and use it in GitHub Desktop.
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