Sha256: 64fa97565a0353cc5abeb4d7c84dce93b0f4ad6da46c7720e8d9c51a5fec63b6

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

require "yaml"

module Matchd
  # Tthe dns pattern registry
  # It basically delegates everything to a YAML::Store but handles the conversion
  # of Regexp dns-patterns into YAML string keys and reverse.
  class Registry
    include Enumerable

    LoadError = Class.new(RuntimeError)

    class ParseError < RuntimeError
      def initialize(registry_file = nil)
        @registry_file = registry_file
      end

      def message
        msg = "Missing 'rules' key"
        msg += " in registry file '#{@registry_file}'" if @registry_file
        msg
      end
    end

    def initialize(rules)
      raise ArgumentError unless rules.is_a?(Enumerable)
      @rules = rules
    end

    attr_reader :rules

    # Loads a registry YAML file
    def self.load_file(registry_file)
      unless File.file?(registry_file)
        raise LoadError, "Registry file '#{registry_file}' does not exist"
      end

      load(YAML.load_file(registry_file), registry_file)
    end

    def self.load(data, registry_file = nil)
      raise ParseError, registry_file unless data.is_a?(Hash) && data.key?("rules")

      rules = data["rules"]

      new(rules ? parse(rules) : [])
    end

    # Parses raw rule hash definitions (like those read from a YAML config) into
    # `Matchd::Rule`s
    #
    # @param rules [Array<Hash>] the raw rule definitions
    # @return [Array<Matchd::Rule>]
    def self.parse(rules)
      rules = rules.is_a?(Array) ? rules : [rules]
      rules.map { |r| Matchd.Rule(r) }
    end

    def each(&block)
      rules.each(&block) if rules
    end

    def valid?
      none? { |r| r.is_a?(Matchd::Rule::Invalid) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
matchd-0.1.0 lib/matchd/registry.rb