Class: RubyDNS::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/rubydns/rule.rb

Overview

Represents a single rule in the server.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, callback) ⇒ Rule

Create a new rule with a given pattern and callback.



25
26
27
28
# File 'lib/rubydns/rule.rb', line 25

def initialize(pattern, callback)
	@pattern = pattern
	@callback = callback
end

Class Method Details

.for(pattern, &block) ⇒ Object

Create a new rule with a given pattern and callback.



17
18
19
# File 'lib/rubydns/rule.rb', line 17

def self.for(pattern, &block)
	new(pattern, block)
end

Instance Method Details

#call(server, name, resource_class, transaction) ⇒ Object

Invoke the rule, if it matches the incoming request, it is evaluated and returns true, otherwise returns false.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rubydns/rule.rb', line 44

def call(server, name, resource_class, transaction)
	unless match(name, resource_class)
		Console.debug "<#{transaction.query.id}> Resource class #{resource_class} failed to match #{@pattern[1].inspect}!"
		
		return false
	end
	
	# Does this rule match against the supplied name?
	case @pattern[0]
	when Regexp
		match_data = @pattern[0].match(name)
		
		if match_data
			Console.debug "<#{transaction.query.id}> Regexp pattern matched with #{match_data.inspect}."
			
			@callback.call(transaction, match_data)
			
			return true
		end
	when String
		if @pattern[0] == name
			Console.debug "<#{transaction.query.id}> String pattern matched."
			
			@callback.call(transaction)
			
			return true
		end
	else
		if (@pattern[0].call(name, resource_class) rescue false)
			Console.debug "<#{transaction.query.id}> Callable pattern matched."
			
			@callback.call(transaction)
			
			return true
		end
	end
	
	Console.debug "<#{transaction.query.id}> No pattern matched."
	
	# We failed to match the pattern.
	return false
end

#match(name, resource_class) ⇒ Object

Returns true if the name and resource_class are sufficient:



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubydns/rule.rb', line 31

def match(name, resource_class)
	# If the pattern doesn't specify any resource classes, we implicitly pass this test:
	return true if @pattern.size < 2
	
	# Otherwise, we try to match against some specific resource classes:
	if Class === @pattern[1]
		@pattern[1] == resource_class
	else
		@pattern[1].include?(resource_class) rescue false
	end
end

#to_sObject

Return a string representation of the rule.



88
89
90
# File 'lib/rubydns/rule.rb', line 88

def to_s
	"#<#{self.class} #{@pattern}>"
end