Skip to content

Instantly share code, notes, and snippets.

@h-lame
Created February 20, 2015 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h-lame/072147e9395bb20cfa69 to your computer and use it in GitHub Desktop.
Save h-lame/072147e9395bb20cfa69 to your computer and use it in GitHub Desktop.
Refactoring?
def find_previous_table_for(element)
find_previous_sibling_of_type(element, 'table')
end
def find_previous_sibling_of_type(element, element_type_to_find)
previous_siblings(element).find do |previous_element|
previous_element.node_name == element_type_to_find
end
end
def previous_siblings(element)
Enumerator.new do |yielder|
while (element = element.previous_element).present?
yielder.yield element
end
end
end
def find_previous_table_for(element)
table = nil
previous_element = element.previous_element()
while table.nil? && !previous_element.nil?
if previous_element.node_name == 'table'
table = previous_element
end
previous_element = previous_element.previous_element()
end
table
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment