Sha256: 2e5da5f4e03fddfe1013047e447590f839d5fdcbfcc6b621b731c0347d356805

Contents?: true

Size: 1.82 KB

Versions: 45

Compression:

Stored size: 1.82 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 format
          @options[:format]
        end

        def matcher?
          !@matcher.nil?
        end

        def boolean?
          !!@options[:boolean]
        end

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

      private

        def apply(subject, name, value, skip_value, ctx)
          return skip_value if skip?(value)

          unless valid_value?(value)
            raise ArgumentError,
                  "Invalid value #{value.inspect} passed to #{self.class.name.split('::').last} #{name}" \
                  "#{" : #{name}" if @name.is_a?(Regexp)}"
          end

          if @block.arity == 2
            filter_context(ctx).instance_exec(subject, value, &@block)
          else
            filter_context(ctx).instance_exec(subject, name, value, &@block)
          end
        end

        def filter_context(context)
          context || @block.binding.receiver
        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

45 entries across 34 versions & 4 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/capybara-3.40.0/lib/capybara/selector/filters/base.rb
capybara-3.40.0 lib/capybara/selector/filters/base.rb
capybara-3.39.2 lib/capybara/selector/filters/base.rb
capybara-3.39.1 lib/capybara/selector/filters/base.rb
capybara-3.39.0 lib/capybara/selector/filters/base.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/capybara-3.36.0/lib/capybara/selector/filters/base.rb
capybara-3.38.0 lib/capybara/selector/filters/base.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/capybara-3.37.1/lib/capybara/selector/filters/base.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/capybara-3.37.1/lib/capybara/selector/filters/base.rb
capybara-3.37.1 lib/capybara/selector/filters/base.rb
capybara-3.37.0 lib/capybara/selector/filters/base.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/capybara-3.36.0/lib/capybara/selector/filters/base.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/capybara-3.35.3/lib/capybara/selector/filters/base.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/capybara-3.36.0/lib/capybara/selector/filters/base.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/capybara-3.36.0/lib/capybara/selector/filters/base.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/capybara-3.36.0/lib/capybara/selector/filters/base.rb
capybara-3.36.0 lib/capybara/selector/filters/base.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/capybara-3.35.3/lib/capybara/selector/filters/base.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/capybara-3.35.3/lib/capybara/selector/filters/base.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/capybara-3.35.3/lib/capybara/selector/filters/base.rb