class Capybara::Selector::Filters::Base

Public Class Methods

new(name, matcher, block, **options) click to toggle source
# File lib/capybara/selector/filters/base.rb, line 7
def initialize(name, matcher, block, **options)
  @name = name
  @matcher = matcher
  @block = block
  @options = options
  @options[:valid_values] = [true, false] if options[:boolean]
end

Public Instance Methods

boolean?() click to toggle source
# File lib/capybara/selector/filters/base.rb, line 35
def boolean?
  !!@options[:boolean]
end
default() click to toggle source
# File lib/capybara/selector/filters/base.rb, line 19
def default
  @options[:default]
end
default?() click to toggle source
# File lib/capybara/selector/filters/base.rb, line 15
def default?
  @options.key?(:default)
end
format() click to toggle source
# File lib/capybara/selector/filters/base.rb, line 27
def format
  @options[:format]
end
handles_option?(option_name) click to toggle source
# File lib/capybara/selector/filters/base.rb, line 39
def handles_option?(option_name)
  if matcher?
    @matcher.match? option_name
  else
    @name == option_name
  end
end
matcher?() click to toggle source
# File lib/capybara/selector/filters/base.rb, line 31
def matcher?
  !@matcher.nil?
end
skip?(value) click to toggle source
# File lib/capybara/selector/filters/base.rb, line 23
def skip?(value)
  @options.key?(:skip_if) && value == @options[:skip_if]
end

Private Instance Methods

apply(subject, name, value, skip_value, ctx) click to toggle source
# File lib/capybara/selector/filters/base.rb, line 49
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
filter_context(context) click to toggle source
# File lib/capybara/selector/filters/base.rb, line 65
def filter_context(context)
  context || @block.binding.receiver
end
valid_value?(value) click to toggle source
# File lib/capybara/selector/filters/base.rb, line 69
def valid_value?(value)
  return true unless @options.key?(:valid_values)

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