Class: Hosts

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

Overview

Class for parsing and writing /etc/hosts file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHosts

Returns a new instance of Hosts.



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

def initialize
  @preamble = ""
  @postamble = ""
  @config = {}
  parse
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/hosts.rb', line 11

def config
  @config
end

Instance Method Details

#save(dev_domain = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hosts.rb', line 13

def save(dev_domain=nil)
  File.open('.tmp.hosts','w') do |f|
    f.write @preamble
    f.puts BEGIN_LINE

    @config.each do |ip, host|
      f.puts "#{ip}\t#{host}"
      f.puts "#{ip}\t#{host}.#{dev_domain}" if dev_domain
    end

    f.puts END_LINE
    f.write @postamble
  end
  system "sudo mv .tmp.hosts /etc/hosts"
end