Module: Condenser::Rails::Utils

Defined in:
lib/condenser/rails/utils.rb

Constant Summary collapse

NO_LEADING_DOT =

todo

'(?=[^\.])'
START_OF_FILENAME =

beginning of string or a slash

'(\A|\/)'
END_OF_STRING =
'\z'

Class Method Summary collapse

Class Method Details

.glob_to_regex(glob_string) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/condenser/rails/utils.rb', line 26

def self.glob_to_regex(glob_string)
  chars = smoosh(glob_string.split(''))

  curlies = 0
  escaping = false
  string = chars.map do |char|
    if escaping
      escaping = false
      char
    else
      case char
        when '**'
          "([^/]+/)*"
        when '*'
          ".*"
        when "?"
          "."
        when "."
          "\\."

        when "{"
          curlies += 1
          "("
        when "}"
          if curlies > 0
            curlies -= 1
            ")"
          else
            char
          end
        when ","
          if curlies > 0
            "|"
          else
            char
          end
        when "\\"
          escaping = true
          "\\"

        else
          char

      end
    end
  end.join
  
  Regexp.new(START_OF_FILENAME + string + END_OF_STRING)
end

.smoosh(chars) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/condenser/rails/utils.rb', line 11

def self.smoosh chars
  out = []
  until chars.empty?
    char = chars.shift
    if char == "*" and chars.first == "*"
      chars.shift
      chars.shift if chars.first == "/"
      out.push("**")
    else
      out.push(char)
    end
  end
  out
end