From 92543978072e9b9a85de07ba3c6ba98190a04c26 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sat, 29 Nov 2008 01:33:55 -0500 Subject: [PATCH] Extracting LinkLocator object --- lib/webrat/core/locators.rb | 13 +++++++------ lib/webrat/core/locators/link_locator.rb | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 lib/webrat/core/locators/link_locator.rb diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index e7db3fc..dde4507 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -116,13 +116,14 @@ module Webrat def find_link(text_or_title_or_id) #:nodoc: # TODO - Convert to using elements + # + # matching_links = links.select do |possible_link| + # possible_link.matches_text?(text_or_title_or_id) || possible_link.matches_id?(text_or_title_or_id) + # end + require "webrat/core/locators/link_locator" - matching_links = links.select do |possible_link| - possible_link.matches_text?(text_or_title_or_id) || possible_link.matches_id?(text_or_title_or_id) - end - - if matching_links.any? - matching_links.min { |a, b| a.text.length <=> b.text.length } + if link = LinkLocator.new(self, text_or_title_or_id).locate + link else raise NotFoundError.new("Could not find link with text or title or id #{text_or_title_or_id.inspect}") end diff --git a/lib/webrat/core/locators/link_locator.rb b/lib/webrat/core/locators/link_locator.rb new file mode 100644 index 0000000..702df31 --- /dev/null +++ b/lib/webrat/core/locators/link_locator.rb @@ -0,0 +1,20 @@ +class LinkLocator + + def initialize(scope, value) + @scope = scope + @value = value + end + + def locate + matching_links = @scope.send(:links).select do |possible_link| + possible_link.matches_text?(@value) || possible_link.matches_id?(@value) + end + + if matching_links.any? + matching_links.min { |a, b| a.text.length <=> b.text.length } + else + nil + end + end + +end \ No newline at end of file