Class: MemDB::Field::Pattern::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/mem_db/field/pattern.rb

Defined Under Namespace

Classes: Exact, Prefix, Rx, Suffix

Constant Summary collapse

WILDCARD =
"*"

Instance Method Summary collapse

Constructor Details

#initialize(source, rx_engine:) ⇒ Pattern

Returns a new instance of Pattern.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mem_db/field/pattern.rb', line 58

def initialize(source, rx_engine:)
  wildcard_count = source.count(WILDCARD)
  @pat =
    if wildcard_count.zero?
      Exact.new(source)
    elsif wildcard_count > 1
      Rx.new(source, rx_engine)
    elsif source.end_with?(WILDCARD)
      Prefix.new(source[0..-2])
    elsif source.start_with?(WILDCARD)
      Suffix.new(source[1..-1])
    else # rubocop:disable Lint/DuplicateBranch
      Rx.new(source, rx_engine)
    end
end

Instance Method Details

#match?(str) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/mem_db/field/pattern.rb', line 74

def match?(str)
  @pat.match?(str)
end