Class: Domain

Inherits:
Object show all
Defined in:
lib/vagrant-dnsmasq/includes/Domain.class.rb

Constant Summary collapse

MATCH =
/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Domain

Returns a new instance of Domain.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vagrant-dnsmasq/includes/Domain.class.rb', line 5

def initialize(name)
  @name = nil

  if name.is_a? Domain
    name = name.dotted
  end

  raise ArgumentError, "no domain name given" if name.blank?

  # parse domain name ...
  name = name.to_s
  name = name[1..-1] if name.start_with? '.'
  name = name.downcase
  raise ArgumentError, "Domain '#{name}' must match #{MATCH}" unless Domain::valid?(name)
  @name = name # without leading .   
end

Class Method Details

.valid?(name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/vagrant-dnsmasq/includes/Domain.class.rb', line 22

def self.valid?(name)
  if not name.blank? and Domain::MATCH.match(name.downcase) then true else false end
end

Instance Method Details

#dottedObject



26
27
28
# File 'lib/vagrant-dnsmasq/includes/Domain.class.rb', line 26

def dotted
  '.' + @name
end

#nameObject



30
31
32
# File 'lib/vagrant-dnsmasq/includes/Domain.class.rb', line 30

def name
  @name
end

#to_sObject



34
35
36
# File 'lib/vagrant-dnsmasq/includes/Domain.class.rb', line 34

def to_s
  dotted
end