Class: Work::Teacher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domains, ip = nil, path = nil) ⇒ Teacher

Returns a new instance of Teacher.



10
11
12
13
14
15
# File 'lib/work/teacher.rb', line 10

def initialize(domains, ip=nil, path=nil)
  @domains = domains
  @ip = ip || Work::DEFAULT_IP
  @path = path || "/etc/hosts"
  raise HostsFileNotWritable if !File.writable?(@path)
end

Instance Attribute Details

#domainsObject

Returns the value of attribute domains.



8
9
10
# File 'lib/work/teacher.rb', line 8

def domains
  @domains
end

#ipObject

Returns the value of attribute ip.



8
9
10
# File 'lib/work/teacher.rb', line 8

def ip
  @ip
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/work/teacher.rb', line 8

def path
  @path
end

Instance Method Details

#msgObject



21
22
23
# File 'lib/work/teacher.rb', line 21

def msg
  "# Do not modify.  Controlled by Work: http://github.com/timblair/work"
end

#playObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/work/teacher.rb', line 37

def play
  File.open(path, 'r+') do |f|
    lines = f.read
    if lines.match /#{separator}/
      self.class.overwrite_file(f) do
        lines.gsub /([\s\n]*#{separator}.*#{separator})/m, ""
      end
    end
  end
end

#recache!Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/work/teacher.rb', line 48

def recache!
  case Sys::Uname.sysname.downcase
  when 'darwin'
    system 'dscacheutil -flushcache'
  when 'linux'
    system '/etc/init.d/networking restart'
  else
    puts "Warning: unknown system type #{Sys::Uname.sysname.downcase}."
    puts "You'll need to flush your DNS cache yourself..."
  end
end

#separatorObject



17
18
19
# File 'lib/work/teacher.rb', line 17

def separator
  @separator ||= "#==#{self.class.name}/#{Digest::MD5.hexdigest(self.class.name + path)}"
end

#workObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/work/teacher.rb', line 25

def work
  File.open(path, 'r+') do |f|
    lines = f.read
    if !lines.match /#{separator}/
      self.class.overwrite_file(f) do
        extra = domains.collect { |d| "#{ip}\t#{d}\twww.#{d}" }
        lines << "\n\n#{separator}\n#{msg}\n#{extra.join("\n")}\n#{separator}"
      end
    end
  end
end