class Capybara::Selector::RegexpDisassembler::Expression

@api private

Public Class Methods

new(exp) click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 93
def initialize(exp)
  @exp = exp
end

Public Instance Methods

extract_strings(process_alternatives) click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 97
def extract_strings(process_alternatives)
  strings = []
  each do |exp|
    next if exp.ignore?

    next strings.push(nil) if exp.optional? && !process_alternatives

    next strings.push(exp.alternative_strings) if exp.alternation? && process_alternatives

    strings.concat(exp.strings(process_alternatives))
  end
  strings
end

Protected Instance Methods

alternation?() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 113
def alternation?
  (type == :meta) && !terminal?
end
alternative_strings() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 156
def alternative_strings
  alts = alternatives.map { |sub_exp| sub_exp.extract_strings(alternation: true) }
  alts.all?(&:any?) ? Set.new(alts) : nil
end
ignore?() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 161
def ignore?
  [Regexp::Expression::Assertion::NegativeLookahead,
   Regexp::Expression::Assertion::NegativeLookbehind].any? { |klass| @exp.is_a? klass }
end
optional?() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 117
def optional?
  min_repeat.zero?
end
optional_strings() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 148
def optional_strings
  options_set(extract_strings(true))
end
repeated_strings(process_alternatives) click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 152
def repeated_strings(process_alternatives)
  repeat_set extract_strings(process_alternatives)
end
strings(process_alternatives) click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 125
def strings(process_alternatives)
  if indeterminate?
    [nil]
  elsif terminal?
    terminal_strings
  elsif optional?
    optional_strings
  else
    repeated_strings(process_alternatives)
  end
end
terminal?() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 121
def terminal?
  @exp.terminal?
end
terminal_strings() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 137
def terminal_strings
  text = case @exp.type
  when :literal then @exp.text
  when :escape then @exp.char
  else
    return [nil]
  end

  optional? ? options_set(text) : repeat_set(text)
end

Private Instance Methods

alternatives() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 200
def alternatives
  @exp.alternatives.map { |exp| Expression.new(exp) }
end
each() { |expression| ... } click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 204
def each
  @exp.each { |exp| yield Expression.new(exp) }
end
fixed_repeat?() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 180
def fixed_repeat?
  min_repeat == max_repeat
end
indeterminate?() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 168
def indeterminate?
  %i[meta set].include?(type)
end
max_repeat() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 176
def max_repeat
  @exp.repetitions.end
end
min_repeat() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 172
def min_repeat
  @exp.repetitions.begin
end
options_set(strs) click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 194
def options_set(strs)
  strs = [Set.new([[''], Array(strs)])]
  strs.push(nil) unless max_repeat == 1
  strs
end
repeat_set(str) click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 188
def repeat_set(str)
  strs = Array(str * min_repeat)
  strs.push(nil) unless fixed_repeat?
  strs
end
type() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 184
def type
  @exp.type
end