Sha256: 53cf5f30dcc06727828943cee8f3056f356b44e7d0d4d365bf194f1564a6d609

Contents?: true

Size: 1.47 KB

Versions: 15

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Capybara
  class Selector
    module Filters
      class Base
        def initialize(name, matcher, block, **options)
          @name = name
          @matcher = matcher
          @block = block
          @options = options
          @options[:valid_values] = [true, false] if options[:boolean]
        end

        def default?
          @options.key?(:default)
        end

        def default
          @options[:default]
        end

        def skip?(value)
          @options.key?(:skip_if) && value == @options[:skip_if]
        end

        def matcher?
          !@matcher.nil?
        end

        def handles_option?(option_name)
          if matcher?
            option_name =~ @matcher
          else
            @name == option_name
          end
        end

      private

        def apply(subject, name, value, skip_value)
          return skip_value if skip?(value)
          raise ArgumentError, "Invalid value #{value.inspect} passed to #{self.class.name.split('::').last} #{name}#{" : #{@name}" if @name.is_a?(Regexp)}" unless valid_value?(value)
          if @block.arity == 2
            @block.call(subject, value)
          else
            @block.call(subject, name, value)
          end
        end

        def valid_value?(value)
          return true unless @options.key?(:valid_values)
          Array(@options[:valid_values]).any? { |valid| valid === value } # rubocop:disable Style/CaseEquality
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
tdiary-5.0.11 vendor/bundle/gems/capybara-3.6.0/lib/capybara/selector/filters/base.rb
capybara-3.8.2 lib/capybara/selector/filters/base.rb
capybara-3.8.1 lib/capybara/selector/filters/base.rb
capybara-3.8.0 lib/capybara/selector/filters/base.rb
capybara-3.7.2 lib/capybara/selector/filters/base.rb
capybara-3.7.1 lib/capybara/selector/filters/base.rb
capybara-3.7.0 lib/capybara/selector/filters/base.rb
capybara-3.6.0 lib/capybara/selector/filters/base.rb
capybara-3.5.1 lib/capybara/selector/filters/base.rb
capybara-3.5.0 lib/capybara/selector/filters/base.rb
capybara-3.4.2 lib/capybara/selector/filters/base.rb
capybara-3.4.1 lib/capybara/selector/filters/base.rb
capybara-3.4.0 lib/capybara/selector/filters/base.rb
capybara-3.3.1 lib/capybara/selector/filters/base.rb
capybara-3.3.0 lib/capybara/selector/filters/base.rb