Class: Capybara::Selector::FilterSet
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/capybara/selector/filter_set.rb |
Class Method Summary
Instance Attribute Summary
- #expression_filters readonly
- #node_filters readonly
Instance Method Summary
- #describe(what = nil, &block)
- #description(node_filters: true, expression_filters: true, **options)
- #descriptions
- #expression_filter(name, *types, **options, &block)
-
#filter(names, *types, **options, &block)
Alias for #node_filter.
- #import(name, filters = nil)
- #node_filter(names, *types, **options, &block) (also: #filter)
- #add_filter(name, filter_class, *types, matcher: nil, **options, &block) private
- #options_with_defaults(options) private
Constructor Details
.new(name, &block) ⇒ FilterSet
# 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) if block end
Class Method Details
.[](name)
[ GitHub ]# File 'lib/capybara/selector/filter_set.rb', line 74
def [](name) all.fetch(name.to_sym) { |set_name| raise ArgumentError, "Unknown filter set (:#{set_name})" } end
.add(name, &block)
[ GitHub ].all
[ GitHub ]# File 'lib/capybara/selector/filter_set.rb', line 70
def all @filter_sets ||= {} # rubocop:disable Naming/MemoizedInstanceVariableName end
.remove(name)
[ GitHub ]# File 'lib/capybara/selector/filter_set.rb', line 82
def remove(name) all.delete(name.to_sym) end
Instance Attribute Details
#expression_filters (readonly)
[ GitHub ]# File 'lib/capybara/selector/filter_set.rb', line 8
attr_reader :node_filters, :expression_filters
#node_filters (readonly)
[ GitHub ]# File 'lib/capybara/selector/filter_set.rb', line 8
attr_reader :node_filters, :expression_filters
Instance Method Details
#add_filter(name, filter_class, *types, matcher: nil, **options, &block) (private)
[ GitHub ]# File 'lib/capybara/selector/filter_set.rb', line 110
def add_filter(name, filter_class, *types, matcher: nil, **, &block) types.each { |type| [type] = true } if matcher && [:default] raise 'ArgumentError', ':default option is not supported for filters with a :matcher option' end filter = filter_class.new(name, matcher, block, ** ) (filter_class <= Filters::ExpressionFilter ? @expression_filters : @node_filters)[name] = filter end
#describe(what = nil, &block)
[ GitHub ]# File 'lib/capybara/selector/filter_set.rb', line 29
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)
[ GitHub ]# File 'lib/capybara/selector/filter_set.rb', line 42
def description(node_filters: true, expression_filters: true, ** ) opts = ( ) 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
[ GitHub ]#expression_filter(name, *types, **options, &block)
[ GitHub ]# File 'lib/capybara/selector/filter_set.rb', line 25
def expression_filter(name, *types, **, &block) add_filter(name, Filters::ExpressionFilter, *types, **, &block) end
#filter(names, *types, **options, &block)
Alias for #node_filter.
# File 'lib/capybara/selector/filter_set.rb', line 23
alias_method :filter, :node_filter
#import(name, filters = nil)
[ GitHub ]# File 'lib/capybara/selector/filter_set.rb', line 56
def import(name, filters = nil) filter_selector = filters.nil? ? ->(*) { true } : ->(filter_name, _) { filters.include? filter_name } self.class[name].tap do |f_set| 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 self end
#node_filter(names, *types, **options, &block) Also known as: #filter
[ GitHub ]# File 'lib/capybara/selector/filter_set.rb', line 18
def node_filter(names, *types, **, &block) Array(names).each do |name| add_filter(name, Filters::NodeFilter, *types, **, &block) end end
#options_with_defaults(options) (private)
[ GitHub ]# File 'lib/capybara/selector/filter_set.rb', line 103
def ( ) expression_filters .chain(node_filters) .filter_map { |name, filter| [name, filter.default] if filter.default? } .to_h.merge!( ) end