work 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/play CHANGED
@@ -16,22 +16,20 @@ end
16
16
  # Find the location of the user's work preferences.
17
17
  dotfile = Work::DotFile.locate
18
18
 
19
- case ARGV.first
20
19
  # We need a list of domains to work with, which is stored in a
21
20
  # ~/.work hidden file.
22
- when 'setup'
23
- dotfile = Work::DotFile.locate
21
+ if ARGV.first == 'setup'
24
22
  if dotfile.exists?
25
23
  puts "Work file already exists at #{dotfile.path}"
26
24
  else
27
- dotfile.write(Work::DEFAULT_DOMAINS) if !dotfile.exists?
25
+ dotfile.write!
28
26
  puts "Default work domains added to #{dotfile.path}"
29
27
  end
30
28
 
31
29
  # Not a setup command, so we'll need the `Teacher`
32
30
  else
33
31
  # Warn the user if they've not set up a domains list
34
- if dotfile.read.length == 0
32
+ if dotfile.domains.length == 0
35
33
  puts "Domain list is empty. Add work domains to #{dotfile.path}"
36
34
  puts "or run `#{$0} setup` to populate with the default list."
37
35
  exit
@@ -39,7 +37,7 @@ else
39
37
 
40
38
  # If we can't write to the hosts file, re-run but with `sudo`.
41
39
  begin
42
- p = Work::Teacher.new(dotfile.read, ARGV[1], ARGV[2])
40
+ p = Work::Teacher.new(dotfile.domains, dotfile.ip)
43
41
  rescue Work::HostsFileNotWritable
44
42
  sudome
45
43
  end
@@ -54,6 +52,9 @@ else
54
52
  p.play
55
53
  else
56
54
  puts "Bums on seats, please."
55
+ # Clear out any old data in case the domain list or IP address has
56
+ # been updated and we're just running `work` again without a `play`.
57
+ p.play
57
58
  p.work
58
59
  end
59
60
 
data/bin/work CHANGED
@@ -16,22 +16,20 @@ end
16
16
  # Find the location of the user's work preferences.
17
17
  dotfile = Work::DotFile.locate
18
18
 
19
- case ARGV.first
20
19
  # We need a list of domains to work with, which is stored in a
21
20
  # ~/.work hidden file.
22
- when 'setup'
23
- dotfile = Work::DotFile.locate
21
+ if ARGV.first == 'setup'
24
22
  if dotfile.exists?
25
23
  puts "Work file already exists at #{dotfile.path}"
26
24
  else
27
- dotfile.write(Work::DEFAULT_DOMAINS) if !dotfile.exists?
25
+ dotfile.write!
28
26
  puts "Default work domains added to #{dotfile.path}"
29
27
  end
30
28
 
31
29
  # Not a setup command, so we'll need the `Teacher`
32
30
  else
33
31
  # Warn the user if they've not set up a domains list
34
- if dotfile.read.length == 0
32
+ if dotfile.domains.length == 0
35
33
  puts "Domain list is empty. Add work domains to #{dotfile.path}"
36
34
  puts "or run `#{$0} setup` to populate with the default list."
37
35
  exit
@@ -39,7 +37,7 @@ else
39
37
 
40
38
  # If we can't write to the hosts file, re-run but with `sudo`.
41
39
  begin
42
- p = Work::Teacher.new(dotfile.read, ARGV[1], ARGV[2])
40
+ p = Work::Teacher.new(dotfile.domains, dotfile.ip)
43
41
  rescue Work::HostsFileNotWritable
44
42
  sudome
45
43
  end
@@ -54,6 +52,9 @@ else
54
52
  p.play
55
53
  else
56
54
  puts "Bums on seats, please."
55
+ # Clear out any old data in case the domain list or IP address has
56
+ # been updated and we're just running `work` again without a `play`.
57
+ p.play
57
58
  p.work
58
59
  end
59
60
 
data/lib/work.rb CHANGED
@@ -4,6 +4,7 @@ require 'work/dotfile'
4
4
 
5
5
  module Work
6
6
 
7
+ DEFAULT_IP = "127.0.0.1"
7
8
  DEFAULT_DOMAINS = %w{
8
9
  arstechnica.com boingboing.net cnet.com codinghorror.com crunchgear.com
9
10
  daringfireball.net digg.com en.wikipedia.org engadget.com engadgetmobile.com
data/lib/work/dotfile.rb CHANGED
@@ -1,7 +1,10 @@
1
+ require 'yaml'
2
+
1
3
  module Work
2
4
  class DotFile
3
5
 
4
- attr_accessor :path
6
+ attr_accessor :path, :ip, :domains
7
+ attr_writer :ip, :domains
5
8
 
6
9
  def self.locate
7
10
  @dotfile ||= self.new File.expand_path(File.join('~', '.work'))
@@ -9,19 +12,32 @@ module Work
9
12
 
10
13
  def initialize(path)
11
14
  @path = path
15
+ read!
16
+ end
17
+
18
+ def ip
19
+ @ip ||= Work::DEFAULT_IP
20
+ end
21
+
22
+ def domains
23
+ @domains ||= Work::DEFAULT_DOMAINS
12
24
  end
13
25
 
14
26
  def exists?
15
27
  File.exists? @path
16
28
  end
17
29
 
18
- def read
19
- File.readlines(path).collect { |d| d.strip }.sort rescue []
30
+ def read!
31
+ if exists?
32
+ data = YAML.load_file(path)
33
+ self.ip = data["ip"] if data.has_key?("ip")
34
+ self.domains = data["domains"].sort if data.has_key?("domains")
35
+ end
20
36
  end
21
37
 
22
- def write(data)
38
+ def write!
23
39
  File.open(path, 'w+') do |f|
24
- f.write(data.is_a?(Array) ? data.join("\n") : data)
40
+ f.write YAML::dump({ "ip" => ip, "domains" => domains })
25
41
  end
26
42
  end
27
43
 
data/lib/work/teacher.rb CHANGED
@@ -7,10 +7,10 @@ module Work
7
7
 
8
8
  attr_accessor :path, :domains, :ip
9
9
 
10
- def initialize(domains, path=nil, ip=nil)
10
+ def initialize(domains, ip=nil, path=nil)
11
11
  @domains = domains
12
+ @ip = ip || Work::DEFAULT_IP
12
13
  @path = path || "/etc/hosts"
13
- @ip = ip || "127.0.0.1"
14
14
  raise HostsFileNotWritable if !File.writable?(@path)
15
15
  end
16
16
 
data/lib/work/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Work
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: work
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tim Blair