class Capybara::Selector::RegexpDisassembler::Expression

@api private

Public Class Methods

new(exp) click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 96
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 100
def extract_strings(process_alternatives)
  strings = []
  each do |exp|
    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 114
def alternation?
  (type == :meta) && !terminal?
end
alternative_strings() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 157
def alternative_strings
  alts = alternatives.map { |sub_exp| sub_exp.extract_strings(alternation: true) }
  alts.all?(&:any?) ? Set.new(alts) : nil
end
optional?() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 118
def optional?
  min_repeat.zero?
end
optional_strings() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 149
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 153
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 126
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 122
def terminal?
  @exp.terminal?
end
terminal_strings() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 138
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 196
def alternatives
  @exp.alternatives.map { |exp| Expression.new(exp) }
end
each() { |expression| ... } click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 200
def each
  @exp.each { |exp| yield Expression.new(exp) }
end
fixed_repeat?() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 176
def fixed_repeat?
  min_repeat == max_repeat
end
indeterminate?() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 164
def indeterminate?
  %i[meta set].include?(type)
end
max_repeat() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 172
def max_repeat
  @exp.repetitions.end
end
min_repeat() click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 168
def min_repeat
  @exp.repetitions.begin
end
options_set(strs) click to toggle source
# File lib/capybara/selector/regexp_disassembler.rb, line 190
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 184
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 180
def type
  @exp.type
end