From 9d3c60f9226579392f5ecd1ae80561ae344cc445 Mon Sep 17 00:00:00 2001 From: Jan Suchal Date: Thu, 9 Oct 2008 14:54:53 +0200 Subject: [PATCH] Replaced some full sorts with simple linear search. Refactored old code to nicer collection methods. --- lib/webrat/core/form.rb | 43 ++++++++++------------------------------ lib/webrat/core/scope.rb | 8 +++----- 2 files changed, 14 insertions(+), 37 deletions(-) diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 60a5c078..4d8a47b5 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -29,27 +29,17 @@ def find_select_option(option_text) end def find_button(value = nil) - return fields_by_type([ButtonField]).first if value.nil? - - possible_buttons = fields_by_type([ButtonField]) - - possible_buttons.each do |possible_button| - return possible_button if possible_button.matches_value?(value) - end - - nil + return fields_by_type([ButtonField]).first if value.nil? + possible_buttons = fields_by_type([ButtonField]) + possible_buttons.detect { |possible_button| possible_button.matches_value?(value) } end def fields return @fields if @fields - @fields = [] - - (@element / "button, input, textarea, select").each do |field_element| - @fields << Field.class_for_element(field_element).new(self, field_element) + @fields = (@element / "button, input, textarea, select").collect do |field_element| + Field.class_for_element(field_element).new(self, field_element) end - - @fields end def submit @@ -59,29 +49,18 @@ def submit protected def find_field_by_id(possible_fields, id) - possible_fields.each do |possible_field| - return possible_field if possible_field.matches_id?(id) - end - - nil + possible_fields.detect { |possible_field| possible_field.matches_id?(id) } end def find_field_by_name(possible_fields, name) - possible_fields.each do |possible_field| - return possible_field if possible_field.matches_name?(name) - end - - nil + possible_fields.detect { |possible_field| possible_field.matches_name?(name) } end def find_field_by_label(possible_fields, label) - matching_fields = [] - - possible_fields.each do |possible_field| - matching_fields << possible_field if possible_field.matches_label?(label) - end - - matching_fields.sort_by { |f| f.label_text.length }.first + matching_fields = possible_fields.select do |possible_field| + possible_field.matches_label?(label) + end + matching_fields.min { |a, b| a.label_text.length <=> b.label_text.length } end def fields_by_type(field_types) diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 445c1e98..c26e3754 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -202,14 +202,12 @@ def find_button(value) end def find_link(text, selector = nil) - matching_links = [] - - links_within(selector).each do |possible_link| - matching_links << possible_link if possible_link.matches_text?(text) + matching_links = links_within(selector).select do |possible_link| + possible_link.matches_text?(text) end 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 flunk("Could not find link with text #{text.inspect}") end