class Roast::Group

Attributes

name[R]

Public Class Methods

new(name) click to toggle source
# File lib/roast/group.rb, line 5
def initialize(name)
  @name     = name
  @hosts    = {}
end

Public Instance Methods

<<(host) click to toggle source
# File lib/roast/group.rb, line 30
def <<(host)
  @hosts[host.hostname] = host
end
[](hostname) click to toggle source
# File lib/roast/group.rb, line 34
def [](hostname)
  @hosts[hostname]
end
delete_host(entry) click to toggle source
# File lib/roast/group.rb, line 46
def delete_host(entry)
  deleted = []
  if entry =~ Host::IP_PATTERN
    @hosts.each { |k, h| deleted << @hosts.delete(k) if h.ip_address == entry }
  else
    @hosts.each { |k, h| deleted << @hosts.delete(k) if h.hostname == entry }
  end

  deleted
end
disable!() click to toggle source
# File lib/roast/group.rb, line 10
def disable!
  hosts.each { |h| h.disable! }
end
disabled?() click to toggle source
# File lib/roast/group.rb, line 18
def disabled?
  hosts.all? { |h| h.disabled? }
end
enable!() click to toggle source
# File lib/roast/group.rb, line 14
def enable!
  hosts.each { |h| h.enable! }
end
enabled?() click to toggle source
# File lib/roast/group.rb, line 22
def enabled?
  hosts.all? { |h| h.enabled? }
end
find_host(entry) click to toggle source
# File lib/roast/group.rb, line 38
def find_host(entry)
  if entry =~ Host::IP_PATTERN
    hosts.select { |h| h.ip_address == entry }
  else
    hosts.select { |h| h.hostname == entry }
  end
end
hosts() click to toggle source
# File lib/roast/group.rb, line 26
def hosts
  @hosts.values
end
to_cli(max_indent = nil) click to toggle source
# File lib/roast/group.rb, line 57
def to_cli(max_indent = nil)
  string = " - \033[4m#{name}\033[0m\n"
  max = max_indent || hosts.map { |h| h.hostname.size }.max

  hosts.each do |host|
    padding = ' ' * (max - host.hostname.size + 4)
    if host.disabled?
      string << "   \033[31m #{["00D7".to_i(16)].pack("U*")} "
    else
      string << '      '
    end
    string << "#{host.hostname}#{padding}#{host.ip_address}"
    string << "  # #{host.alias}" if host.alias
    string << "\033[0m\n"
  end

  string
end
to_hosts_file() click to toggle source
# File lib/roast/group.rb, line 76
def to_hosts_file
  max     = hosts.map { |h| h.ip_address.size }.max
  section = "## [#{name}]\n"

  hosts.each do |host|
    padding = ' ' * (max - host.ip_address.size + 4)
    section << '# ' if host.disabled?
    section << "#{host.ip_address}#{padding}#{host.hostname}"
    section <<  "  # #{host.alias}" if host.alias
    section << "\n"
  end

  section
end