Class: Wovnrb::Glob

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

Overview

Note:

“?” or other pattern is not implemented

inspired by github.com/isaacs/node-glob

“*” Matches 0 or more characters in a single path portion “**” If a “globstar” is alone in a path portion,

then it matches zero or more directories and subdirectories searching for matches.

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Glob

Returns a new instance of Glob.



10
11
12
13
14
15
16
17
18
19
# File 'lib/wovnrb/services/glob.rb', line 10

def initialize(pattern)
  sub_directories = pattern.split('/**', -1)
  regexp = sub_directories.map do |sub_dir|
    sub_dir.split('*', -1)
           .map { |p| Regexp.escape(p) }
           .join('[^/]*')
  end.join('(/[^/]*)*')

  @regexp = Regexp.new("^#{regexp}$")
end

Instance Method Details

#match?(url) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/wovnrb/services/glob.rb', line 21

def match?(url)
  !@regexp.match(url).nil?
end