From c44f50e68d0d098633bb6565fb7628c7ea182563 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Mon, 28 Mar 2016 11:58:01 -0700 Subject: [PATCH] move Selector::Filter to its own file --- lib/capybara/selector.rb | 39 ++------------------------- lib/capybara/selector/filter.rb | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 37 deletions(-) create mode 100644 lib/capybara/selector/filter.rb diff --git a/lib/capybara/selector.rb b/lib/capybara/selector.rb index 3813d936..1b7bf61a 100644 --- a/lib/capybara/selector.rb +++ b/lib/capybara/selector.rb @@ -1,43 +1,8 @@ # frozen_string_literal: true +require 'capybara/selector/filter' + module Capybara class Selector - class Filter - def initialize(name, block, options={}) - @name = name - @block = block - @options = options - @options[:valid_values] = [true,false] if options[:boolean] - end - - def default? - @options.has_key?(:default) - end - - def default - @options[:default] - end - - def matches?(node, value) - return true if skip?(value) - - if @options.has_key?(:valid_values) && !Array(@options[:valid_values]).include?(value) - msg = "Invalid value #{value.inspect} passed to filter #{@name} - " - if default? - warn msg + "defaulting to #{default}" - value = default - else - warn msg + "skipping" - return true - end - end - - @block.call(node, value) - end - - def skip?(value) - @options.has_key?(:skip_if) && value == @options[:skip_if] - end - end attr_reader :name, :custom_filters, :format diff --git a/lib/capybara/selector/filter.rb b/lib/capybara/selector/filter.rb new file mode 100644 index 00000000..f36069b5 --- /dev/null +++ b/lib/capybara/selector/filter.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true +module Capybara + class Selector + class Filter + def initialize(name, block, options={}) + @name = name + @block = block + @options = options + @options[:valid_values] = [true,false] if options[:boolean] + end + + def default? + @options.has_key?(:default) + end + + def default + @options[:default] + end + + def matches?(node, value) + return true if skip?(value) + + if !valid_value?(value) + msg = "Invalid value #{value.inspect} passed to filter #{@name} - " + if default? + warn msg + "defaulting to #{default}" + value = default + else + warn msg + "skipping" + return true + end + end + + @block.call(node, value) + end + + def skip?(value) + @options.has_key?(:skip_if) && value == @options[:skip_if] + end + + private + + def valid_value?(value) + !@options.has_key?(:valid_values) || Array(@options[:valid_values]).include?(value) + end + end + end +end