Class: I18n::Tasks::Data::Router::IsolatingRouter::Glob

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/tasks/data/router/isolating_router.rb

Overview

Constant Summary collapse

NO_LEADING_DOT =
'(?=[^\.])'
START_OF_FILENAME =
'(?:\A|\/)'
END_OF_STRING =
'\z'

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Glob

Returns a new instance of Glob.



78
79
80
# File 'lib/i18n/tasks/data/router/isolating_router.rb', line 78

def initialize(pattern)
  @pattern = pattern
end

Instance Method Details

#smoosh(chars) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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_regexpObject



125
126
127
# File 'lib/i18n/tasks/data/router/isolating_router.rb', line 125

def to_regexp
  Regexp.new(to_regexp_string)
end

#to_regexp_stringObject

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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