Skip to content

Commit

Permalink
set start/end tokens as a class var
Browse files Browse the repository at this point in the history
  • Loading branch information
sbeam committed Apr 15, 2011
1 parent 33f59fc commit f1a402b
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/ghost/linux-host.rb
Expand Up @@ -17,18 +17,20 @@ def ==(other)
alias :hostname :host

@@hosts_file = '/etc/hosts'
@@start_token, @@end_token = '# ghost start', '# ghost end'


class << self
protected :new

def list
entries = []
in_ghost_area = false
File.open(@@hosts_file).each do |line|
if !in_ghost_area and line =~ /^# ghost start/
if !in_ghost_area and line =~ /^#{@@start_token}/
in_ghost_area = true
elsif in_ghost_area
if line =~ /^# ghost end/
if line =~ /^#{@@end_token}/o
in_ghost_area = false
elsif line =~ /^(\d+\.\d+\.\d+\.\d+)\s+(.*)$/
ip = $1
Expand Down Expand Up @@ -94,10 +96,10 @@ def write_out!(hosts)
out,over,seen_tokens = "",false,false

f.each do |line|
if line =~ /^# ghost start/
if line =~ /^#{@@start_token}/o
over,seen_tokens = true,true
out << line << new_ghosts
elsif line =~ /^# ghost end/
elsif line =~ /^#{@@end_token}/o
over = false
end

Expand All @@ -114,7 +116,7 @@ def write_out!(hosts)
end

def surround_with_tokens(str)
"\n# ghost start\n" + str + "\n# ghost end\n"
"\n#{@@start_token}\n" + str + "\n#{@@end_token}\n"
end
end
end

0 comments on commit f1a402b

Please sign in to comment.