Module: Leftovers::MatcherBuilders::StringPattern

Defined in:
lib/leftovers/matcher_builders/string_pattern.rb

Class Method Summary collapse

Class Method Details

.build(match: nil, has_prefix: nil, has_suffix: nil) ⇒ Object

rubocop:disable Metrics



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/leftovers/matcher_builders/string_pattern.rb', line 6

def self.build(match: nil, has_prefix: nil, has_suffix: nil) # rubocop:disable Metrics
  has_prefix = ::Regexp.escape(has_prefix) if has_prefix
  has_suffix = ::Regexp.escape(has_suffix) if has_suffix

  if match && has_prefix && has_suffix
    /\A(?=#{match}\z)(?=#{has_prefix}).*#{has_suffix}\z/
  elsif match && has_prefix
    /\A(?=#{match}\z)#{has_prefix}/
  elsif match && has_suffix
    /\A(?=#{match}\z).*#{has_suffix}\z/
  elsif match
    /\A#{match}\z/
  elsif has_prefix && has_suffix
    /\A(?=#{has_prefix}).*#{has_suffix}\z/
  elsif has_prefix
    /\A#{has_prefix}/
  elsif has_suffix
    /#{has_suffix}\z/
  else
    nil
  end
end