Class: DevDNSd::Rule
- Inherits:
-
Object
- Object
- DevDNSd::Rule
- Defined in:
- lib/devdnsd/rule.rb
Overview
This class encapsulate a rule for matching an hostname.
Instance Attribute Summary (collapse)
-
- (Object) block
An optional block to compute the reply instead of using the
reply
parameter. -
- (Object) match
The pattern to match.
-
- (Object) options
A list of options for the request.
-
- (Object) reply
The IP or hostname to reply back to the client.
-
- (Object) type
The type of request to match.
Class Method Summary (collapse)
-
+ (Object) create(match, reply_or_type = nil, type = nil, options = {}, &block)
Creates a new rule.
-
+ (Symbol) resource_class_to_symbol(klass)
Converts a class to the correspondent symbol.
-
+ (Symbol) symbol_to_resource_class(symbol)
Converts a symbol to the correspondent DNS resource class.
Instance Method Summary (collapse)
-
- (Boolean) has_block?
Checks if the rule is a regexp.
-
- (Rule) initialize(match = /.+/, reply = "127.0.0.1", type = :A, options = {}, &block)
constructor
Creates a new rule.
-
- (Boolean) is_regexp?
Checks if the rule is a regexp.
-
- (MatchData|Boolean|Nil) match_host(hostname)
Matches a hostname to the rule.
-
- (Array|Class) resource_class
Returns the resource class(es) for the current rule.
Constructor Details
- (Rule) initialize(match = /.+/, reply = "127.0.0.1", type = :A, options = {}, &block)
Creates a new rule.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/devdnsd/rule.rb', line 39 def initialize(match = /.+/, reply = "127.0.0.1", type = :A, = {}, &block) reply ||= "127.0.0.1" type ||= :A @match = match @type = type @reply = block.blank? ? reply : nil @options = @block = block raise(DevDNSd::Errors::InvalidRule.new("You must specify at least a rule and a host (also via a block). Optionally you can add a record type (default: A) and the options.")) if @reply.blank? && @block.nil? raise(DevDNSd::Errors::InvalidRule.new("You can only use hashs for options.")) if !@options.is_a?(::Hash) end |
Instance Attribute Details
- (Object) block
An optional block to compute the reply instead of using the reply
parameter.
29 30 31 |
# File 'lib/devdnsd/rule.rb', line 29 def block @block end |
- (Object) match
The pattern to match. Default: /.+/
.
11 12 13 |
# File 'lib/devdnsd/rule.rb', line 11 def match @match end |
- (Object) options
A list of options for the request. Default is an empty hash.
24 25 26 |
# File 'lib/devdnsd/rule.rb', line 24 def @options end |
- (Object) reply
The IP or hostname to reply back to the client. Default: 127.0.0.1
.
21 22 23 |
# File 'lib/devdnsd/rule.rb', line 21 def reply @reply end |
- (Object) type
The type of request to match. Default: :A
.
16 17 18 |
# File 'lib/devdnsd/rule.rb', line 16 def type @type end |
Class Method Details
+ (Object) create(match, reply_or_type = nil, type = nil, options = {}, &block)
Creates a new rule.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/devdnsd/rule.rb', line 89 def self.create(match, reply_or_type = nil, type = nil, = {}, &block) raise(DevDNSd::Errors::InvalidRule.new("You must specify at least a rule and a host (also via a block). Optionally you can add a record type (default: A) and the options.")) if reply_or_type.blank? && block.nil? raise(DevDNSd::Errors::InvalidRule.new("You can only use hashs for options.")) if !.is_a?(::Hash) rv = self.new(match) rv. = if block.present? then # reply_or_type acts like a type, type is ignored rv.type = reply_or_type || :A rv.reply = nil rv.block = block else # reply_or_type acts like a reply rv.reply = reply_or_type || "127.0.0.1" rv.type = type || :A end rv end |
+ (Symbol) resource_class_to_symbol(klass)
Converts a class to the correspondent symbol.
112 113 114 |
# File 'lib/devdnsd/rule.rb', line 112 def self.resource_class_to_symbol(klass) klass.to_s.gsub(/(.+::)?(.+)/, "\\2").to_sym end |
+ (Symbol) symbol_to_resource_class(symbol)
Converts a symbol to the correspondent DNS resource class.
120 121 122 123 124 125 126 127 128 |
# File 'lib/devdnsd/rule.rb', line 120 def self.symbol_to_resource_class(symbol) symbol = symbol.to_s.upcase begin "Resolv::DNS::Resource::IN::#{symbol}".constantize rescue ::NameError raise(DevDNSd::Errors::InvalidRule.new("Invalid resource class #{symbol}.")) end end |
Instance Method Details
- (Boolean) has_block?
Checks if the rule is a regexp.
70 71 72 |
# File 'lib/devdnsd/rule.rb', line 70 def has_block? self.block.present? end |
- (Boolean) is_regexp?
Checks if the rule is a regexp.
63 64 65 |
# File 'lib/devdnsd/rule.rb', line 63 def is_regexp? self.match.is_a?(::Regexp) end |
- (MatchData|Boolean|Nil) match_host(hostname)
Matches a hostname to the rule.
78 79 80 |
# File 'lib/devdnsd/rule.rb', line 78 def match_host(hostname) self.is_regexp? ? self.match.match(hostname) : (self.match == hostname) end |
- (Array|Class) resource_class
Returns the resource class(es) for the current rule.
55 56 57 58 |
# File 'lib/devdnsd/rule.rb', line 55 def resource_class classes = self.type.ensure_array.collect {|cls| self.class.symbol_to_resource_class(cls) }.compact.uniq classes.length == 1 ? classes.first : classes end |