Class: Hosts
- Inherits:
-
Object
- Object
- Hosts
- Defined in:
- lib/hosts.rb
Overview
Class for parsing and writing /etc/hosts file
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#initialize ⇒ Hosts
constructor
A new instance of Hosts.
- #save(dev_domain = nil) ⇒ Object
Constructor Details
#initialize ⇒ Hosts
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
#config ⇒ Object (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 |