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.



141
142
143
144
# File 'lib/rubydns/server.rb', line 141

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.



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
197
198
199
200
201
# File 'lib/rubydns/server.rb', line 160

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:



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rubydns/server.rb', line 147

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



203
204
205
# File 'lib/rubydns/server.rb', line 203

def to_s
  @pattern.inspect
end