Module: Samsao::Regexp

Defined in:
lib/samsao/regexp.rb

Overview

Regexp utility methods

Class Method Summary collapse

Class Method Details

.from_matcher(matcher, when_string_pattern_prefix_with: '') ⇒ Regexp

Turns an input into a Regexp. If the input is a String, it turns it into a Regexp and escape all special characters. If the input is already a Regexp, it returns it without modification.

Parameters:

  • matcher

    The input matcher to transform into a Regexp

Returns:

  • (Regexp)

    The input as a regexp



23
24
25
26
27
# File 'lib/samsao/regexp.rb', line 23

def self.from_matcher(matcher, when_string_pattern_prefix_with: '')
  return matcher if matcher.is_a?(::Regexp)

  /#{when_string_pattern_prefix_with}#{::Regexp.quote(matcher)}/
end

.from_source(source) ⇒ Regexp

Turns a source entry input into a Regexp. Uses rules from [from_matcher](#from_matcher) function and append a ‘^` to final regexp when the source if a pure String.

Parameters:

  • matcher

    The source entry to transform into a Regexp

Returns:

  • (Regexp)

    The source entry as a regexp



10
11
12
# File 'lib/samsao/regexp.rb', line 10

def self.from_source(source)
  from_matcher(source, when_string_pattern_prefix_with: '^')
end