Sha256: 5f2d7cd90e539446ee9a482d0c38b52271ca5623f35f020d0979b67bb97e8586

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

module Apropos
  # ExtensionParser manages registered variant parsers and provides a base
  # class which new parsers subclass. Parsers are initialized with a pattern
  # (String or Regexp) and a block that is called to generate ClassList or
  # MediaQuery objects from the provided match data.
  class ExtensionParser
    @parsers = {}

    def self.parsers
      @parsers
    end

    def self.add_parser(extension, &block)
      @parsers[extension] = new(extension, &block)
    end

    def self.each_parser(&block)
      parsers.values.each(&block)
    end

    attr_reader :pattern

    def initialize(pattern, &block)
      @pattern = generate_pattern(pattern)
      @match_block = block
    end

    def match(extension)
      matchdata = pattern.match(extension)
      if matchdata
        @match_block.call(matchdata)
      end
    end

    private

    def generate_pattern(pattern)
      case pattern
      when String
        %r(^#{Regexp.escape(pattern)}$)
      else
        pattern
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
apropos-0.2.0 lib/apropos/extension_parser.rb
apropos-0.1.4 lib/apropos/extension_parser.rb