From d7eec209509f7e1035415e469d9064edae515173 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sat, 29 Nov 2008 01:58:27 -0500 Subject: [PATCH] Extract FieldNamedLocator object --- lib/webrat/core/locators.rb | 13 ++------- .../core/locators/field_named_locator.rb | 27 +++++++++++++++++++ lib/webrat/core/locators/locator.rb | 3 ++- 3 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 lib/webrat/core/locators/field_named_locator.rb diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index f194477..c0ccb70 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -38,18 +38,9 @@ module Webrat end def find_field_named(name, *field_types) #:nodoc: - if field_types.any? - xpath_searches = field_types.map { |field_type| field_type.xpath_search }.flatten - field_elements = Webrat::XML.xpath_search(dom, xpath_searches) - else - field_elements = Webrat::XML.xpath_search(dom, *Field.xpath_search) - end + require "webrat/core/locators/field_named_locator" - field_element = field_elements.detect do |field_element| - Webrat::XML.attribute(field_element, "name") == name.to_s - end - - field_by_element(field_element) + FieldNamedLocator.new(self, name, *field_types).locate end def field_by_element(element) diff --git a/lib/webrat/core/locators/field_named_locator.rb b/lib/webrat/core/locators/field_named_locator.rb new file mode 100644 index 0000000..f18a2fc --- /dev/null +++ b/lib/webrat/core/locators/field_named_locator.rb @@ -0,0 +1,27 @@ +require "webrat/core/locators/locator" + +class FieldNamedLocator < Locator + + def locate + @scope.field_by_element(field_element) + end + + def field_element + field_elements.detect do |field_element| + Webrat::XML.attribute(field_element, "name") == @value.to_s + end + end + + def field_elements + Webrat::XML.xpath_search(@scope.dom, *xpath_searches) + end + + def xpath_searches + if @field_types.any? + @field_types.map { |field_type| field_type.xpath_search }.flatten + else + Array(Field.xpath_search) + end + end + +end \ No newline at end of file diff --git a/lib/webrat/core/locators/locator.rb b/lib/webrat/core/locators/locator.rb index a2557d2..e2bf815 100644 --- a/lib/webrat/core/locators/locator.rb +++ b/lib/webrat/core/locators/locator.rb @@ -1,8 +1,9 @@ class Locator - def initialize(scope, value) + def initialize(scope, value, *field_types) @scope = scope @value = value + @field_types = field_types end end \ No newline at end of file