Class: Hostupdater

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

Constant Summary collapse

VERSION =
'1.0.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostfile) ⇒ Hostupdater

Returns a new instance of Hostupdater.



7
8
9
10
# File 'lib/hostupdater.rb', line 7

def initialize( hostfile )
  @hostfile = hostfile
  init_empty_file
end

Instance Attribute Details

#hostfileObject

Returns the value of attribute hostfile.



5
6
7
# File 'lib/hostupdater.rb', line 5

def hostfile
  @hostfile
end

#hostmapObject

Returns the value of attribute hostmap.



5
6
7
# File 'lib/hostupdater.rb', line 5

def hostmap
  @hostmap
end

Instance Method Details

#add(hostname, hostip) ⇒ Object



37
38
39
40
41
# File 'lib/hostupdater.rb', line 37

def add( hostname, hostip )
  load
  @hostmap [ hostname ] = hostip
  store
end

#delete_by_hostname(hostname) ⇒ Object



43
44
45
46
47
# File 'lib/hostupdater.rb', line 43

def delete_by_hostname( hostname )
  load
  @hostmap.delete hostname
  store
end

#init_empty_fileObject



12
13
14
15
# File 'lib/hostupdater.rb', line 12

def init_empty_file
  FileUtils.touch @hostfile
  load
end

#loadObject



17
18
19
20
21
22
23
24
25
# File 'lib/hostupdater.rb', line 17

def load
  @hostmap = {}
  f = File.open( @hostfile, 'r' )
  f.readlines.each { |line|
    arr = line.split( /\s+/ )
    @hostmap[ arr[1] ] = arr[0]
  }
  f.close
end

#storeObject



27
28
29
30
31
32
33
34
35
# File 'lib/hostupdater.rb', line 27

def store
  f = File.open( @hostfile, 'w' )
  @hostmap.each_key do |key|
    hostname = key
    hostip = @hostmap[ key  ]
    f.write( hostip + " " + hostname +"\n")
  end
  f.close
end