Class: RubyDNS::RuleBasedServer::Rule

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

Overview

Represents a single rule in the server.

Instance Method Summary collapse

Constructor Details

#initialize(pattern, callback) ⇒ Rule

Returns a new instance of Rule.



136
137
138
139
# File 'lib/rubydns/server.rb', line 136

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

Instance Method Details

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

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



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rubydns/server.rb', line 155

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
	
	# Does this rule match against the supplied 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}."
			
			@callback[*args, match_data]
			
			return true
		end
	when String
		if @pattern[0] == name
			server.logger.debug "String pattern matched."
			
			@callback[*args]
			
			return true
		end
	else
		if (@pattern[0].call(name, resource_class) rescue false)
			server.logger.debug "Callable pattern matched."
			
			@callback[*args]
			
			return true
		end
	end
	
	server.logger.debug "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:



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rubydns/server.rb', line 142

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



198
199
200
# File 'lib/rubydns/server.rb', line 198

def to_s
	@pattern.inspect
end