Class: Middleman::WebP::PathnameMatcher

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/middleman-webp/pathname_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern = '**/*') ⇒ PathnameMatcher

Initializes matcher with given pattern.

pattern - Pattern to match pathnames against to. May be

string, glob, prog or regex.


11
12
13
# File 'lib/middleman-webp/pathname_matcher.rb', line 11

def initialize(pattern = '**/*')
  @pattern = pattern
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



5
6
7
# File 'lib/middleman-webp/pathname_matcher.rb', line 5

def pattern
  @pattern
end

Instance Method Details

#<=>(other) ⇒ Object

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.



28
29
30
31
32
33
# 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

#hashObject



35
36
37
# File 'lib/middleman-webp/pathname_matcher.rb', line 35

def hash
  @pattern.hash
end

#matches?(path) ⇒ Boolean

Checks given file against pattern.

file - File, Pathname or String

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/middleman-webp/pathname_matcher.rb', line 18

def matches?(path)
  return false if path.nil?

  send match_method, Pathname.new(path)
end