Sha256: 2237a65b8f33d9739207d43422a154ee707badcd5e9ca76711609569e13328e1

Contents?: true

Size: 1016 Bytes

Versions: 11

Compression:

Stored size: 1016 Bytes

Contents

module Middleman
  module WebP
    class PathnameMatcher
      # Initializes matcher with given pattern.
      #
      # pattern - Pattern to match pathnames against to. May be
      #           string, glob, prog or regex.
      def initialize(pattern = '**/*')
        @pattern = pattern
      end

      # Checks given file against pattern.
      #
      # file - File, Pathname or String
      def matches?(path)
        return false if path.nil?

        send match_method, Pathname.new(path)
      end

      private

      def match_method
        @match_method ||=
          if @pattern.respond_to? :call
            :matches_proc?
          elsif @pattern.class == Regexp
            :matches_re?
          else
            :matches_glob?
          end
      end

      def matches_glob?(path)
        path.fnmatch?(@pattern)
      end

      def matches_re?(path)
        !@pattern.match(path.to_s).nil?
      end

      def matches_proc?(path)
        @pattern.call(path.to_s)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
middleman-webp-1.0.1 lib/middleman-webp/pathname_matcher.rb
middleman-webp-1.0.0 lib/middleman-webp/pathname_matcher.rb
middleman-webp-0.4.3 lib/middleman-webp/pathname_matcher.rb
middleman-webp-0.4.2 lib/middleman-webp/pathname_matcher.rb
middleman-webp-0.4.1 lib/middleman-webp/pathname_matcher.rb
middleman-webp-0.4.0 lib/middleman-webp/pathname_matcher.rb
middleman-webp-0.3.2 lib/middleman-webp/pathname_matcher.rb
middleman-webp-0.3.1 lib/middleman-webp/pathname_matcher.rb
middleman-webp-0.3.0 lib/middleman-webp/pathname_matcher.rb
middleman-webp-0.2.7 lib/middleman-webp/pathname_matcher.rb
middleman-webp-0.2.6 lib/middleman-webp/pathname_matcher.rb