module CqlRuby::PatternMatcher

Public Class Methods

match?(pattern, subject) click to toggle source
# File lib/cql_ruby/pattern_matcher.rb, line 4
def self.match?(pattern, subject)
  pattern = pattern.to_s
  return true if pattern == CqlRuby::MATCH_ANYTHING

  subject = subject.to_s

  if regex?(pattern)
    regex_match?(pattern, subject)
  elsif partial_string?(pattern)
    partial_string_match?(pattern, subject)
  else
    full_string_match?(pattern, subject)
  end
end

Private Class Methods

full_string_match?(pattern, subject) click to toggle source
# File lib/cql_ruby/pattern_matcher.rb, line 45
def self.full_string_match?(pattern, subject)
  pattern == subject
end
partial_string?(pattern) click to toggle source
# File lib/cql_ruby/pattern_matcher.rb, line 24
def self.partial_string?(pattern)
  pattern[0] == '%'
end
partial_string_match?(pattern, subject) click to toggle source
# File lib/cql_ruby/pattern_matcher.rb, line 50
def self.partial_string_match?(pattern, subject)
  !subject.index(pattern[1..]).nil?
end
regex?(pattern) click to toggle source
# File lib/cql_ruby/pattern_matcher.rb, line 19
def self.regex?(pattern)
  pattern[0..1] == 'r/'
end
regex_match?(pattern, subject) click to toggle source
# File lib/cql_ruby/pattern_matcher.rb, line 29
def self.regex_match?(pattern, subject)
  pattern = pattern[2..]
  delim_idx = pattern.rindex('/')
  mods = pattern[delim_idx + 1..].chars
  pattern = pattern[0..delim_idx - 1]

  fops = 0
  fops |= Regexp::IGNORECASE if mods.include?('i')
  fops |= Regexp::MULTILINE if mods.include?('m')
  fops |= Regexp::EXTENDED if mods.include?('x')
  fops |= Regexp::FIXEDENCODING if mods.include?('f')
  fops |= Regexp::NOENCODING if mods.include?('n')
  Regexp.new(pattern, fops).match?(subject)
end