Class: Archaeo::PatternFilter

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

Overview

URL pattern filter for include/exclude matching during downloads.

Supports string substring matching, Regexp objects, and %r… and /…/ string-to-regexp conversion with inline flags.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(only: nil, exclude: nil) ⇒ PatternFilter

Returns a new instance of PatternFilter.



9
10
11
12
# File 'lib/archaeo/pattern_filter.rb', line 9

def initialize(only: nil, exclude: nil)
  @only_patterns = compile_patterns(Array(only))
  @exclude_patterns = compile_patterns(Array(exclude))
end

Class Method Details

.to_regex(pattern) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/archaeo/pattern_filter.rb', line 26

def self.to_regex(pattern)
  case pattern
  when Regexp then pattern
  when String then parse_regex_string(pattern)
  else
    raise ArgumentError,
          "Pattern must be String or Regexp, got #{pattern.class}"
  end
end

Instance Method Details

#match?(url) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/archaeo/pattern_filter.rb', line 14

def match?(url)
  url = url.to_s
  return false if excluded?(url)
  return true if @only_patterns.empty?

  included?(url)
end

#reject?(url) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/archaeo/pattern_filter.rb', line 22

def reject?(url)
  !match?(url)
end