Class: Work::Teacher
- Inherits:
-
Object
- Object
- Work::Teacher
- Defined in:
- lib/work/teacher.rb
Instance Attribute Summary collapse
-
#domains ⇒ Object
Returns the value of attribute domains.
-
#ip ⇒ Object
Returns the value of attribute ip.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(domains, ip = nil, path = nil) ⇒ Teacher
constructor
A new instance of Teacher.
- #msg ⇒ Object
- #play ⇒ Object
- #recache! ⇒ Object
- #separator ⇒ Object
- #work ⇒ Object
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
#domains ⇒ Object
Returns the value of attribute domains.
8 9 10 |
# File 'lib/work/teacher.rb', line 8 def domains @domains end |
#ip ⇒ Object
Returns the value of attribute ip.
8 9 10 |
# File 'lib/work/teacher.rb', line 8 def ip @ip end |
#path ⇒ Object
Returns the value of attribute path.
8 9 10 |
# File 'lib/work/teacher.rb', line 8 def path @path end |
Instance Method Details
#msg ⇒ Object
21 22 23 |
# File 'lib/work/teacher.rb', line 21 def msg "# Do not modify. Controlled by Work: http://github.com/timblair/work" end |
#play ⇒ Object
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 |
#separator ⇒ Object
17 18 19 |
# File 'lib/work/teacher.rb', line 17 def separator @separator ||= "#==#{self.class.name}/#{Digest::MD5.hexdigest(self.class.name + path)}" end |
#work ⇒ Object
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 |