Created
February 20, 2015 10:42
-
-
Save h-lame/072147e9395bb20cfa69 to your computer and use it in GitHub Desktop.
Refactoring?
This file contains 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
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 |
This file contains 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
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