module RHosts::ConsoleMethods

Public Instance Methods

actives() click to toggle source
# File lib/rhosts/console/app.rb, line 6
def actives
  @actives ||= Hash.new{ |h, k| h[k] = Set.new }
end
display(title, mappings) click to toggle source

print mappings

# File lib/rhosts/console/app.rb, line 43
def display(title, mappings)
  puts "### #{title}"
  mappings.each do |ip, hosts|
    puts ip
    hosts.each{ |host| puts "  #{host}" }
    puts ''
  end
end
inactives() click to toggle source
# File lib/rhosts/console/app.rb, line 10
def inactives
  @inactives ||= Hash.new{ |h, k| h[k] = Set.new }
end
map(target) click to toggle source
# File lib/rhosts/console/app.rb, line 14
def map(target)
  process(target) do |host, ip|
    inactivate(host)

    unless inactives[ip].empty?
      inactives[ip].delete_if{ |h| h == host }
    end

    inactives.delete(ip) if inactives[ip].empty?

    actives[ip] << host
    puts "map #{host} to #{ip}"
  end
end
unmap(target) click to toggle source
# File lib/rhosts/console/app.rb, line 29
def unmap(target)
  process(target) do |host, ip|
    unless actives[ip].empty?
      actives[ip].delete_if{ |h| h == host }
    end

    actives.delete(ip) if actives[ip].empty?

    inactives[ip] << host
    puts "unmap #{host} from #{ip}"
  end
end

Private Instance Methods

inactivate(host) click to toggle source
# File lib/rhosts/console/app.rb, line 79
def inactivate(host)
  actives.each do |active_ip, active_hosts|
    if active_hosts.include?(host)
      active_hosts.delete(host)
      actives.delete(active_ip) if active_hosts.empty?

      inactives[active_ip] << host
    end
  end
end
process(target, &block) click to toggle source
# File lib/rhosts/console/app.rb, line 53
def process(target, &block)
  raise ArgumentsError.new('mapping target must be Hash') unless target.instance_of? Hash

  # TODO
  # before_actions.each{ |action| action.call }

  target.each do |host, ip|
    host = alias_hosts[host] || host
    ip   = alias_ips[ip]     || ip

    ip_without_zone_index = ip.split('%')[0]
    unless IPAddress.valid?(ip_without_zone_index)
      warn "#{ip} is invalid IP Address!"
      next
    end

    block.call(host, ip)
  end

  RHosts::Filer.backup if RHosts.config.make_backup?
  RHosts::Filer.save(actives, inactives)

  # TODO
  # after_actions.each{ |action| action.call }
end