class I18n::Tasks::Data::Router::IsolatingRouter::Glob

based on github.com/alexch/rerun/blob/36f2d237985b670752abbe4a7f6814893cdde96f/lib/rerun/glob.rb

Constants

END_OF_STRING
NO_LEADING_DOT
START_OF_FILENAME

Public Class Methods

new(pattern) click to toggle source
# File lib/i18n/tasks/data/router/isolating_router.rb, line 78
def initialize(pattern)
  @pattern = pattern
end

Public Instance Methods

smoosh(chars) click to toggle source
# File lib/i18n/tasks/data/router/isolating_router.rb, line 129
def smoosh(chars)
  out = []
  until chars.empty?
    char = chars.shift
    if char == '*' && chars.first == '*'
      chars.shift
      chars.shift if chars.first == '/'
      out.push('**')
    else
      out.push(char)
    end
  end
  out
end
to_regexp() click to toggle source
# File lib/i18n/tasks/data/router/isolating_router.rb, line 125
def to_regexp
  Regexp.new(to_regexp_string)
end
to_regexp_string() click to toggle source
# File lib/i18n/tasks/data/router/isolating_router.rb, line 82
def to_regexp_string # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
  chars = smoosh(@pattern.chars)

  curlies = 0
  escaping = false

  string = chars.map do |char|
    if escaping
      escaping = false
      next char
    end

    case char
    when '**' then '(?:[^/]+/)*'
    when '*' then '.*'
    when '?' then '.'
    when '.' then '\.'
    when '{'
      curlies += 1
      '('
    when '}'
      if curlies.positive?
        curlies -= 1
        ')'
      else
        char
      end
    when ','
      if curlies.positive?
        '|'
      else
        char
      end
    when '\\'
      escaping = true
      '\\'
    else char
    end
  end.join

  START_OF_FILENAME + string + END_OF_STRING
end