class Middleman::WebP::PathnameMatcher

Attributes

pattern[R]

Public Class Methods

new(pattern = '**/*') click to toggle source

Initializes matcher with given pattern.

pattern - Pattern to match pathnames against to. May be

string, glob, prog or regex.
# File lib/middleman-webp/pathname_matcher.rb, line 11
def initialize(pattern = '**/*')
  @pattern = pattern
end

Public Instance Methods

<=>(other) click to toggle source

Compares matchers based on their preciness.

  • One with longest pattern is considered to be more precise

  • Glob or Regexp patterns are considered more precise than procs.

# File lib/middleman-webp/pathname_matcher.rb, line 28
def <=>(other)
  is_proc_involed = other.pattern.respond_to?(:call) || @pattern.respond_to?(:call)
  return compare_to_proc(other) if is_proc_involed

  @pattern.to_s.length <=> other.pattern.to_s.length
end
hash() click to toggle source
# File lib/middleman-webp/pathname_matcher.rb, line 35
def hash
  @pattern.hash
end
matches?(path) click to toggle source

Checks given file against pattern.

file - File, Pathname or String

# File lib/middleman-webp/pathname_matcher.rb, line 18
def matches?(path)
  return false if path.nil?

  send match_method, Pathname.new(path)
end

Private Instance Methods

compare_to_proc(other) click to toggle source
# File lib/middleman-webp/pathname_matcher.rb, line 64
def compare_to_proc(other)
  i_am_proc = @pattern.respond_to?(:call)
  other_is_proc = other.pattern.respond_to?(:call)

  if i_am_proc && !other_is_proc
    return -1
  elsif !i_am_proc && other_is_proc
    return 1
  end

  0
end
match_method() click to toggle source
# File lib/middleman-webp/pathname_matcher.rb, line 41
def match_method
  @match_method ||=
    if @pattern.respond_to? :call
      :matches_proc?
    elsif @pattern.class == Regexp
      :matches_re?
    else
      :matches_glob?
    end
end
matches_glob?(path) click to toggle source
# File lib/middleman-webp/pathname_matcher.rb, line 52
def matches_glob?(path)
  path.fnmatch?(@pattern)
end
matches_proc?(path) click to toggle source
# File lib/middleman-webp/pathname_matcher.rb, line 60
def matches_proc?(path)
  @pattern.call(path.to_s)
end
matches_re?(path) click to toggle source
# File lib/middleman-webp/pathname_matcher.rb, line 56
def matches_re?(path)
  !@pattern.match(path.to_s).nil?
end