Class: Glob::Matcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Matcher

Returns a new instance of Matcher.



7
8
9
10
11
12
13
14
15
16
# File 'lib/glob/matcher.rb', line 7

def initialize(path)
  @path = path
  @reject = path.start_with?("!")

  pattern = Regexp.escape(path.gsub(/^!/, ""))
                  .gsub(/(\\{.*?\\})/) {|match| process_group(match) }
                  .gsub("\\*", "[^.]+")
  anchor = path.end_with?("*") ? "" : "$"
  @regex = Regexp.new("^#{pattern}#{anchor}")
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/glob/matcher.rb', line 5

def path
  @path
end

#regexObject (readonly)

Returns the value of attribute regex.



5
6
7
# File 'lib/glob/matcher.rb', line 5

def regex
  @regex
end

Instance Method Details

#include?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/glob/matcher.rb', line 22

def include?
  !reject?
end

#match?(other) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/glob/matcher.rb', line 18

def match?(other)
  other.match?(regex)
end

#process_group(group) ⇒ Object



30
31
32
33
34
# File 'lib/glob/matcher.rb', line 30

def process_group(group)
  group = group.gsub(/[{}\\]/, "").split(",").join("|")

  "(#{group})"
end

#reject?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/glob/matcher.rb', line 26

def reject?
  @reject
end