File: filter.rb

package info (click to toggle)
ruby-capybara 2.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,376 kB
  • ctags: 779
  • sloc: ruby: 12,494; makefile: 4
file content (48 lines) | stat: -rw-r--r-- 1,087 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
module Capybara
  class Selector
    class Filter
      def initialize(name, block, options={})
        @name = name
        @block = block
        @options = options
        @options[:valid_values] = [true,false] if options[:boolean]
      end

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

      def default
        @options[:default]
      end

      def matches?(node, value)
        return true if skip?(value)

        if !valid_value?(value)
          msg = "Invalid value #{value.inspect} passed to filter #{@name} - "
          if default?
            warn msg + "defaulting to #{default}"
            value = default
          else
            warn msg + "skipping"
            return true
          end
        end

        @block.call(node, value)
      end

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

      private

      def valid_value?(value)
        !@options.has_key?(:valid_values) || Array(@options[:valid_values]).include?(value)
      end
    end
  end
end