class Ghost::Store::HostsFileStore

TODO: A lot of this duplicates Resolv::Hosts in Ruby stdlib.

Can that be modifiied to use tokens in place of this?

Constants

DEFAULT_FILE
MAX_HOSTS_PER_LINE

Attributes

file[RW]
path[RW]
section_name[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 18
def initialize(options = {})
  self.path         = options.fetch(:path, DEFAULT_FILE)
  self.section_name = options.fetch(:section_name)

  self.file = Ghost::TokenizedFile.new(self.path,
    "# #{self.section_name} start",
    "# #{self.section_name} end")
end

Public Instance Methods

add(host) click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 34
def add(host)
  sync do |buffer|
    buffer[host.ip] << host.name
    buffer_changed!
  end

  true
end
all() click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 53
def all
  sync do |buffer|
    buffer.map do |ip, hosts|
      hosts.map { |h| Ghost::Host.new(h, ip) }
    end.flatten.sort
  end
end
delete(host) click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 65
def delete(host)
  sync do |buffer|
    delete_host host, buffer, :strict
  end
end
delete_host(host, buffer, strict = false) click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 77
def delete_host(host, buffer, strict = false)
  result = SortedSet.new
  buffer.each do |ip, names|
    names.each do |name|
      if host.kind_of? Host
        next unless host.name == name
      elsif host.kind_of? String
        next unless host == name
      else
        next unless host.match(name)
      end

      next if host.respond_to?(:ip) && host.ip != ip && strict

      result << Ghost::Host.new(name, ip)
      names.delete(name)
      buffer_changed!
    end
  end
  result.to_a
end
empty() click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 99
def empty
  result = false
  sync do |buffer|
    unless buffer.empty?
      result = true
      buffer.replace({})
      buffer_changed!
    end
  end
  result
end
find(filter) click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 61
def find(filter)
  all.select { |host| host.name =~ filter }
end
purge(host) click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 71
def purge(host)
  sync do |buffer|
    delete_host host, buffer
  end
end
section_name=(name) click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 27
def section_name=(name)
  if self.section_name
    raise RuntimeError, "Cannot change section name"
  end
  @section_name = name
end
set(host) click to toggle source
# File lib/ghost/store/hosts_file_store.rb, line 43
def set(host)
  sync do |buffer|
    delete_host host, buffer
    buffer[host.ip] << host.name
    buffer_changed!
  end

  true
end