Class: RubyDNS::Server::Rule

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

Instance Method Summary collapse

Constructor Details

#initialize(pattern, callback) ⇒ Rule

Returns a new instance of Rule.



31
32
33
34
# File 'lib/rubydns/server.rb', line 31

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

Instance Method Details

#call(server, name, resource_class, *args) ⇒ Object



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
# File 'lib/rubydns/server.rb', line 48

def call(server, name, resource_class, *args)
	unless match(name, resource_class)
		server.logger.debug "Resource class #{resource_class} failed to match #{@pattern[1].inspect}!"
		
		return false
	end
	
	# Match succeeded against name?
	case @pattern[0]
	when Regexp
		match_data = @pattern[0].match(name)
		if match_data
			server.logger.debug "Regexp pattern matched with #{match_data.inspect}."
			return @callback[*args, match_data]
		end
	when String
		if @pattern[0] == name
			server.logger.debug "String pattern matched."
			return @callback[*args]
		end
	else
		if (@pattern[0].call(name, resource_class) rescue false)
			server.logger.debug "Callable pattern matched."
			return @callback[*args]
		end
	end
	
	server.logger.debug "No pattern matched."
	# We failed to match the pattern.
	return false
end

#match(name, resource_class) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubydns/server.rb', line 36

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



80
81
82
# File 'lib/rubydns/server.rb', line 80

def to_s
	@pattern.inspect
end