Class: DevDNSd::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/devdnsd/rule.rb

Overview

This class encapsulate a rule for matching an hostname.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match: /.+/, reply: "127.0.0.1", type: :A, options: {}, &block) ⇒ Rule

Creates a new rule.

Parameters:

  • match (String|Regexp) (defaults to: /.+/)

    The pattern to match.

  • reply (String|Symbol) (defaults to: "127.0.0.1")

    The IP or hostname to reply back to the client. It can be omitted (and it will be ignored) if a block is provided.

  • type (Symbol) (defaults to: :A)

    The type of request to match.

  • options (Hash) (defaults to: {})

    A list of options for the request.

  • block (Proc)

    An optional block to compute the reply instead of using the reply parameter.

See Also:



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, options, block)
  validate_rule
end

Instance Attribute Details

#blockProc

Returns An optional block to compute the reply instead of using the reply parameter. @see .create.

Returns:

  • (Proc)

    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: 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, options, 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, options[: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, options, 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 = options
    @block = block
    locale = options.is_a?(Hash) ? options[: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.rule_invalid_options) unless @options.is_a?(::Hash)
  end
end

#matchString|Regexp

Returns The pattern to match. Default: /.+/.

Returns:

  • (String|Regexp)

    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: 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, options, 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, options[: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, options, 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 = options
    @block = block
    locale = options.is_a?(Hash) ? options[: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.rule_invalid_options) unless @options.is_a?(::Hash)
  end
end

#optionsHash

Returns A list of options for the request. Default is an empty hash. Supported key are :priority and :ttl, both integers.

Returns:

  • (Hash)

    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: 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, options, 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, options[: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, options, 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 = options
    @block = block
    locale = options.is_a?(Hash) ? options[: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.rule_invalid_options) unless @options.is_a?(::Hash)
  end
end

#replyString

Returns The IP or hostname to reply back to the client. Default: 127.0.0.1. @see .create.

Returns:

  • (String)

    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: 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, options, 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, options[: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, options, 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 = options
    @block = block
    locale = options.is_a?(Hash) ? options[: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.rule_invalid_options) unless @options.is_a?(::Hash)
  end
end

#typeSymbol

Returns The type of request to match. Default: :A. @see .create.

Returns:

  • (Symbol)

    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: 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, options, 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, options[: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, options, 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 = options
    @block = block
    locale = options.is_a?(Hash) ? options[: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.rule_invalid_options) 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.

Parameters:

  • match (String|Regexp) (defaults to: /.+/)

    The pattern to match.

  • reply (String|Symbol) (defaults to: "127.0.0.1")

    The IP or hostname to reply back to the client. It can be omitted (and it will be ignored) if a block is provided.

  • type (Symbol) (defaults to: :A)

    The type of request to match.

  • options (Hash) (defaults to: {})

    A list of options for the request.

  • block (Proc)

    An optional block to compute the reply instead of using the reply parameter.

Returns:

  • (Rule)

    The 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: options, &block)
end

.resource_class_to_symbol(klass) ⇒ Symbol

Converts a class to the correspondent symbol.

Parameters:

  • klass (Class)

    The class to convert.

Returns:

  • (Symbol)

    The symbol representation of the class.



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.

Parameters:

  • symbol (Symbol)

    The symbol to convert.

  • locale (Symbol) (defaults to: nil)

    The locale to use for the messages.

Returns:

  • (Symbol)

    The class associated to the symbol.



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.

Returns:

  • (Boolean)

    true if the rule has a block, false otherwise.



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.

Parameters:

  • hostname (String)

    The hostname to match.

Returns:

  • (MatchData|Boolean|Nil)

    Return true or MatchData (if the pattern is a regexp) if the rule matches, false or nil otherwise.



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.

Returns:

  • (Boolean)

    true if the rule is a Regexp, false otherwise.



90
91
92
# File 'lib/devdnsd/rule.rb', line 90

def regexp?
  @match.is_a?(::Regexp)
end

#resource_classArray|Class

Returns the resource class(es) for the current rule.

Returns:

  • (Array|Class)

    The 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, options[:locale]) }
  classes.length == 1 ? classes.first : classes
end