Module: FakeFS::Globber

Extended by:
Globber
Included in:
Globber
Defined in:
lib/fakefs/globber.rb

Overview

Handles globbing for FakeFS.

Instance Method Summary collapse

Instance Method Details

#expand(pattern) ⇒ Object



6
7
8
9
10
11
12
13
14
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
# File 'lib/fakefs/globber.rb', line 6

def expand(pattern)
  return [pattern] if pattern[0] != '{' || pattern[-1] != '}'

  part = ''
  result = []

  each_char_with_levels pattern, '{', '}' do |chr, level|
    case level
    when 0
      case chr
      when '{'
        # noop
      else
        part << chr
      end
    when 1
      case chr
      when ','
        result << part
        part = ''
      when '}'
        # noop
      else
        part << chr
      end
    else
      part << chr
    end
  end

  result << part

  result
end

#path_components(pattern) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fakefs/globber.rb', line 41

def path_components(pattern)
  part = ''
  result = []

  each_char_with_levels pattern, '{', '}' do |chr, level|
    if level == 0 && chr == File::SEPARATOR
      result << part
      part = ''
    else
      part << chr
    end
  end

  result << part

  drop_root(result).reject(&:empty?)
end

#regexp(pattern) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fakefs/globber.rb', line 59

def regexp(pattern)
  regex_body = pattern.gsub('.', '\.')
               .gsub('?', '.')
               .gsub('*', '.*')
               .gsub('(', '\(')
               .gsub(')', '\)')
               .gsub('$', '\$')
               .gsub(/\{(.*?)\}/) do
                 "(#{Regexp.last_match[1].gsub(',', '|')})"
               end
               .gsub(/\A\./, '(?!\.).')
  /\A#{regex_body}\Z/
end