Class: Hosum::Hosts

Inherits:
Container show all
Defined in:
lib/hosum/hosts.rb

Instance Attribute Summary collapse

Attributes inherited from Container

#children, #ips_list, #options, #parent

Instance Method Summary collapse

Methods inherited from Container

#find_first_ip

Constructor Details

#initializeHosts

Returns a new instance of Hosts.



5
6
7
8
# File 'lib/hosum/hosts.rb', line 5

def initialize
  super
  self.domains = []
end

Instance Attribute Details

#domainsObject

Returns the value of attribute domains.



3
4
5
# File 'lib/hosum/hosts.rb', line 3

def domains
  @domains
end

Instance Method Details

#expand_domainsObject



10
11
12
13
14
15
16
17
18
19
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
# File 'lib/hosum/hosts.rb', line 10

def expand_domains
  self.domains.map do |domain|
    holders = []
    
    regex = /\[([^\[\]]+)-([^\[\]]+)\]/
    domain = domain.gsub(regex) do |str|
      m = str.match(regex)
      holders << Range.new(m[1], m[2]).to_a
      "%s"
    end
    
    expand_list = []
    holders.each do |holder|
      if expand_list.empty?
        expand_list = holder.map {|v| [v] }
        next
      end

      tmp = []
      expand_list.each do |expand|
        holder.each do |h|
          tmp << expand + [ h ]
        end
      end
      expand_list = tmp
    end
    
    if holders.empty?
      domain
    else
      expand_list.map { |e| domain % e}
    end
    
  end.flatten
end

#to_dnsmasq(options = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/hosum/hosts.rb', line 50

def to_dnsmasq(options = {})
  domains = self.expand_domains
  if self.options[:dnsmasq]
    domains = [ self.options[:dnsmasq] ].flatten
  end
  
  domains.map{ |domain| "address=/#{domain}/#{find_first_ip}\n" }.join
end

#to_hosts(options = {}) ⇒ Object



46
47
48
# File 'lib/hosum/hosts.rb', line 46

def to_hosts(options = {})
  self.expand_domains.map{ |domain| "#{find_first_ip}	#{domain}\n" }.join
end