Sha256: 52173981064002ad530c9ab0478acaa5197784d9bc6d33a3ff0ef13d86991771

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

module Nanoc::Int
  # @api private
  class Pattern
    def self.from(obj)
      case obj
      when Nanoc::Int::StringPattern, Nanoc::Int::RegexpPattern
        obj
      when String
        Nanoc::Int::StringPattern.new(obj)
      when Regexp
        Nanoc::Int::RegexpPattern.new(obj)
      else
        raise ArgumentError, "Do not know how to convert `#{obj.inspect}` into a Nanoc::Pattern"
      end
    end

    def initialize(_obj)
      raise NotImplementedError
    end

    def match?(_identifier)
      raise NotImplementedError
    end

    def captures(_identifier)
      raise NotImplementedError
    end
  end

  # @api private
  class StringPattern
    MATCH_OPTS = File::FNM_PATHNAME | File::FNM_EXTGLOB

    def initialize(string)
      @string = string
    end

    def match?(identifier)
      File.fnmatch(@string, identifier.to_s, MATCH_OPTS)
    end

    def captures(_identifier)
      nil
    end

    def to_s
      @string
    end
  end

  # @api private
  class RegexpPattern
    def initialize(regexp)
      @regexp = regexp
    end

    def match?(identifier)
      (identifier.to_s =~ @regexp) != nil
    end

    def captures(identifier)
      matches = @regexp.match(identifier.to_s)
      matches && matches.captures
    end

    def to_s
      @regexp.to_s
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nanoc-4.2.0 lib/nanoc/base/entities/pattern.rb
nanoc-4.2.0b1 lib/nanoc/base/entities/pattern.rb