Class: Pebbles::Productivity10x::SwitchHosts

Inherits:
Object
  • Object
show all
Defined in:
lib/pebbles-productivity10x-switch_hosts.rb,
lib/pebbles-productivity10x-switch_hosts/version.rb

Defined Under Namespace

Modules: Default

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ SwitchHosts

Returns a new instance of SwitchHosts.



32
33
34
35
36
37
# File 'lib/pebbles-productivity10x-switch_hosts.rb', line 32

def initialize opt = {}
  @hosts_path       = opt[:hosts_path]       || Default::HOSTS_PATH
  @original_extname = opt[:original_extname] || Default::ORIGINAL_EXTNAME
  @deny_extname     = opt[:deny_extname]     || Default::DENY_EXTNAME
  @template         = opt[:template]         || Default::TEMPLATE
end

Instance Method Details

#allowObject



43
44
45
# File 'lib/pebbles-productivity10x-switch_hosts.rb', line 43

def allow
  use(original_file)
end

#denyObject



39
40
41
# File 'lib/pebbles-productivity10x-switch_hosts.rb', line 39

def deny
  use(deny_file)
end

#deny_fileObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pebbles-productivity10x-switch_hosts.rb', line 59

def deny_file
  deny_path = @hosts_path + @deny_extname
  unless File.exist?(deny_path)
    FileUtils.cp original_file, deny_path, preserve: true
    File.open(deny_path, 'a') do |f|
      f << "\n"
      f << Default::DENY_HOSTS.map { |host| @template.result(binding) }.join
    end
  end
  File.new deny_path
end

#hosts_fileObject



47
48
49
# File 'lib/pebbles-productivity10x-switch_hosts.rb', line 47

def hosts_file
  File.new(@hosts_path)
end

#original_fileObject



51
52
53
54
55
56
57
# File 'lib/pebbles-productivity10x-switch_hosts.rb', line 51

def original_file
  original_path = @hosts_path + @original_extname
  unless File.exist?(original_path)
    FileUtils.cp(hosts_file, original_path, preserve: true)
  end
  File.new original_path
end