summaryrefslogtreecommitdiff
path: root/qa/qa/page/element.rb
blob: 6bfdf98587baa967bb11a71110e8bd1099ffbc7f (plain)
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
49
50
51
52
53
54
55
# frozen_string_literal: true

require 'active_support/core_ext/array/extract_options'

module QA
  module Page
    class Element
      attr_reader :name, :attributes

      def initialize(name, *options)
        @name = name
        @attributes = options.extract_options!
        @attributes[:pattern] ||= selector

        options.each do |option|
          if option.is_a?(String) || option.is_a?(Regexp)
            @attributes[:pattern] = option
          end
        end
      end

      def selector
        "qa-#{@name.to_s.tr('_', '-')}"
      end

      def required?
        !!@attributes[:required]
      end

      def selector_css
        %Q([data-qa-selector="#{@name}"]#{additional_selectors},.#{selector})
      end

      def expression
        if @attributes[:pattern].is_a?(String)
          @_regexp ||= Regexp.new(Regexp.escape(@attributes[:pattern]))
        else
          @attributes[:pattern]
        end
      end

      def matches?(line)
        !!(line =~ /["']#{name}['"]|#{expression}/)
      end

      private

      def additional_selectors
        @attributes.dup.delete_if { |attr| attr == :pattern || attr == :required }.map do |key, value|
          %Q([data-qa-#{key.to_s.tr('_', '-')}="#{value}"])
        end.join
      end
    end
  end
end