class Capybara::Selector::FilterSet

Attributes

expression_filters[R]
node_filters[R]

Public Class Methods

add(name, &block) click to toggle source
# File lib/capybara/selector/filter_set.rb, line 71
def add(name, &block)
  all[name.to_sym] = FilterSet.new(name.to_sym, &block)
end
all() click to toggle source
# File lib/capybara/selector/filter_set.rb, line 67
def all
  @filter_sets ||= {} # rubocop:disable Naming/MemoizedInstanceVariableName
end
new(name, &block) click to toggle source
# File lib/capybara/selector/filter_set.rb, line 10
def initialize(name, &block)
  @name = name
  @node_filters = {}
  @expression_filters = {}
  @descriptions = Hash.new { |hsh, key| hsh[key] = [] }
  instance_eval(&block)
end
remove(name) click to toggle source
# File lib/capybara/selector/filter_set.rb, line 75
def remove(name)
  all.delete(name.to_sym)
end

Public Instance Methods

describe(what = nil, &block) click to toggle source
# File lib/capybara/selector/filter_set.rb, line 27
def describe(what = nil, &block)
  case what
  when nil
    undeclared_descriptions.push block
  when :node_filters
    node_filter_descriptions.push block
  when :expression_filters
    expression_filter_descriptions.push block
  else
    raise ArgumentError, 'Unknown description type'
  end
end
description(node_filters: true, expression_filters: true, **options) click to toggle source
# File lib/capybara/selector/filter_set.rb, line 40
def description(node_filters: true, expression_filters: true, **options)
  opts = options_with_defaults(options)
  description = +''
  description += undeclared_descriptions.map { |desc| desc.call(opts).to_s }.join
  description += expression_filter_descriptions.map { |desc| desc.call(opts).to_s }.join if expression_filters
  description += node_filter_descriptions.map { |desc| desc.call(opts).to_s }.join if node_filters
  description
end
descriptions() click to toggle source
# File lib/capybara/selector/filter_set.rb, line 49
def descriptions
  warn 'DEPRECATED: FilterSet#descriptions is deprecated without replacement'
  [undeclared_descriptions, node_filter_descriptions, expression_filter_descriptions].flatten
end
expression_filter(name, *types_and_options, &block) click to toggle source
# File lib/capybara/selector/filter_set.rb, line 23
def expression_filter(name, *types_and_options, &block)
  add_filter(name, Filters::ExpressionFilter, *types_and_options, &block)
end
filter(name, *types_and_options, &block)
Alias for: node_filter
import(name, filters = nil) click to toggle source
# File lib/capybara/selector/filter_set.rb, line 54
def import(name, filters = nil)
  f_set = self.class.all[name]
  filter_selector = filters.nil? ? ->(*) { true } : ->(filter_name, _) { filters.include? filter_name }

  expression_filters.merge!(f_set.expression_filters.select(&filter_selector))
  node_filters.merge!(f_set.node_filters.select(&filter_selector))

  f_set.undeclared_descriptions.each { |desc| describe(&desc) }
  f_set.expression_filter_descriptions.each { |desc| describe(:expression_filters, &desc) }
  f_set.node_filter_descriptions.each { |desc| describe(:node_filters, &desc) }
end
node_filter(name, *types_and_options, &block) click to toggle source
# File lib/capybara/selector/filter_set.rb, line 18
def node_filter(name, *types_and_options, &block)
  add_filter(name, Filters::NodeFilter, *types_and_options, &block)
end
Also aliased as: filter

Protected Instance Methods

expression_filter_descriptions() click to toggle source
# File lib/capybara/selector/filter_set.rb, line 90
def expression_filter_descriptions
  @descriptions[:expression_filters]
end
node_filter_descriptions() click to toggle source
# File lib/capybara/selector/filter_set.rb, line 86
def node_filter_descriptions
  @descriptions[:node_filters]
end
undeclared_descriptions() click to toggle source
# File lib/capybara/selector/filter_set.rb, line 82
def undeclared_descriptions
  @descriptions[:undeclared]
end

Private Instance Methods

add_filter(name, filter_class, *types, matcher: nil, **options, &block) click to toggle source
# File lib/capybara/selector/filter_set.rb, line 106
def add_filter(name, filter_class, *types, matcher: nil, **options, &block)
  types.each { |type| options[type] = true }
  raise 'ArgumentError', ':default option is not supported for filters with a :matcher option' if matcher && options[:default]
  if filter_class <= Filters::ExpressionFilter
    @expression_filters[name] = filter_class.new(name, matcher, block, options)
  else
    @node_filters[name] = filter_class.new(name, matcher, block, options)
  end
end
options_with_defaults(options) click to toggle source
# File lib/capybara/selector/filter_set.rb, line 96
def options_with_defaults(options)
  options = options.dup
  [expression_filters, node_filters].each do |filters|
    filters.select { |_n, filter| filter.default? }.each do |name, filter|
      options[name] = filter.default unless options.key?(name)
    end
  end
  options
end