Class: Matchd::Registry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/matchd/registry.rb

Overview

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.

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

LoadError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules) ⇒ Registry

Returns a new instance of Registry.

Raises:

  • (ArgumentError)


24
25
26
27
# File 'lib/matchd/registry.rb', line 24

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

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



29
30
31
# File 'lib/matchd/registry.rb', line 29

def rules
  @rules
end

Class Method Details

.load(data, registry_file = nil) ⇒ Object

Raises:



40
41
42
43
44
45
46
# File 'lib/matchd/registry.rb', line 40

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

.load_file(registry_file) ⇒ Object

Loads a registry YAML file



32
33
34
35
36
37
38
# File 'lib/matchd/registry.rb', line 32

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

.parse(rules) ⇒ Array<Matchd::Rule>

Parses raw rule hash definitions (like those read from a YAML config) into ‘Matchd::Rule`s

Parameters:

  • rules (Array<Hash>)

    the raw rule definitions

Returns:



53
54
55
56
# File 'lib/matchd/registry.rb', line 53

def self.parse(rules)
  rules = rules.is_a?(Array) ? rules : [rules]
  rules.map { |r| Matchd.Rule(r) }
end

Instance Method Details

#each(&block) ⇒ Object



58
59
60
# File 'lib/matchd/registry.rb', line 58

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

#valid?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/matchd/registry.rb', line 62

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