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
-
#block ⇒ Proc
An optional block to compute the reply instead of using the
replyparameter. -
#match ⇒ String|Regexp
The pattern to match.
-
#options ⇒ Hash
A list of options for the request.
-
#reply ⇒ String
The IP or hostname to reply back to the client.
-
#type ⇒ Symbol
The type of request to match.
Class Method Summary collapse
-
.create(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) ⇒ Rule
Creates a new rule.
-
.resource_class_to_symbol(klass) ⇒ Symbol
Converts a class to the correspondent symbol.
-
.symbol_to_resource_class(symbol, locale = nil) ⇒ Symbol
Converts a symbol to the correspondent DNS resource class.
Instance Method Summary collapse
-
#block? ⇒ Boolean
(also: #has_block?)
Checks if the rule is a regexp.
-
#initialize(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) ⇒ Rule
constructor
Creates a new rule.
-
#match_host(hostname) ⇒ MatchData|Boolean|Nil
Matches a hostname to the rule.
-
#regexp? ⇒ Boolean
(also: #is_regexp?)
Checks if the rule is a regexp.
-
#resource_class ⇒ Array|Class
Returns the resource class(es) for the current rule.
Constructor Details
#initialize(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) ⇒ Rule
Creates a new rule.
74 75 76 77 |
# File 'lib/devdnsd/rule.rb', line 74 def initialize(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) setup(match, reply, type, , block) validate_rule end |
Instance Attribute Details
#block ⇒ Proc
Returns An optional block to compute the reply instead of using the reply parameter. @see .create.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/devdnsd/rule.rb', line 20 class Rule attr_accessor :match attr_accessor :type attr_accessor :reply attr_accessor :options attr_accessor :block # Class methods class << self # Creates a new rule. # # @param match [String|Regexp] The pattern to match. # @param reply [String|Symbol] The IP or hostname to reply back to the client. It can be omitted (and it will be ignored) if a block is provided. # @param type [Symbol] The type of request to match. # @param options [Hash] A list of options for the request. # @param block [Proc] An optional block to compute the reply instead of using the `reply` parameter. # @return [Rule] The new rule. def create(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) new(match: match, reply: reply, type: type, options: , &block) end # Converts a class to the correspondent symbol. # # @param klass [Class] The class to convert. # @return [Symbol] The symbol representation of the class. def resource_class_to_symbol(klass) klass.to_s.gsub(/(.+::)?(.+)/, "\\2").to_sym end # Converts a symbol to the correspondent DNS resource class. # # @param symbol [Symbol] The symbol to convert. # @param locale [Symbol] The locale to use for the messages. # @return [Symbol] The class associated to the symbol. def symbol_to_resource_class(symbol, locale = nil) symbol = symbol.to_s.upcase begin "Resolv::DNS::Resource::IN::#{symbol}".constantize rescue ::NameError i18n = Bovem::I18n.new(locale, root: "devdnsd", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") raise(DevDNSd::Errors::InvalidRule, i18n.rule_invalid_resource(symbol)) end end end # Creates a new rule. # # @param match [String|Regexp] The pattern to match. # @param reply [String|Symbol] The IP or hostname to reply back to the client. It can be omitted (and it will be ignored) if a block is provided. # @param type [Symbol] The type of request to match. # @param options [Hash] A list of options for the request. # @param block [Proc] An optional block to compute the reply instead of using the `reply` parameter. # @see .create def initialize(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) setup(match, reply, type, , block) validate_rule end # Returns the resource class(es) for the current rule. # # @return [Array|Class] The class(es) for the current rule. def resource_class classes = @type.ensure_array(no_duplicates: true, compact: true, flatten: true) { |cls| self.class.symbol_to_resource_class(cls, [:locale]) } classes.length == 1 ? classes.first : classes end # Checks if the rule is a regexp. # # @return [Boolean] `true` if the rule is a Regexp, `false` otherwise. def regexp? @match.is_a?(::Regexp) end alias_method :is_regexp?, :regexp? # Checks if the rule is a regexp. # # @return [Boolean] `true` if the rule has a block, `false` otherwise. def block? @block.present? end alias_method :has_block?, :block? # Matches a hostname to the rule. # # @param hostname [String] The hostname to match. # @return [MatchData|Boolean|Nil] Return `true` or MatchData (if the pattern is a regexp) if the rule matches, `false` or `nil` otherwise. def match_host(hostname) regexp? ? @match.match(hostname) : (@match == hostname) end private # :nodoc: def setup(match, reply, type, , block) @match = match if block.present? # reply acts like a type, type is ignored @type = type || :A @reply = nil else # reply acts like a reply @reply = reply || "127.0.0.1" @type = type || :A end @options = @block = block locale = .is_a?(Hash) ? [:locale] : :en @i18n = Bovem::I18n.new(locale, root: "devdnsd", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") end # Validates a newly created rule. def validate_rule raise(DevDNSd::Errors::InvalidRule, @i18n.rule_invalid_call) if @reply.blank? && @block.nil? raise(DevDNSd::Errors::InvalidRule, @i18n.) unless @options.is_a?(::Hash) end end |
#match ⇒ String|Regexp
Returns The pattern to match. Default: /.+/.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/devdnsd/rule.rb', line 20 class Rule attr_accessor :match attr_accessor :type attr_accessor :reply attr_accessor :options attr_accessor :block # Class methods class << self # Creates a new rule. # # @param match [String|Regexp] The pattern to match. # @param reply [String|Symbol] The IP or hostname to reply back to the client. It can be omitted (and it will be ignored) if a block is provided. # @param type [Symbol] The type of request to match. # @param options [Hash] A list of options for the request. # @param block [Proc] An optional block to compute the reply instead of using the `reply` parameter. # @return [Rule] The new rule. def create(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) new(match: match, reply: reply, type: type, options: , &block) end # Converts a class to the correspondent symbol. # # @param klass [Class] The class to convert. # @return [Symbol] The symbol representation of the class. def resource_class_to_symbol(klass) klass.to_s.gsub(/(.+::)?(.+)/, "\\2").to_sym end # Converts a symbol to the correspondent DNS resource class. # # @param symbol [Symbol] The symbol to convert. # @param locale [Symbol] The locale to use for the messages. # @return [Symbol] The class associated to the symbol. def symbol_to_resource_class(symbol, locale = nil) symbol = symbol.to_s.upcase begin "Resolv::DNS::Resource::IN::#{symbol}".constantize rescue ::NameError i18n = Bovem::I18n.new(locale, root: "devdnsd", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") raise(DevDNSd::Errors::InvalidRule, i18n.rule_invalid_resource(symbol)) end end end # Creates a new rule. # # @param match [String|Regexp] The pattern to match. # @param reply [String|Symbol] The IP or hostname to reply back to the client. It can be omitted (and it will be ignored) if a block is provided. # @param type [Symbol] The type of request to match. # @param options [Hash] A list of options for the request. # @param block [Proc] An optional block to compute the reply instead of using the `reply` parameter. # @see .create def initialize(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) setup(match, reply, type, , block) validate_rule end # Returns the resource class(es) for the current rule. # # @return [Array|Class] The class(es) for the current rule. def resource_class classes = @type.ensure_array(no_duplicates: true, compact: true, flatten: true) { |cls| self.class.symbol_to_resource_class(cls, [:locale]) } classes.length == 1 ? classes.first : classes end # Checks if the rule is a regexp. # # @return [Boolean] `true` if the rule is a Regexp, `false` otherwise. def regexp? @match.is_a?(::Regexp) end alias_method :is_regexp?, :regexp? # Checks if the rule is a regexp. # # @return [Boolean] `true` if the rule has a block, `false` otherwise. def block? @block.present? end alias_method :has_block?, :block? # Matches a hostname to the rule. # # @param hostname [String] The hostname to match. # @return [MatchData|Boolean|Nil] Return `true` or MatchData (if the pattern is a regexp) if the rule matches, `false` or `nil` otherwise. def match_host(hostname) regexp? ? @match.match(hostname) : (@match == hostname) end private # :nodoc: def setup(match, reply, type, , block) @match = match if block.present? # reply acts like a type, type is ignored @type = type || :A @reply = nil else # reply acts like a reply @reply = reply || "127.0.0.1" @type = type || :A end @options = @block = block locale = .is_a?(Hash) ? [:locale] : :en @i18n = Bovem::I18n.new(locale, root: "devdnsd", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") end # Validates a newly created rule. def validate_rule raise(DevDNSd::Errors::InvalidRule, @i18n.rule_invalid_call) if @reply.blank? && @block.nil? raise(DevDNSd::Errors::InvalidRule, @i18n.) unless @options.is_a?(::Hash) end end |
#options ⇒ Hash
Returns A list of options for the request. Default is an empty hash. Supported key are :priority and :ttl, both integers.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/devdnsd/rule.rb', line 20 class Rule attr_accessor :match attr_accessor :type attr_accessor :reply attr_accessor :options attr_accessor :block # Class methods class << self # Creates a new rule. # # @param match [String|Regexp] The pattern to match. # @param reply [String|Symbol] The IP or hostname to reply back to the client. It can be omitted (and it will be ignored) if a block is provided. # @param type [Symbol] The type of request to match. # @param options [Hash] A list of options for the request. # @param block [Proc] An optional block to compute the reply instead of using the `reply` parameter. # @return [Rule] The new rule. def create(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) new(match: match, reply: reply, type: type, options: , &block) end # Converts a class to the correspondent symbol. # # @param klass [Class] The class to convert. # @return [Symbol] The symbol representation of the class. def resource_class_to_symbol(klass) klass.to_s.gsub(/(.+::)?(.+)/, "\\2").to_sym end # Converts a symbol to the correspondent DNS resource class. # # @param symbol [Symbol] The symbol to convert. # @param locale [Symbol] The locale to use for the messages. # @return [Symbol] The class associated to the symbol. def symbol_to_resource_class(symbol, locale = nil) symbol = symbol.to_s.upcase begin "Resolv::DNS::Resource::IN::#{symbol}".constantize rescue ::NameError i18n = Bovem::I18n.new(locale, root: "devdnsd", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") raise(DevDNSd::Errors::InvalidRule, i18n.rule_invalid_resource(symbol)) end end end # Creates a new rule. # # @param match [String|Regexp] The pattern to match. # @param reply [String|Symbol] The IP or hostname to reply back to the client. It can be omitted (and it will be ignored) if a block is provided. # @param type [Symbol] The type of request to match. # @param options [Hash] A list of options for the request. # @param block [Proc] An optional block to compute the reply instead of using the `reply` parameter. # @see .create def initialize(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) setup(match, reply, type, , block) validate_rule end # Returns the resource class(es) for the current rule. # # @return [Array|Class] The class(es) for the current rule. def resource_class classes = @type.ensure_array(no_duplicates: true, compact: true, flatten: true) { |cls| self.class.symbol_to_resource_class(cls, [:locale]) } classes.length == 1 ? classes.first : classes end # Checks if the rule is a regexp. # # @return [Boolean] `true` if the rule is a Regexp, `false` otherwise. def regexp? @match.is_a?(::Regexp) end alias_method :is_regexp?, :regexp? # Checks if the rule is a regexp. # # @return [Boolean] `true` if the rule has a block, `false` otherwise. def block? @block.present? end alias_method :has_block?, :block? # Matches a hostname to the rule. # # @param hostname [String] The hostname to match. # @return [MatchData|Boolean|Nil] Return `true` or MatchData (if the pattern is a regexp) if the rule matches, `false` or `nil` otherwise. def match_host(hostname) regexp? ? @match.match(hostname) : (@match == hostname) end private # :nodoc: def setup(match, reply, type, , block) @match = match if block.present? # reply acts like a type, type is ignored @type = type || :A @reply = nil else # reply acts like a reply @reply = reply || "127.0.0.1" @type = type || :A end @options = @block = block locale = .is_a?(Hash) ? [:locale] : :en @i18n = Bovem::I18n.new(locale, root: "devdnsd", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") end # Validates a newly created rule. def validate_rule raise(DevDNSd::Errors::InvalidRule, @i18n.rule_invalid_call) if @reply.blank? && @block.nil? raise(DevDNSd::Errors::InvalidRule, @i18n.) unless @options.is_a?(::Hash) end end |
#reply ⇒ String
Returns The IP or hostname to reply back to the client. Default: 127.0.0.1. @see .create.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/devdnsd/rule.rb', line 20 class Rule attr_accessor :match attr_accessor :type attr_accessor :reply attr_accessor :options attr_accessor :block # Class methods class << self # Creates a new rule. # # @param match [String|Regexp] The pattern to match. # @param reply [String|Symbol] The IP or hostname to reply back to the client. It can be omitted (and it will be ignored) if a block is provided. # @param type [Symbol] The type of request to match. # @param options [Hash] A list of options for the request. # @param block [Proc] An optional block to compute the reply instead of using the `reply` parameter. # @return [Rule] The new rule. def create(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) new(match: match, reply: reply, type: type, options: , &block) end # Converts a class to the correspondent symbol. # # @param klass [Class] The class to convert. # @return [Symbol] The symbol representation of the class. def resource_class_to_symbol(klass) klass.to_s.gsub(/(.+::)?(.+)/, "\\2").to_sym end # Converts a symbol to the correspondent DNS resource class. # # @param symbol [Symbol] The symbol to convert. # @param locale [Symbol] The locale to use for the messages. # @return [Symbol] The class associated to the symbol. def symbol_to_resource_class(symbol, locale = nil) symbol = symbol.to_s.upcase begin "Resolv::DNS::Resource::IN::#{symbol}".constantize rescue ::NameError i18n = Bovem::I18n.new(locale, root: "devdnsd", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") raise(DevDNSd::Errors::InvalidRule, i18n.rule_invalid_resource(symbol)) end end end # Creates a new rule. # # @param match [String|Regexp] The pattern to match. # @param reply [String|Symbol] The IP or hostname to reply back to the client. It can be omitted (and it will be ignored) if a block is provided. # @param type [Symbol] The type of request to match. # @param options [Hash] A list of options for the request. # @param block [Proc] An optional block to compute the reply instead of using the `reply` parameter. # @see .create def initialize(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) setup(match, reply, type, , block) validate_rule end # Returns the resource class(es) for the current rule. # # @return [Array|Class] The class(es) for the current rule. def resource_class classes = @type.ensure_array(no_duplicates: true, compact: true, flatten: true) { |cls| self.class.symbol_to_resource_class(cls, [:locale]) } classes.length == 1 ? classes.first : classes end # Checks if the rule is a regexp. # # @return [Boolean] `true` if the rule is a Regexp, `false` otherwise. def regexp? @match.is_a?(::Regexp) end alias_method :is_regexp?, :regexp? # Checks if the rule is a regexp. # # @return [Boolean] `true` if the rule has a block, `false` otherwise. def block? @block.present? end alias_method :has_block?, :block? # Matches a hostname to the rule. # # @param hostname [String] The hostname to match. # @return [MatchData|Boolean|Nil] Return `true` or MatchData (if the pattern is a regexp) if the rule matches, `false` or `nil` otherwise. def match_host(hostname) regexp? ? @match.match(hostname) : (@match == hostname) end private # :nodoc: def setup(match, reply, type, , block) @match = match if block.present? # reply acts like a type, type is ignored @type = type || :A @reply = nil else # reply acts like a reply @reply = reply || "127.0.0.1" @type = type || :A end @options = @block = block locale = .is_a?(Hash) ? [:locale] : :en @i18n = Bovem::I18n.new(locale, root: "devdnsd", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") end # Validates a newly created rule. def validate_rule raise(DevDNSd::Errors::InvalidRule, @i18n.rule_invalid_call) if @reply.blank? && @block.nil? raise(DevDNSd::Errors::InvalidRule, @i18n.) unless @options.is_a?(::Hash) end end |
#type ⇒ Symbol
Returns The type of request to match. Default: :A. @see .create.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/devdnsd/rule.rb', line 20 class Rule attr_accessor :match attr_accessor :type attr_accessor :reply attr_accessor :options attr_accessor :block # Class methods class << self # Creates a new rule. # # @param match [String|Regexp] The pattern to match. # @param reply [String|Symbol] The IP or hostname to reply back to the client. It can be omitted (and it will be ignored) if a block is provided. # @param type [Symbol] The type of request to match. # @param options [Hash] A list of options for the request. # @param block [Proc] An optional block to compute the reply instead of using the `reply` parameter. # @return [Rule] The new rule. def create(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) new(match: match, reply: reply, type: type, options: , &block) end # Converts a class to the correspondent symbol. # # @param klass [Class] The class to convert. # @return [Symbol] The symbol representation of the class. def resource_class_to_symbol(klass) klass.to_s.gsub(/(.+::)?(.+)/, "\\2").to_sym end # Converts a symbol to the correspondent DNS resource class. # # @param symbol [Symbol] The symbol to convert. # @param locale [Symbol] The locale to use for the messages. # @return [Symbol] The class associated to the symbol. def symbol_to_resource_class(symbol, locale = nil) symbol = symbol.to_s.upcase begin "Resolv::DNS::Resource::IN::#{symbol}".constantize rescue ::NameError i18n = Bovem::I18n.new(locale, root: "devdnsd", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") raise(DevDNSd::Errors::InvalidRule, i18n.rule_invalid_resource(symbol)) end end end # Creates a new rule. # # @param match [String|Regexp] The pattern to match. # @param reply [String|Symbol] The IP or hostname to reply back to the client. It can be omitted (and it will be ignored) if a block is provided. # @param type [Symbol] The type of request to match. # @param options [Hash] A list of options for the request. # @param block [Proc] An optional block to compute the reply instead of using the `reply` parameter. # @see .create def initialize(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) setup(match, reply, type, , block) validate_rule end # Returns the resource class(es) for the current rule. # # @return [Array|Class] The class(es) for the current rule. def resource_class classes = @type.ensure_array(no_duplicates: true, compact: true, flatten: true) { |cls| self.class.symbol_to_resource_class(cls, [:locale]) } classes.length == 1 ? classes.first : classes end # Checks if the rule is a regexp. # # @return [Boolean] `true` if the rule is a Regexp, `false` otherwise. def regexp? @match.is_a?(::Regexp) end alias_method :is_regexp?, :regexp? # Checks if the rule is a regexp. # # @return [Boolean] `true` if the rule has a block, `false` otherwise. def block? @block.present? end alias_method :has_block?, :block? # Matches a hostname to the rule. # # @param hostname [String] The hostname to match. # @return [MatchData|Boolean|Nil] Return `true` or MatchData (if the pattern is a regexp) if the rule matches, `false` or `nil` otherwise. def match_host(hostname) regexp? ? @match.match(hostname) : (@match == hostname) end private # :nodoc: def setup(match, reply, type, , block) @match = match if block.present? # reply acts like a type, type is ignored @type = type || :A @reply = nil else # reply acts like a reply @reply = reply || "127.0.0.1" @type = type || :A end @options = @block = block locale = .is_a?(Hash) ? [:locale] : :en @i18n = Bovem::I18n.new(locale, root: "devdnsd", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") end # Validates a newly created rule. def validate_rule raise(DevDNSd::Errors::InvalidRule, @i18n.rule_invalid_call) if @reply.blank? && @block.nil? raise(DevDNSd::Errors::InvalidRule, @i18n.) unless @options.is_a?(::Hash) end end |
Class Method Details
.create(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) ⇒ Rule
Creates a new rule.
37 38 39 |
# File 'lib/devdnsd/rule.rb', line 37 def create(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) new(match: match, reply: reply, type: type, options: , &block) end |
.resource_class_to_symbol(klass) ⇒ Symbol
Converts a class to the correspondent symbol.
45 46 47 |
# File 'lib/devdnsd/rule.rb', line 45 def resource_class_to_symbol(klass) klass.to_s.gsub(/(.+::)?(.+)/, "\\2").to_sym end |
.symbol_to_resource_class(symbol, locale = nil) ⇒ Symbol
Converts a symbol to the correspondent DNS resource class.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/devdnsd/rule.rb', line 54 def symbol_to_resource_class(symbol, locale = nil) symbol = symbol.to_s.upcase begin "Resolv::DNS::Resource::IN::#{symbol}".constantize rescue ::NameError i18n = Bovem::I18n.new(locale, root: "devdnsd", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/") raise(DevDNSd::Errors::InvalidRule, i18n.rule_invalid_resource(symbol)) end end |
Instance Method Details
#block? ⇒ Boolean Also known as: has_block?
Checks if the rule is a regexp.
98 99 100 |
# File 'lib/devdnsd/rule.rb', line 98 def block? @block.present? end |
#match_host(hostname) ⇒ MatchData|Boolean|Nil
Matches a hostname to the rule.
107 108 109 |
# File 'lib/devdnsd/rule.rb', line 107 def match_host(hostname) regexp? ? @match.match(hostname) : (@match == hostname) end |
#regexp? ⇒ Boolean Also known as: is_regexp?
Checks if the rule is a regexp.
90 91 92 |
# File 'lib/devdnsd/rule.rb', line 90 def regexp? @match.is_a?(::Regexp) end |
#resource_class ⇒ Array|Class
Returns the resource class(es) for the current rule.
82 83 84 85 |
# File 'lib/devdnsd/rule.rb', line 82 def resource_class classes = @type.ensure_array(no_duplicates: true, compact: true, flatten: true) { |cls| self.class.symbol_to_resource_class(cls, [:locale]) } classes.length == 1 ? classes.first : classes end |