Extracting LinkLocator object

This commit is contained in:
Bryan Helmkamp 2008-11-29 01:33:55 -05:00
parent 0b6d9c28ea
commit 9254397807
2 changed files with 27 additions and 6 deletions

View File

@ -116,13 +116,14 @@ module Webrat
def find_link(text_or_title_or_id) #:nodoc: def find_link(text_or_title_or_id) #:nodoc:
# TODO - Convert to using elements # 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| if link = LinkLocator.new(self, text_or_title_or_id).locate
possible_link.matches_text?(text_or_title_or_id) || possible_link.matches_id?(text_or_title_or_id) link
end
if matching_links.any?
matching_links.min { |a, b| a.text.length <=> b.text.length }
else else
raise NotFoundError.new("Could not find link with text or title or id #{text_or_title_or_id.inspect}") raise NotFoundError.new("Could not find link with text or title or id #{text_or_title_or_id.inspect}")
end end

View File

@ -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