Replaced some full sorts with simple linear search.

Refactored old code to nicer collection methods.
This commit is contained in:
Jan Suchal 2008-10-09 14:54:53 +02:00
parent 33fdf33c89
commit 9d3c60f922
2 changed files with 14 additions and 37 deletions

View File

@ -30,26 +30,16 @@ module Webrat
def find_button(value = nil) def find_button(value = nil)
return fields_by_type([ButtonField]).first if value.nil? return fields_by_type([ButtonField]).first if value.nil?
possible_buttons = fields_by_type([ButtonField]) possible_buttons = fields_by_type([ButtonField])
possible_buttons.detect { |possible_button| possible_button.matches_value?(value) }
possible_buttons.each do |possible_button|
return possible_button if possible_button.matches_value?(value)
end
nil
end end
def fields def fields
return @fields if @fields return @fields if @fields
@fields = [] @fields = (@element / "button, input, textarea, select").collect do |field_element|
Field.class_for_element(field_element).new(self, field_element)
(@element / "button, input, textarea, select").each do |field_element|
@fields << Field.class_for_element(field_element).new(self, field_element)
end end
@fields
end end
def submit def submit
@ -59,29 +49,18 @@ module Webrat
protected protected
def find_field_by_id(possible_fields, id) def find_field_by_id(possible_fields, id)
possible_fields.each do |possible_field| possible_fields.detect { |possible_field| possible_field.matches_id?(id) }
return possible_field if possible_field.matches_id?(id)
end
nil
end end
def find_field_by_name(possible_fields, name) def find_field_by_name(possible_fields, name)
possible_fields.each do |possible_field| possible_fields.detect { |possible_field| possible_field.matches_name?(name) }
return possible_field if possible_field.matches_name?(name)
end
nil
end end
def find_field_by_label(possible_fields, label) def find_field_by_label(possible_fields, label)
matching_fields = [] matching_fields = possible_fields.select do |possible_field|
possible_field.matches_label?(label)
possible_fields.each do |possible_field|
matching_fields << possible_field if possible_field.matches_label?(label)
end end
matching_fields.min { |a, b| a.label_text.length <=> b.label_text.length }
matching_fields.sort_by { |f| f.label_text.length }.first
end end
def fields_by_type(field_types) def fields_by_type(field_types)

View File

@ -202,14 +202,12 @@ module Webrat
end end
def find_link(text, selector = nil) def find_link(text, selector = nil)
matching_links = [] matching_links = links_within(selector).select do |possible_link|
possible_link.matches_text?(text)
links_within(selector).each do |possible_link|
matching_links << possible_link if possible_link.matches_text?(text)
end end
if matching_links.any? if matching_links.any?
matching_links.sort_by { |l| l.text.length }.first matching_links.min { |a, b| a.text.length <=> b.text.length }
else else
flunk("Could not find link with text #{text.inspect}") flunk("Could not find link with text #{text.inspect}")
end end