Class: Rerun::Glob

Inherits:
Object
  • Object
show all
Defined in:
lib/rerun/glob.rb

Constant Summary collapse

NO_LEADING_DOT =

todo

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

beginning of string or a slash

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

Instance Method Summary collapse

Constructor Details

#initialize(glob_string) ⇒ Glob

Returns a new instance of Glob.



11
12
13
# File 'lib/rerun/glob.rb', line 11

def initialize glob_string
  @glob_string = glob_string
end

Instance Method Details

#smoosh(chars) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rerun/glob.rb', line 70

def 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

#to_regexpObject



66
67
68
# File 'lib/rerun/glob.rb', line 66

def to_regexp
  Regexp.new(to_regexp_string)
end

#to_regexp_stringObject



15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/rerun/glob.rb', line 15

def to_regexp_string
  chars = @glob_string.split('')

  chars = smoosh(chars)

  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
  START_OF_FILENAME + string + END_OF_STRING
end