Class: Picoglob::Matcher

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

Overview

A compiled glob. Compile once, match many times.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, separator: "/", dot: false, extglob: true, nocase: false) ⇒ Matcher

Returns a new instance of Matcher.



66
67
68
69
70
71
# File 'lib/picoglob.rb', line 66

def initialize(pattern, separator: "/", dot: false, extglob: true, nocase: false)
  @pattern = pattern
  @regexp = Compiler.new(
    separator: separator, dot: dot, extglob: extglob, nocase: nocase
  ).compile(pattern)
end

Instance Attribute Details

#patternString (readonly)

Returns the original glob pattern.

Returns:

  • (String)

    the original glob pattern



62
63
64
# File 'lib/picoglob.rb', line 62

def pattern
  @pattern
end

#regexpRegexp (readonly)

Returns the compiled regular expression.

Returns:

  • (Regexp)

    the compiled regular expression



64
65
66
# File 'lib/picoglob.rb', line 64

def regexp
  @regexp
end

Instance Method Details

#filter(strings) ⇒ Array<String>

Parameters:

  • strings (Enumerable<String>)

Returns:

  • (Array<String>)


87
88
89
# File 'lib/picoglob.rb', line 87

def filter(strings)
  strings.select { |s| @regexp.match?(s) }
end

#match?(string) ⇒ Boolean Also known as: ===

Parameters:

  • string (String)

Returns:

  • (Boolean)


75
76
77
# File 'lib/picoglob.rb', line 75

def match?(string)
  @regexp.match?(string)
end

#to_regexpRegexp

Returns:

  • (Regexp)


81
82
83
# File 'lib/picoglob.rb', line 81

def to_regexp
  @regexp
end